|
Posted by Jochem Maas on 10/04/31 11:09
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)
> {
> $this = $that;
> }
just do:
if ($something) $this = $that;
...personally I don't like braceless if statements.
>
> ?>
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; }
>
> ?>
>
Navigation:
[Reply to this message]
|