Can an HTML code be put inside a PHP code

image

The sample usually is that PHP code should be put inside an HTML tag… can it be done the
other way around like

<?php

echo $_POST['first_name'];
//LIKE THE HTML CODE CAN BE PUT INSIDE THE PHP CODE?
?>

Can you output html using php? Yes -

echo "<p>Your last name is: {$_POST['last_name']}</p>";

Most programmers use a majority rule to decide which way to write code. If most of what you are doing in a block is html, use code like in your picture. If a majority of what you are doing is php logic, echo the html using php code.

BTW - everything inside a .php file IS php code. The things that are outside of <?php ?> tags are considered inline html and when php parses and tokenizes the content of the .php file, the inline html receives a token named - T_INLINE_HTML, with the token value being the literal inline html, such as the <p>Your last name is: in your picture.

2 Likes

thanks for the insights… what is meant by a token by the way?
also, why did you have curly brackets inside the p tag?

you can also write , like It should be -

echo "<p>Your last name is:".$_POST['last_name']."</p>";
1 Like

Both of these approaches work just fine. The curly brackets, when used in a double quoted string, enclose a PHP expression that is resolved then placed in the string.

1 Like

And as a fun fact, the tokenized byte-code that php produces for both is exactly the same.

2 Likes
Sponsor our Newsletter | Privacy Policy | Terms of Service