Re: Re: Like ternary but without the else.
| From: | anirudh dutt | Date: | Tue, 01 Mar 2005 05:25:14 +0000 |
| Subject: | Re: Re: Like ternary but without the else. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-209662@lists.php.net to get a copy of this message | ||
dunno if u've read the options ppl have given u or u've ignored their answers:
imho, these are the answers to ur question:
On Sat, 26 Feb 2005 00:46:36 +0100, rich <richgray@gmail.com> wrote:
>
> is this what you're after?
>
> $this = ($something ? $that : $this)
>
> rich
>
On Fri, 25 Feb 2005 17:35:03 -0800, Chris W. Parker
<cparker@swatgear.com> wrote:
> Jason Barnett <mailto:jason.barnett@telesuite.com>
> on Friday, February 25, 2005 2:10 PM said:
>
> > $this = $something ? $that : null;
>
> That won't work because I'm setting a default value for $this and want
> it to change only if it has to. The long winded way to write this would
> be:
>
> <?php
>
> $myVariable = "default";
>
> if($somethingElse == "a value")
> {
> $myVariable = "new value";
> }
> else
> {
> // don't do anything
> }
>
> ?>
>
> Chris.
>
so this one still applies:
On Sat, 26 Feb 2005 01:03:51 +0100, Jochem Maas <jochem@iamjochem.com> wrote:
> 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()
>
to put it simply, use:
$this = ($something ? $that : $this)
it does have the else part of the ternary operator, but it's the closest.
it assumes that 'this' has a value it already should have and unless
the condition 'something' is true, the value remains the same (or
remains null/not set). in which case...
On Mon, 28 Feb 2005 17:12:15 -0800 (PST), Richard Lynch <ceo@l-i-e.com> wrote:
> If you're not *SURE* $this has a value:
>
> $this = ($something ? $that : (isset($this) ? $this : NULL));
>
u've also mentioned:
On Fri, 25 Feb 2005 17:28:13 -0800, Chris W. Parker
<cparker@swatgear.com> wrote:
> Justin Lilly <mailto:justinlilly@gmail.com>
> on Friday, February 25, 2005 3:10 PM said:
>
> > This should do the trick:
> >
> > $something ? $this=$that
>
> Actually that gives a synax error. But I've figured it out based on your
> suggestion.
>
> It's actually:
>
> <?php
>
> $something && $this = $that;
>
> ?>
this seems to do the trick as per "Like ternary but without the else".
for those who want to know, u can even use:
$a && print "this only prints if 'a' is non false"
but not echo since it doesn't return a value.
<?php
$b = 2;
$a = !true;
$a && $b = 5;
$a && print "$a, $b (boolean-in-a-line worked)<br>\n";
echo "$a, $b :: echo line";
?>
u get either:
1, 5 (boolean-in-a-line worked)
1, 5 :: echo line
- OR -
, 2 :: echo line
depending on combos with $a = true or $a = !true.
true prints as 1.
so u've found ur answer.
--
]#
Anirudh Dutt
...pilot of the storm who leaves no trace
like thoughts inside a dream