RE: [PHP] Simple logic but can't get it right
| From: | Daevid Vincent | Date: | Thu, 09 Nov 2006 06:08:49 +0000 |
| Subject: | RE: [PHP] Simple logic but can't get it right | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-244420@lists.php.net to get a copy of this message | ||
Since you don't declare variables in PHP, they are preinitialized for you as
'false' or '0'
So since $b is not set initially, it is 0 therefore second elseif passes.
> -----Original Message-----
> From: Choy, Wai Yew [mailto:waiyew.choy@ap.att.com]
> Sent: Wednesday, November 08, 2006 9:34 PM
> To: php-general@lists.php.net
> Subject: [PHP] Simple logic but can't get it right
>
> Hi gurus,
>
>
>
> I've the following piece of PHP code....It is a simple logic
> but I can't
> get it right....The output result of $id is "b"!!...It should be
> "outside", right??
>
>
>
> I think it is the variable $b....Coz' it depend on the
> previous check (
> if ($a == 0) ) for the value... If this is the case, how can
> I get this
> logic works??
>
>
>
> Thanks a million,
>
> Choy
>
>
>
>
>
>
>
>
>
> <?php
>
>
>
> $a = 1;
>
> $id = "outside";
>
>
>
> if ($a == 0) {
>
>
>
> $b = 1;
>
> $id = "a";
>
>
>
> }
>
>
>
>
>
> elseif ($b == 0) {
>
>
>
> $c = 1;
>
> $id = "b";
>
>
>
> }
>
>
>
>
>
> elseif ($c == 0){
>
>
>
> $d = 1;
>
> $id = "c";
>
>
>
> }
>
>
>
>
>
> echo "ID = $id<br>"; //Output is "b"...WHY?? It should be
> "outside"...
>
>
>
>
>
>
>
> ?>
>
>