Jump to content

Newbie question - why does this example from "PHP Programming" parse?


doug

Recommended Posts

This example in the O'Reilly "PHP Programming" book shows the use of an alternate block if syntax:

<?if($user_validated):?>   <table>     <tr>       <td>First Name:</td><td>Sophia</td>     </tr>     <tr>       <td>Last Name:</td><td>Lee</td>     </tr>   </table> <?else:?>   Please log in. <?endif?> 

I understand the code, but I don't understand why it works. The if, else and endif statements are all "out there by themselves" outside of the HTML. Why does one section of the HTML get sent by the server if true and the other part if false? Since ALL the HTML is outside the PHP scripting brackets, why doesn't ALL the HTML get sent by the server?Thanks,doug

Link to comment
Share on other sites

Even things outside PHP blocks are still affected by conditionals. Thats just the way they made it. And be happy, it makes writing long code blocks that are subject to conditionals much easier :) . So this works.

<?php	$var = true;	if ($var) {		?>$var is true<?php	} else {		?>$var is false<?php	}?>

As for the colon notation, that is an alternative syntax instead of using curly braces (think switch()).

Link to comment
Share on other sites

Even things outside PHP blocks are still affected by conditionals. Thats just the way they made it. And be happy, it makes writing long code blocks that are subject to conditionals much easier :) . So this works.
<?php	$var = true;	if ($var) {		?>$var is true<?php	} else {		?>$var is false<?php	}?>

As for the colon notation, that is an alternative syntax instead of using curly braces (think switch()).

I guess I'm semi-happy that it "works" that way. It seems counter-intuitive though. Maybe not. Certainly in a client-side sense something like that happening with client-side JavaScript would be way bizarro. But on the server-side I suppose the parser can do what it likes.But rather than "stopping and starting" the PHP blocks, it seems it would have been neater to just have some sort of "literal" syntax. For example, in your example, it would be easier to write and read if it was something like:
<?phpif ($var) {<literal>$var is true</literal>}else {<literal>$var is false</literal>}?>

At least you would know you were still inside PHP while reading through the code.But it is an interesting syntax thing to be aware of. Thanks.doug

Link to comment
Share on other sites

But rather than "stopping and starting" the PHP blocks, it seems it would have been neater to just have some sort of "literal" syntax.
There is actually something like what you are thinking of - its called HEREDOC syntax. Its not quite literal though, as it still interprets variables.http://au2.php.net/manual/en/language.type....syntax.heredoc
Link to comment
Share on other sites

PHP syntax is counterintuitive sometimes, and this is one of those times. The way I wrapped my head around it was to realize that there is only one PHP script in your document. A section of php enclosed within php tags is not a separate script. It's a part of the whole script that gets executed at a particular place. So when a curly brace (a colon, in alt syntax) is opened within one set of php tags and closed within the next set of php tags, the parser is totally cool with that.I think this is specifically an innovation over Perl, whose limitations led to the development of PHP. In Perl, you either use a ton of print statements or heredoc syntax to generate your html. That makes conditionals very awkward sometimes. In fact, here's the big difference between Perl CGI (a traditional language) and php: A Perl document is a script with print statements embedded in it. A PHP document is (not always, but typically) an HTML document with scripting statements embedded in it. Very different perspectives that lead to different ways of doing things.Like, every now and then someone will show up on the forum with a PHP script and ALL the html is in echo statements. How icky is that?

Link to comment
Share on other sites

PHP syntax is counterintuitive sometimes, and this is one of those times. The way I wrapped my head around it was to realize that there is only one PHP script in your document. A section of php enclosed within php tags is not a separate script. It's a part of the whole script that gets executed at a particular place. So when a curly brace (a colon, in alt syntax) is opened within one set of php tags and closed within the next set of php tags, the parser is totally cool with that.I think this is specifically an innovation over Perl, whose limitations led to the development of PHP. In Perl, you either use a ton of print statements or heredoc syntax to generate your html. That makes conditionals very awkward sometimes. In fact, here's the big difference between Perl CGI (a traditional language) and php: A Perl document is a script with print statements embedded in it. A PHP document is (not always, but typically) an HTML document with scripting statements embedded in it. Very different perspectives that lead to different ways of doing things.Like, every now and then someone will show up on the forum with a PHP script and ALL the html is in echo statements. How icky is that?
Thanks. That sounds like a good way of thinking about it.doug
Link to comment
Share on other sites

I saw this example in the PHP book which also struck me as being sort of awkward syntax:

<? function column() { ?> </td><td> <? } ?>

It seems that rewriting this as follows is easier to read, don't you think?

<? function column() {  echo '</td><td>';?>

I guess if there are MANY lines of HTML in the function that the first format might be easier in that case.doug

Link to comment
Share on other sites

I have totally flipping lost my book just within the week, BUT -- I suspect the column example is mostly to illustrate how you can use that syntax. It's really the same idea as what you pointed out before. For just 2 lines, yeah, echo looks more straightforward. As you suspect, it makes the most sense when you've got a whole bunch. In any case, we are talking about a tool for very dynamic page generation here, where lots of content depends on user input or the weather or what have you.

Link to comment
Share on other sites

You will learn to love the ability to write plain HTML that is conditional on PHP. These examples are all very simple and when you look at them the echo statments don't seem to make the code any different, but imagine on a larger scale.What if you have an entire huge section of page that is only set to display if a PHP variable is true? would you want echo, echo, echo, echo, echo, echo, echo, echo... for a bazillion lines? Or would you rather just have<? if(something){ ?><table><tr><td><font><whatever><blah>....etc<? } ?>

Link to comment
Share on other sites

Here's another example:

<?phpif (isset($error_message)){?><div class="error"><?php echo $error_message; ?></div><?php}else{?><div class="message">Thank you for logging in (or whatever it is that you do).</div><?php}?>

That whole thing is equivalent to these:

<?phpif (isset($error_message)){  echo '<div class="error">';  echo $error_message;  echo '</div>';}else{  echo '<div class="message">';  echo 'Thank you for logging in (or whatever it is that you do).';  echo '</div>';}?>

<?phpif (isset($error_message)){  echo <<<EOT<div class="error">{$error_message}</div>EOT;}else{  echo <<<EOT<div class="message">Thank you for logging in (or whatever it is that you do).</div>EOT;}?>

All of those will do exactly the same thing, it's just your preference which style either makes the most sense to you or is the easiest to maintain. I'll tell you right now that a bunch of echo statements is not the easiest thing to maintain.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...