DISQUS

DISQUS Hello! Tech-Recipes is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Tech-Recipes

Cookbook of Tech Tutorials
Jump to original thread »
Author

PHP if statement syntax | PHP programming | Tech-Recipes

Started by qdideas · 9 months ago

No excerpt available. Jump to website »

4 comments

  • I think describing the ternary operator "?" as an if statement is a bit misleading. For example, inside a while loop:

    while($row = mysql_fetch_row):
    if($condition = true) { $something = $value }
    endwhile;

    that works. However, consider the following

    while($row = mysql_fetch_row):
    $something = ($condition = $true) ? $value : null;
    endwhile;

    If the condition is met, everything works fine, but on the next iteration of the loop, if the condition is not met, $something is set to null, which is probably not the desired effect 9 times out of 10.
  • Thats a great shorthand, and gives good readability!
  • yes, ternary condition does give a problem

    I would advise the following as well

    while($row = mysql_fetch_row):
    if($condition = true) { $something = $value }
    endwhile;
  • My above suggestion is the same as what Tom Elders suggested. I support it

Add New Comment

Returning? Login