Page 1 of 1

Flow Control Spanning Multiple PHP Tags : "Technical Term"

Posted: Sun Aug 14, 2011 3:14 am
by louis_carole
Dear All -

Consider the following HTML/PHP mix:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">  
<TITLE> A PHP-HTML-PHP Control Sandwich </TITLE>  

<?php $makes_me_feel = "blue" ; ?>

<html>
	<body>

<?php if( $makes_me_feel == "blue" ) { ?>

		<a href="http://www.google.com/search?q=pantone+292" >
			Pantone 292
		</a>

<?php } ?>

	</body>
</html>
Say the code above is the contents of a file [text]iota.php[/text] and I point my browser to it, and I see this:
Pantone 292

This seems to be fine PHP coding. I see it in common use, and found some sites about it in my searchings. People rely on this type of PHP-controlled HTML block technique working, and get bent out of shape when their favorite IDE paints it otherwise.

But what is the technical term for the programming concept demonstrated here, whereby a control statement in a PHP program can exist over two separate tags?
I'm asking so I know better what search terms to use to look up more specific documentation on this construction.
As either an irrelevant side-note or a slam-dunk proof-of-something-ness (I'm not sure which), note that

Code: Select all

<? if(expression) { ?> HTML-block <? } else { ?><? } ?>
works OK, but, moving the first closing bracket over into the first tag,

Code: Select all

<? if(expression) { } ?> HTML-block <? else { ?><? } ?>
generates an error.

- Louis

PS

Code: Select all

<? if(expression) { expression-list ?> HTML-block <? expression-list } ?>
seems to work OK.

Re: Flow Control Spanning Multiple PHP Tags : "Technical Ter

Posted: Sun Aug 14, 2011 1:26 pm
by McInfo
I don't know that it is known by an official term, but it is covered in the Basic Syntax section of the manual.

Re: Flow Control Spanning Multiple PHP Tags : "Technical Ter

Posted: Sun Aug 14, 2011 4:46 pm
by louis_carole
Thanks, McInfo! I've seen it covered in a few other 'basics' places as "See, you can do it this way, too. Neat, huh?"

I'd like to know, well, why I am allowed to do it that way, and how it can and cannot be generalized. The answer to the original burning question of mine, "What do the developers call this?" doesn't pop out at me on that page.

I'll start calling it the "HTML escape/descape" technique. What is the opposite of 'escape'? Last night I was thinking of it as 'persistence of flow' as an analog for 'persistence of state'.
So far, I figured out:
  • You can't escape/descape in the middle of an expression:

    Code: Select all

    <? print 1 ?><? + 1 ; ?>
    causes an error.
  • You can escape/descape between two expressions ; that's how people normally use it:

    Code: Select all

    <? print "some" ; ?><? print "thing" ; ?>
    prints "something" or "some thing" or maybe some other white-spaced variant depending on the use of white-space between the two tags. Any visible HTML elements and other script output that comes between the two tags shows up sandwiched between "some" and "thing". This explains virtually all uses of PHP tags besides the flow
  • You can escape/descape right after clause-openers and right before clause-closers, but not the other way around.

    Code: Select all

    <?php $cow = array("occupation" => "astronaut") ; ?>
    
    <?php if( $cow["occupation"] == "astronaut" ) { print "Look!" ; ?>
    	The cow jumps over the text block.
    <?php print "And she makes it look easy too." ; } else ?>
    	( I bet It's all the fiber she eats :P )
    <?php { print "OMG MT, Bessie!  I'm so sorry!" ; } ?>
    
    prints "Look! The cow jumps over the text block. And she makes it look easy too. ( I bet It's all the fiber she eats :P ) OMG MT, Bessie! I'm so sorry! "
    But, moving the 'else' token to the beginning of the last php tag causes an error:

    Code: Select all

    <?php $cow = array("occupation" => "astronaut") ; ?>
    
    <?php if( $cow["occupation"] == "astronaut" ) { print "Look!" ; ?>
    	The cow jumps over the text block.
    <?php print "And she makes it look easy too." ; } ?>
    	( Bessie's a show-off, ain't she? )
    <?php else { print "You'll never see that, Bessie!" ; } ?>
    
    Indeed Bessie will only see a blank page.
I've seen it said that the PHP parser "ignores" everything outside its tags, and plays the simple role of a command substitution doohickey for HTML.
That explanation has helped me sleep at night for most every other way and place I've seen the PHP tags used. Is there a way to look at this block d/escape trick as just another version of the same thing?

- Louis

Re: Flow Control Spanning Multiple PHP Tags : "Technical Ter

Posted: Mon Aug 15, 2011 11:57 am
by louis_carole
PS

Functions can also handle d/escaping:

Code: Select all

<? function f() { print "sha" ; ?><? print "zaam" ; } ?>
<? f() ; ?> <br> <? f() ; ?>
prints "shazaam shazaam" with a line break. No error. Can't put the d/escape between the function name and the opening bracket, though.