note 128765 added to language.operators.comparison

From: Date: Fri, 28 Jul 2023 13:00:46 +0000
Subject: note 128765 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-220725@lists.php.net to get a copy of this message
You said: "The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued." Well - it does work, but it's not particularly good code. Better is: return ($var == 43) ? $a : $b; However, using ternary operators within string concatenations won't work unless you enclose the whole expression within brackets, eg. <?php function test ($var) { return 'The statement ' . $var . ' = 42 is ' . (($var == 42) ? 'true' : 'false') . '.'; } echo test (43); ?> will return 'The statement 43 = 42 is false.', as expected. Failure to enclose the whole ternary expression within brackets will just return the first option, ie. the word 'true' (and nothing else) regardless of the number passed to $var. So, to be cast iron using a ternary operator within a return directive, use: return (($var == 42) ? $a : $b); ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 2.99.51.218) ---- Manual Page -- https://php.net/manual/en/language.operators.comparison.php Edit -- https://main.php.net/note/edit/128765 Del: integrated -- https://main.php.net/note/delete/128765/integrated Del: useless -- https://main.php.net/note/delete/128765/useless Del: bad code -- https://main.php.net/note/delete/128765/bad+code Del: spam -- https://main.php.net/note/delete/128765/spam Del: non-english -- https://main.php.net/note/delete/128765/non-english Del: in docs -- https://main.php.net/note/delete/128765/in+docs Del: other reasons-- https://main.php.net/note/delete/128765 Reject -- https://main.php.net/note/reject/128765 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220725) next »