PHP white space rendering in source code formatting


I am building out a template for Wordpress using some custom field functions and variables. I am having a bit of a formatting problem.

I try to keep my code fairly formatted:

<div class="lp-panel

  if( !empty( $lp_custom_class ) ){
    echo ' ' . $lp_custom_class;


  if( !empty($lp_panel_styles) ){
    echo ' ' . $lp_panel_styles;

      <?php  echo $lp_bg_type; ?>

But, when I write my code this way, the white space of my file actually gets rendered in the browser source code.

So my div class is broken to another line, instead of being clean and on the same line like:

<div class="first-class

when I'd like it like:

<div class="first-class second-class">

Any input would be appreciated. Also, if anyone has any suggested resources about clean code formatting, or anything else they'd like to share, I'm sure others of my skill level would get a lot out of it.