note 78911 added to language.operators

From: Date: Thu, 01 Nov 2007 22:13:30 +0000
Subject: note 78911 added to language.operators
Groups: php.notes 
Request: Send a blank email to php-notes+get-131924@lists.php.net to get a copy of this message
Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity. You cannot write code like this (as you may have accustomed to in C/C++): <? $a = 2; echo ( $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 4 ? 'four' : 'other'); echo "\n"; // prints 'four' ?> You need to add brackets to get the results you want: <? $a = 2; echo ($a == 1 ? 'one' : ($a == 2 ? 'two' : ($a == 3 ? 'three' : ($a == 4 ? 'four' : 'other') ) ) ); echo "\n"; //prints 'two' ?> ---- Server IP: 217.13.201.10 Probable Submitter: 84.19.182.242 ---- Manual Page -- http://www.php.net/manual/en/language.operators.php Edit -- https://master.php.net/note/edit/78911 Del: integrated -- https://master.php.net/note/delete/78911/integrated Del: useless -- https://master.php.net/note/delete/78911/useless Del: bad code -- https://master.php.net/note/delete/78911/bad+code Del: spam -- https://master.php.net/note/delete/78911/spam Del: non-english -- https://master.php.net/note/delete/78911/non-english Del: in docs -- https://master.php.net/note/delete/78911/in+docs Del: other reasons-- https://master.php.net/note/delete/78911 Reject -- https://master.php.net/note/reject/78911 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131924) next »