RE: [PHP] nested if parse error
| From: | Ford, Mike [LSS] | Date: | Thu, 30 May 2002 15:02:42 +0000 |
| Subject: | RE: [PHP] nested if parse error | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-99937@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Miroslav Figlar [mailto:mfiglar@genesispo.sk]
> Sent: 30 May 2002 13:05
> To: php-general@lists.php.net
> Subject: [PHP] nested if parse error
>
>
> could somebody explain me please what is wrong with this
> piece of code?
> <?
> if (1):
> if (1) echo "hello";
> else:
> endif;
> ?>
Well, first off you've broken the golden rule, which is: Don't mix the :-type syntax with
the {}-type or completely bare syntaxes.
In more detail: on reaching the "else", PHP matches it with the nearest preceding if;
this is the "If (1) echo ..." line, which isn't using :-type syntax, so it expects
the else also not to use :-type syntax -- so the : is a parse error.
Using :-type syntax, you *must* write this as:
if (1):
if (1):
echo "hello";
endif;
else:
endif;
> this works fine (no parse error):
> <?
> if (1):
> else:
> if (1) echo "hello";
> endif;
> ?>
This is because the "else" now matches the first "if (1):", which uses :-type
syntax, so the "else" is expected to have a ":" after it, which it does -- so no
parse error.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211