Advertisement
ridjis

PHP if/elsif/else

Mar 29th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. // I stopped at the if/elseif/else part... there're that many variations how you can write the same thing in PHP:
  2.  
  3. if ( $age > 10 ) {
  4.     echo "foo";
  5. } elseif ( 10 === $age ) { # strict comparison
  6.    echo "bar";
  7. } else {
  8.     echo "baz";
  9. }
  10.  
  11. // In above you can put the brackets on different lines. You can as well avoid them completely. Or you can do other stuff:
  12.  
  13. $age > 10
  14.     ? print "bar"
  15.     : print "foo";
  16.  
  17. // Or you can go nuts:
  18.  
  19. $result = "foo";
  20. $age < 10 AND $result = "bar";
  21. echo $result;
  22.  
  23. // and so on. This is just the tip of the iceberg. While Python is nice, I personally prefer PHP for allowing full type safety like you // know it from Java or C# while still having the ability to write with a very custom style.
  24.  
  25. // Another example of if:
  26.  
  27. if ( $age > 10 ) :
  28.     print "foo";
  29. elseif ( 10 === $age ) :
  30.     print "baz";
  31. else :
  32.     print "bar";
  33. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement