I need help writing an if/else statement. I have a piece of code(below) that displays the price of my product on the page. What I want to do is add a statement that basically says if the price is zero, then display "Free" rather than $0.00 Any help is appreciated <div class="price"> <?php if (!$product['special']) { ?> <?php echo $product['price']; ?> <?php } else { ?> <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span> <?php } ?> Code (markup): Thanks is advance
Just add this. <?php } else if ($product['price'] == '0.00') { ?> echo 'Free'; <?php } else { ?> ... PHP:
STOP OPENING AND CLOSING PHP ON EVERY BLASTED LINE FOR NO GOOD REASON!!!!!!!!! (actually, that needs more exclamation points) Seriously, why the blue blazes do people do that?!? As to your "problem", I'd lose the if statements for inline evaluation, and since pretty much all those output values need the same check, I'd make a function to handle the 0 or free. // assumes you are already in PHP. function freeCheck($price) { return $price > 0 ? $price : 'free'; } echo ' <div class="price"> ', $product['special'] ? '<span class="price-old">'.freeCheck($product['price']).'</span> <span class="price-new">'.freeCheck($product['special']).'</span>' : freeCheck($product['price']),' </div>'; Code (markup): I closed the DIV, but that was just a guess. There is NO reason to be constantly opening and closing PHP like that -- which is how the taffer before this post's version is 100% gibberish since the echo isn't even inside PHP. Now I know why I left the keepers.
Because the vast majority of people doesn't know how to escape the quote marks within php, so they open and close it at every single statement. You forget a backslash, your php goes "Syntax Error: Unexpected """ at the line XX". But still, it's not an excuse. (actually, it says "Parse error: syntax error, unexpected T_STRING in __ on line XX", but you get the idea.)
True enough, with all the halfwits using double quotes for everything -- when if you use singles, you only need to escape singles -- which since you don't need singles in your markup, the only place they should occur is in content... aka something PHP should be plugging into the markup from a variable anyways.
I must be a halfwit then... $this->thread_box .= "<a class=\"btn_comment_style\" href=\"delete.comment.php?title=" . $row['title'] . "&page=" . $row['thread'] . "\" onclick=\"return confirm('Are you sure you want to delete this comment?');\">Delete</a>"; PHP: BTW escaping single quotes doesn't work if you have javascript in the string
Uhm, since when? Unless you misunderstood what I meant by using single quotes for the PHP and thought I meant for the HTML attributes, which is also stupid. $this->thread_box .= ' <a class="btn_comment_style" href="delete.comment.php?title=' . $row['title'] . '&page=' . $row['thread'] . '" onclick="return confirm(\'Are you sure you want to delete this comment?\');" >Delete</a>'; Code (markup): Far, FAR simpler to deal with, since you only need to escape two singles instead of every double. I also cleaned it up to be easier to ready by not stuffing everything on one painful to follow line. NOT that I believe in using the onevent methods in the markup -- firm believer scripting should be self attaching, not set using attributes, but that's me... Just as I think <?php ?> should be stricken from PHP completely and STYLE should be obsolete as a tag / deprecated as an attribute, I also think the onevent attributes should be deprecated.