Re: Like ternary but without the else.
| From: | Jochem Maas | Date: | Sat, 26 Feb 2005 00:03:51 +0000 |
| Subject: | Re: Like ternary but without the else. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-209475@lists.php.net to get a copy of this message | ||
Chris W. Parker wrote:
Hello, I couldn't find this anywhere on google or PHP's site but I'm pretty sure there's an answer to it. How can I turn the following into something that resembles the ternary operator?write an extension to the php engine? don't think it exists.
<?php if($something) {just do: if ($something) $this = $that; ..personally I don't like braceless if statements.$this = $that;}
?>or one of these might work for you; $this = ($something) ? $that: $this; // if $this already exists. or $this = ($something) ? $that: null; // if $this does not exist and your happy to use is_null()
I seem to remember it looking something like: <?php $this = ($something) || $that; ?>btw your not using 'this' as an actual variable name are you? (unless its on purpose for doing bad(tm) things in php4 :-)
Although this isn't syntactically wrong, i.e. no errors, it did not do what I hoped it would do. (What it does is assign 1/true.)what it does is assign the result of the expression (($something) _or_ $that) to $this. if either of those equate to true (dont forget php autotypecasts), then the expression is true, its a boolean 'or' so it will always make $this a boolean, which print as: php -r 'echo true;' // echos 1 php -r 'echo false;' // echos ''
Thanks! Chris. p.s. No need to respond if your suggestion is: <?php if($something) { $this = $that; } ?>