DISQUS

Tech-Recipes: PHP if statement syntax | PHP programming | Tech-Recipes

  • Tom Elders · 10 months ago
    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.
  • peace love · 9 months ago
    Thats a great shorthand, and gives good readability!
  • Sagar · 5 months ago
    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;
  • Sagar · 5 months ago
    My above suggestion is the same as what Tom Elders suggested. I support it
  • Name · 2 months ago
    I am a novie in php programming.I was looking for "elseif" in php and this writ up heloped me to do it..

    Thanking you
    Anand V Mohan
  • davak · 2 months ago
    Glad we could help!