note 46599 added to control-structures.alternative-syntax

From: Date: Sat, 16 Oct 2004 18:15:55 +0000
Subject: note 46599 added to control-structures.alternative-syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-78673@lists.php.net to get a copy of this message
I'd like to further explain something that had me confused for a while, as I wasn't reading the examples very carefully. When using the short if else syntax there are two styles: return (empty($value)) ? false : $value; is just like: if(empty($value)) return false; else return $value (and remember that if you're just executing one statement per branch, no brackets are required) However the second "inline" style is what's being used in the nested examples below and the difference is that: $string = 'The var Value is equal to '.(empty($value) ? 'empty' : $value); the entire statement is inside of the outer brackets. Also note that while the first style can only be used in a normal block of code, you can place the inline style ANYWHERE, when calling a function, creating a foreach loop, anywhere a string or variable would normally go. So the following is valid perfectly valid: <?php foreach(explode(':',(count($a)>3?$small:$big)) as $item) echo ($item == 3) ? 'three' : 'not three' ; ?> ---- Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46599 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46599&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46599&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78673) next »