note 100210 added to control-structures.alternative-syntax

From: Date: Fri, 01 Oct 2010 07:18:57 +0000
Subject: note 100210 added to control-structures.alternative-syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-172865@lists.php.net to get a copy of this message
Sorry, my previous comment is mistaken. It should be: You can NOT perform if/elseif/else in one line like this: <?php $c = "c"; echo ($c == "a") ? 1 : ($c == "b") ? 2 : ($c == "c") ? 3 : 0; ?> If we set $c = "b" the result will be still 3. Why? After performing several testings, the real logic behind that line would be more similar to this: <?php $c = "b"; $a = ($c == "a") ? 1 : ($c == "b"); //which in our case will be "true" (as it falls in the false statement). $b = ($a) ? 2 : ($c == "c"); //which will be "2" as $a == true $c = $b ? 3 : 0; //as $b > 0 turns the evaluation "true" and thus the answer is 3. ?> So in order to be able to use if/elseif/else in one line, grouping is required (as it is expected): <?php $c = "b"; echo ($c == "a") ? 1 : (($c == "b") ? 2 : (($c == "c") ? 3 : 0)); //result: 2 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 211.6.179.194 ---- Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php Edit -- https://master.php.net/note/edit/100210 Del: integrated -- https://master.php.net/note/delete/100210/integrated Del: useless -- https://master.php.net/note/delete/100210/useless Del: bad code -- https://master.php.net/note/delete/100210/bad+code Del: spam -- https://master.php.net/note/delete/100210/spam Del: non-english -- https://master.php.net/note/delete/100210/non-english Del: in docs -- https://master.php.net/note/delete/100210/in+docs Del: other reasons-- https://master.php.net/note/delete/100210 Reject -- https://master.php.net/note/reject/100210 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172865) next »