PHP in HTML documents

If you looked at the previous page, you saw that all the information, including the html code, we printed out using the echo statement. However, PHP scripts can be written in other ways. Let”s consider an example:
<html>
<head>
<title> Example </title>
</head>
<body>

<? php
echo "Hi, I'm a PHP script!";
?>

</body>
</html>
Now you can save the given PHP script on the server as a .php file, for example start.php and check the result.
The html code will be correctly processed by the PHP interpreter, so you will not get errors when executing this script.
When PHP processes a file, it simply passes in its text until it encounters one of the special tags that tell it to start interpreting the text as PHP code. Then it executes all the code it finds, up to the closing tag telling the interpreter that it is just text again. This mechanism allows you to embed PHP code in HTML – everything outside of PHP tags remains unchanged, while inside it is interpreted as PHP code.
There are four sets of tags that can be used to denote PHP code. Of these, only two (<? Php…?> And <script language = “php”>… </script>) are always available; others can be enabled or disabled in the php.ini configuration file. While short and ASP style tags can be handy, they are not as portable as the long versions. Also, if you intend to embed PHP code in XML or XHTML to conform to XML, you should use the <? Php form. … .?>.
PHP Supported Tags:
1. <? Php echo ("if you want to work with XHTML or XML documents, do this \ n"); ?>

2. <? echo ("this is the simplest SGML processing instruction \ n"); ?>
<? = expression?> This is a synonym for "<? echo expression?>"

3. <script language = "php">
echo ("some editors (like FrontPage) do not
love processing instructions ");
</script>

4. <% echo ("You can choose to use ASP-style tags"); %>
<% = $ variable; # This is a synonym for "<% echo..." %>
The first way, <? Php. … .?> is most preferred because it allows PHP to be used in XML-compliant code such as XHTML.
The second method is not always available. Short tags are only available when enabled. This can be done using the short_tags () function (PHP 3 only), by enabling the short_open_tag setting in the PHP config file, or by compiling PHP with the –enable-short-tags option to configure. Even if it is enabled by default in php.ini-dist, using short tags is not recommended.
The fourth method is only available if ASP style tags have been enabled using the asp_tags config setting.
Note: ASP style tag support was added in PHP 3.0.4.
Note: You should avoid using short tags when developing applications or libraries that are intended to be distributed or hosted on PHP servers outside your control, as short tags may not be supported on the target server. To create portable, compatible code, do not use short tags.
The closing tag of a PHP code block includes a line feed immediately following it, if any. In addition, the closing tag automatically implies a semicolon; you don”t need to end the last line of code in a block with a semicolon. The closing PHP block tag at the end of the file is optional.
PHP allows you to use structures like this:
Professional insert:
<? php
if ($ expression) {
?>
<strong> This is true. </strong>
<? php
} else {
?>
<strong> This is a lie. </strong>
<? php
}
?>
This code works as expected, because when PHP encounters the closing tags?>, It just prints whatever it finds until the next opening tag. The above example is more efficient for outputting large blocks of text than sending all of the text via echo (), print () or something similar.

Leave a Reply

Your email address will not be published. Required fields are marked *