Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div inside php echo [closed]

Tags:

php

i have this:

<?php  
       if ( ($cart->count_product) > 0) { 
           print $cart->count_product; 
       } else { 
           print ''; 
       }  
?>

and i need to put print $cart->count_product inside of <div class="my_class"></div>

I tried different ways, but i'm missing something in syntax. I'll be glad if someone could help.

like image 375
LifeIsShort Avatar asked Mar 19 '13 13:03

LifeIsShort


4 Answers

You can do the following:

echo '<div class="my_class">';
echo ($cart->count_product > 0) ? $cart->count_product : '';
echo '</div>';

If you want to have it inside your statement, do this:

if($cart->count_product > 0) 
{
    echo '<div class="my_class">'.$cart->count_product.'</div>';
}

You don't need the else statement, since you're only going to output the above when it's truthy anyway.

like image 79
BenM Avatar answered Oct 22 '22 17:10

BenM


Try this,

<?php  if ( ($cart->count_product) > 0) { ?>
         <div class="my_class"><?php print $cart->count_product; ?></div>
<?php } else { 
          print ''; 
}  ?>
like image 45
Edwin Alex Avatar answered Oct 22 '22 17:10

Edwin Alex


You can do this:

<div class"my_class">
<?php if ($cart->count_product > 0) {
          print $cart->count_product; 
      } else { 
          print ''; 
      } 
?>
</div>

Before hitting the div, we are not in PHP tags

like image 23
UnholyRanger Avatar answered Oct 22 '22 16:10

UnholyRanger


You can also do this,

<?php 
if ( ($cart->count_product) > 0) { 
  $print .= "<div class='my_class'>"
  $print .= $cart->count_product; 
  $print .= "</div>"
} else { 
   $print = ''; 
} 
echo  $print;
?>
like image 39
believe me Avatar answered Oct 22 '22 16:10

believe me