On 06 January 2004 03:58, Tim L wrote:

> I'm not sure if this is intended behaviour or not, but I can
> see that is
> might be.  Just thought I would bounce this to see what
> people think about
> it:
> 
> <?php
>  if (false)
> ?> Hello<?php
>  else
>   echo "Hi";
> 
>  echo " World\n";
> ?> 
> 
> This returns a parse error due to the fact there is no
> brackets around the
> first part of the if statement.

Yes, but that's only part of the story.  What you seem to be neglecting here
is that the closing ?> tag also implies a semicolon, so the above is
equivalent to:

  <?php
   if (false) ;
      echo "Hello"
   else
      echo "Hi";
 
   echo " World\n";
  ?> 

and I'm sure you can see why that is both syntactically and semantically
invalid.

It's because of this implied semicolon that any conditional or loop in which
you break out of PHP must be enclosed in braces, or use the colon syntax.
The following are valid ways of doing what you want:

  <?php
   if (false) {
  ?> Hello<?php
   }
   else
      echo "Hi";
 
   echo " World\n";
  ?> 

   if (false):
  ?> Hello <?php
   else:
  ?> Hi <?php
   endif;
 
   echo " World\n";
  ?>

>  It seems that because the
> next line after
> the if is not php, then it terminates the if at the closing
> of the php tag.

As I'm sure you can see now, this is becaue of the implied semicolon.
Possibly the easiest way to work out what's going on when you break in and
out of PHP is just to remember that any time you have a fragment like:

   ?> something <?php

This is treated like

  ; echo "something"

(Notice the semicolon at the beginning, and the lack of one at the end!)

> 
> But if you do this:
> 
> <?php
>  if (false)
> ?> Hello<?php
> ?> 
> 
> or
> 
> <?php
>  if (true)
> ?> Hello<?php
> ?> 

These are syntactically valid because the closing ?> tags imply the
necessary semicolons, but semantically wrong because the first one
terminates the if statement -- so the Hello will be output unconditionally
(and a following else would be syntactically invalid again!).

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: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to