0

I was surprised to find that you can break out of a PHP function into raw HTML and back. I knew that you could do this sort of thing with loops and conditionals, but this was a surprise to me. Is it an accident or is this well-defined behavior? (I couldn't find any explicit discussion of the function case in the manual.)

[NOTE: The following code doesn't give a good example of when I would use this behavior, but I kept it simple for demonstration purposes.]

<?php

$i = 0;

while($i++ < 3) {
    ?><p>I am in a while loop.</p><?php
}

// this part surprised me
function actSkeptical($adjective) {
    ?><p>Is it <?= $adjective ?> that this works?.</p><?php
}

actSkeptical("weird");
?>

Output:

I am in a while loop.

I am in a while loop.

I am in a while loop.

Is it weird that this works?

I know some people absolutely hate mixing PHP and HTML like this, but I can't do OOP/templating (for reasons I won't go into here) and I do like seeing as much raw HTML as possible.

Also, I don't quite understand the semantics of how the short open/close tag above (outputting $adjective) works in conjunction with the surrounding code. Does PHP just treat raw HTML like it was an echo statement? And then the <?= $adjective ?> is just like including a variable within a string?

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • You're looking at a behavior of PHP. It isn't pretty, and I don't recommend coding it like that, but it is there to utilize if you deem it necessary. Basically, PHP doesn't care about anything outside of the `` tags and anything outside of PHP is sent to the browser. – Crackertastic Oct 17 '14 at 04:55
  • http://php.net/manual/en/language.basic-syntax.phpmode.php – Tieson T. Oct 17 '14 at 04:57
  • @Tieson Thanks but I've read that and unfortunately it makes no mention of functions. – Chris Middleton Oct 17 '14 at 05:01
  • see this:http://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php – Suchit kumar Oct 17 '14 at 05:17
  • @suchit I see a lot of people using the alternative syntax for while/if/etc when mixing HTML and PHP. Is there any reason for that other than thinking it's more readable? – Chris Middleton Oct 17 '14 at 06:21

2 Answers2

1

I can't seem to find any documentation relating to the exiting of PHP tags within blocks. However, there's really only a few places escaping to HTML will work.

  1. Normal Usage

    <?php
    
        php_related_code();
    
    ?>
    
    //html/css/js/etc
    
  2. Within blocks, such as while, for, functions, etc

    <?php
    
    for ($i = 0; $i < 5; $i++) {
        ?>
    
        hello world
    
        <?php
    }
    
    $i = 5;
    while ($i-- > 0) {
        ?> hello there <?php
    }
    
    function myFunc() {
    ?>
        hello universe
    <?php
    }
    
    myFunc();
    

You can think of ?>stuff<?php similar to an echo or print command found in PHP, because you can escape to HTML in the same places you can echo. So you can echo within the main script, in for loops, and you can echo in functions. But you can't echo in an array, for example:

<?php

$array = array(echo "here"); //not allowed
$array = array(?>here<?php); //also not allowed

So you can think of escaping the same as echoing in which it can tell you where you can use it, but you can't do the same thing when you're thinking about what it does.

They act differently and are processed by PHP differently as well. But your question is only asking about any restrictions so I won't go into what are the differences between ?><?php and echo.

I forgot to mention, <?=$variable?> is just short tag for <?php echo $variable; ?> if you have this feature enabled.

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • Thanks for the explanation. I guess I have to trust that they don't break any of the functionality in a future update (since it's partially undocumented behavior) but that's true of lots of things. – Chris Middleton Oct 17 '14 at 05:25
  • I would recommend creating a small template engine, basically one where it never escapes to HTML, you just have your PHP code, and your HTML, and your PHP will simply call the HTML via readfile/etc. As others have pointed out, switching between PHP and HTML creates unmaintainable code, so I hope that's another reason to add to your list as to why you shouldn't be doing this in your code. – Dave Chen Oct 17 '14 at 05:29
  • I mostly agree with you, but I'm not the primary developer of the code, so adding the layer of complexity/abstraction that a templating engine brings isn't an option. I'll try to keep the mixing to a minimum though. – Chris Middleton Oct 17 '14 at 06:04
0

This is called a spaghetti code. DO NOT USE IT.

If you really want to separate code, logic and markup start using MVC; YII is great.

http://www.yiiframework.com/

Also check this out: https://www.youtube.com/watch?v=mhwfFgSzg7U

Andrew
  • 7,619
  • 13
  • 63
  • 117
  • 1
    I appreciate the gentle "nudge", but as I stated in the question, I cannot use frameworks at this time for reasons that I won't detail here. This is not an answer. – Chris Middleton Oct 17 '14 at 04:56