PHP in HTML
When building a complex page, at some point you will be faced with the need to combine PHP and HTML to achieve your needed results. At first point, this can seem complicated, since PHP and HTML are two separate languages, but this is not the case. PHP is designed to interact with HTML and PHP scriptscan be included in an HTML page without a problem.In an HTML page, PHP code is enclosed within special PHP tags. When a visitor opens the page, the server processes the PHP code and then sends the output (not the PHP code itself) to the visitor's browser. Actually it is quite simple to integrate HTML and PHP. A PHP script can be treated as an HTML page, with bits of PHP inserted here and there. Anything in a PHP script that is not contained within <?php ?> tags is ignored by the PHP compiler and passed directly to the web browser. If you look at the example below you can see what a full PHP script might look like:
Recommended usage:
<html>
<head></head>
<body class="page_bg">
Hello, today is <?php echo date('l, F jS, Y'); ?>.
</body>
</html>
The code above is simply HTML, with just a bit of PHP that prints out today's date using the built-in date function. As mentioned above, all of the plain HTML in the code above will be ignored by the PHP compiler and passed through to the web browser untouched. <head></head>
<body class="page_bg">
Hello, today is <?php echo date('l, F jS, Y'); ?>.
</body>
</html>
See how easy that is? Integrating PHP and HTML is really very simple. Just remember that at its core, a PHP script is just an HTML page with some PHP sprinkled through it. If you want, you can create a PHP script that only has HTML in it and no <?php ?> tags, and it will work just fine.
More advanced techniques:
<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
<?php } ?>
</ul>
</body>
</html>
and the result is:<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
<?php } ?>
</ul>
</body>
</html>
- Menu Item 1
- Menu Item 2
- Menu Item 3
- Menu Item 4
- Menu Item 5
PHP in HTML using short_open_tag
If you want to shorten your code as much as possible, you can go for the short_tags option. This will save you from typing <?php at the beginning of the code, shortening it to just <?. In order to enable this, you should update the php.ini file and turn the "short_tags" setting from "Off" to "On". While on most servers this setting is already turned on, it's always best to check beforehand. A problem that can occur if using short tags is a conflict with the XML usage. For XML, the <? syntax will start a processing function. To avoid this problem, the alternative <?= tag can be used.PHP in HTML using short_tags:
<html>
<head></head>
<body class="page_bg">
Hello, today is <?=date('l, F jS, Y'); ?>.
</body>
</html>
Have in mind that if you want to build a website compatible with as many platforms as possible, you should not rely on short_tags. <head></head>
<body class="page_bg">
Hello, today is <?=date('l, F jS, Y'); ?>.
</body>
</html>
0 Ads:
Post a Comment