1

From tuxradar.com:

Example 1:

<?php
    if ($foo == $bar) {
        print "Lots of stuff here";
        print "Lots of stuff here";
        print "Lots of stuff here";
        ...[snip]...
        print "Lots of stuff here";
        print "Lots of stuff here";
    }
?>

Example 2:

<?php
    if ($foo == $bar) {
?>
    Lots of stuff here
    Lots of stuff here
    Lots of stuff here
    ...[snip]...
    Lots of stuff here
    Lots of stuff here
<?php
    }
?>

Assume $foo = $bar.

The output is equal on both of them. I don't understand why. Example 2 has no print/echo, as far as I understand that bunch of words should not be understood by the PHP parser without print or echo. So why does it actually get printed when that 'bunch of wards' is seperated by another set of <?php ?> tags, when it normally wouldn't?

I think I'm missing something here that I would like to understand to the core.

1 Answer 1

2

The reason that Example 2 outputs the text is because you have closed the PHP tag. The browser is rendering/interpreting it as plain text. You could also format the text output with HTML and it would render nicely in the browser.

Sign up to request clarification or add additional context in comments.

2 Comments

@frrlod: This is a very common practice and can lead to very nice looking code. If you need variables output in the plain HTML, consider doing so like this: <?= $variable ?>, or <?php echo $variable; ?>. It's all about readability.
@Kristy The server is interpreting it as plain text. The browser doesn't read PHP anyways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.