Jump to content

If statement formating


evolooshun

Recommended Posts

I need to put the following code into an if / else statement like similar to this code.

<?php if ( $price > "0.00" ) {
print("Call for pricing"); }
else {
print("18001234567"); }

Where the first line would be the same but changing the "Call for pricing" with this code:

<div class="product">
<input value="<?php print($itemID); ?> <?php print($title); ?>" class="product-title" type="hidden">
<input value="<?php print("$thisPic"); ?>" class="product-image" type="hidden">
<input value="<?php print("$price"); ?>" class="product-price" type="hidden">
<div title="Add to cart" role="button" tabindex="0" class="googlecart-add-button">
</div></div>										

 

I cant figure out how to format it properly to not break the <?php print($itemID); ?> inside the if statement.  Please help, Ive tried everything I can think of.

I think its supposed to be something like this:

<?php if ( $price > "0.00" ) {
echo "<div class="product">";
echo "<input value="<?php print($itemID); ?> <?php print($title); ?>" class="product-title" type="hidden">";
echo "<input value="<?php print("$thisPic"); ?>" class="product-image" type="hidden">";
echo "<input value="<?php print("$price"); ?>" class="product-price" type="hidden">";
echo "<div title="Add to cart" role="button" tabindex="0" class="googlecart-add-button"></div></div>";

 

I just cant figure out how to format it properlly

Link to comment
https://forums.phpfreaks.com/topic/198046-if-statement-formating/
Share on other sites

There are many ways to do that. If the 18001234567 is static you can do it like this:

 

<?php if( $price > "0.00" ): ?>

   <div class="product">
   <input value="<?php print($itemID); ?> <?php print($title); ?>" class="product-title" type="hidden">
   <input value="<?php print("$thisPic"); ?>" class="product-image" type="hidden">
   <input value="<?php print("$price"); ?>" class="product-price" type="hidden">
   <div title="Add to cart" role="button" tabindex="0" class="googlecart-add-button">
   </div></div>  

<?php else: ?>

   <p>18001234567</p>

<?php endif; ?>

Yes, and you can improve your if statement by using something like this instead... it's not good practice to put numbers in strings like you did

 

<?php if( (float)$price > 0 ): ?>

 

Or if you're already certain $price is a float the you can remove the (float) cast.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.