PHP Code shortcuts to if/else statements

January 1st, 2009

Here are some very useful PHP code shortcuts that I have learned. You may have seen them before or you may not have, but if you didn’t understand them, hopefully you will after reading this article.

The first is the Ternary Operator

$var = (expression) ? 'value if true' : 'value if false';
//The above does the same as the following:
if (expression)
	$var = 'value if true';
	$var = 'value if false';
//Example use:
$name = empty($me) ? $you : $me; //sets $name to $me if $me is not empty, or to $you if $me is empty

The next two examples show how to use the “or” and “and” operators (also known as || and &&) as shortcuts

(expression1) or (expression2);
//The above does the same as the following:
if (!expression1)
//Example use:
//I actually found this line of code in the Yii framework
defined('YII_DEBUG') or define('YII_DEBUG',true); //Defines the constant YII_DEBUG if it has not been defined already

The above works because in the or operator, the right hand expression is not evaluated unless the left hand expression is evaluated to false (if it evaluates to true, there is no need for the right-hand side to be solved anyways – and PHP tries to be efficient)

The and operator does the exact opposite thing:

(expression1) and (expression2);
//The above does the same as the following:
if (expression1)
//Example use:
($page > LAST_PAGE) and ($page = LAST_PAGE) //does not allow the page number to be set past the last page

The reason you may want to use these would be to save typing, and to increase readability (in the case of the Ternary Operator anyways, you can argue the other two are less readable). Even if you do not like to use these, it is important to know them in case you ever find yourself reading other people’s code.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • Technorati
  • Reddit
  • RSS
  • Twitter

Categories: Web Development

Leave a comment

Comments Feed5 Comments

  1. Conor

    thanks for the tutorial!

  2. Okapi

    Ah the good old Ternary Operator (See more: Available in other languages too.

    I didn’t know about the AND and OR operators! Thanks

  3. David Johnston

    Hi Jonah, thanks for the info. Wonderfing if you could assist me. Looking for a php code to do the following. I’d like to show a swf banner 400×92 pixels on a page. However if the banner file can’t be found in a folder called “banners” then I would like it not to show.
    If BANNER1.SWF is found, show it, else leave blank.
    if BANNER2.SWF is found, show it, else leave blank.
    and so on for about 8 banners.
    Is there anyway that when it’s left blank that it doesn’t take up that space? Meaning, the page would move up?

    Thanks ahead of time.

  4. Jonah

    @David Johnston

    This is definitely possible but this question doesn’t really belong here.. I recommend asking on this website:

  5. john dow

    do not use
    if($page = LAST_PAGE)

    better use
    if($page == LAST_PAGE) {


    due to readability und prevent side-effects

Leave a comment

+ two = 5

Feed / PHP Code shortcuts to if/else statements