PHP Conditional Code Following Closing Tag

I am a bit confused about how the code below works. In my head, I imagine how every php block is executed as a whole and passed in HTML. The fact that the first block is incomplete with a hanging bracket doesn’t work well with how I can imagine PHP to work. What does a PHP module do when it hits the PHP close tag? How this code inside PHP tags can affect the clear text output outside of PHP tags, i.e. Only conditionally displays the form?

I would think that to achieve below you really would need to use an echo expression to conditionally respond to the form.

<html> <head></head> <body> <?php /* if the "submit" variable does not exist, the form has not been submitted - display initial page */ if (!isset($_POST['submit'])) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Enter your age: <input name="age" size="2"> <input type="submit" name="submit" value="Go"> </form> <?php } else { /* if the "submit" variable exists, the form has been submitted - look for and process form data */ // display result $age = $_POST['age']; if ($age >= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } else { echo 'You're too young for this club, come back when you're a little older'; } } ?> </body> </html> 
+6
php
source share
6 answers

the PHP manual explains this pretty decently:

... when PHP falls into the closing tags ?> , it just starts outputting everything that it finds (except for the next line of the new line - see the separation of commands) until it falls into another opening tag ... but to output large blocks of text, output from PHP parsing mode, as a rule, is more efficient than sending all text through echo() or print() ...

+4
source share

Parts that are outside of php tags are treated as literals that are displayed in this part of the program stream.

+3
source share

Think of it in reverse order. The whole document is PHP, with implicit? > at the beginning and implicit <? in the end. Then you get these equivalences:

 ?>HTML TAGS<? 

becomes equivalent

 echo 'HTML TAGS'; 

In other words, every inverted pair of open / close parentheses in PHP encapsulates an echo statement.

+3
source share

If the condition fails, it skips the braces inside the php block to the end. Everything else is considered a literal that goes directly to the page (unlike the code in question).

+2
source share

PHP developers often encounter the problem of sending headers and cookies after sending the output to the browser, but this problem can also occur when an unintentional exit occurs. If a space is inserted after the end of the PHP code, this may lead to inadvertent output when this PHP script is included.

Source: http://phpstarter.net/2009/01/omit-the-php-closing-tag/

0
source share

+1 Reply Bernard.

You can make this look less strange / broken by using template-level control structures, for example, well-formed tags, for example:

 <?php if (!isset($_POST['submit'])) { ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> <label for="age">Enter your age:</label> <input name="age" id="age"> <input type="submit" name="submit" value="Go"> </form> <?php } else { ?> <?php // Read submitted age // $age= intval($_POST['age']); ?> <?php if ($age >= 21) { ?> Come on in, we have alcohol and music awaiting you! <?php } else { ?> You're too young for this club, come back when you're a little older. <?php } ?> <?php } ?> 

Pay attention to htmlspecialchars around $_SERVER['PHP_SELF']; - it was a through hole for scripts in the sample code. Plus there was an obvious problem with the apostrophes in the latest echo .

0
source share

All Articles