PHP is server side scripting language. So, it is execute on the server, and the plain HTML result is sent back to the browser.
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>
<?php //PHP code goes here ?>
The default file extension for PHP files is
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file structure, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello PHP!" on a web page:
<!DOCTYPE html> <html> <head> <title>Hello PHP</title> </head> <body> <?php echo "Hello PHP!"; ?> </body> </html>
PHP statements are terminated by semicolon (;).
The closing tag of a block of PHP code also automatically implies a semicolon (so you do not have to have a semicolon terminating the last line of a PHP block).
The PHP code in the preceding example began with
<?php and ended with
This is similar to all HTML tags because they all begin with a less than (
<) symbol and end with a greater than (
These symbols (
?>) are called PHP tags. They tell the web server where the PHP code starts and finishes.
Any text between the tags is interpreted as PHP. Any text outside these tags is treated as normal HTML.
The PHP tags allow you to escape from HTML.
There are actually four different styles of PHP tags. Each of the following fragments of code is equivalent:
<?php echo '<p>Tutorialink.com</p>'; ?>
This is the tag style that we use in this site; it is the preferred PHP tag style.
The server administrator cannot turn it off, so you can guarantee it will be available on all servers, which is especially important if you are writing applications that may be used on different installations.
This tag style can be used with Extensible Markup Language (XML) documents.
In general, we recommend you use this tag style.
<? echo '<p>Tutorialink.com</p>'; ?>
This tag style is the simplest and follows the style of a Standard Generalized Markup Language (SGML) processing instruction.
To use this type of tag-which is the shortest to type-you either need to enable the
short_open_tag setting in
php.ini config file or compile PHP with short tags enabled.
The use of this style is not recommended because it will not work in many environments as it is no longer enabled by default.
<script language='php'> echo '<p>Tutorialink.com</p>'; </script>
You might use it if you're using an HTML editor that gives you problems with the other tag styles.
<% echo '<p>Tutorialink.com</p>'; %>
This tag style is the same as used in Active Server Pages (ASP) or ASP.NET.
You can use it if you have enabled the asp_tags configuration setting. You probably have no reason to use this style of tag unless you are using an editor that is geared toward ASP or ASP.NET.
By default, this tag style is disabled.
You tell the PHP interpreter what to do by including PHP statements between your opening and closing tags.
The preceding example used only one type of statement:
As you have probably guessed, using the echo construct has a very simple result: It prints (or echoes) the string passed to it to the browser.
Comments are exactly that: Comments in code act as notes to people reading the code.
Comments can be used to explain the purpose of the script, who wrote it, why they wrote it the way they did, when it was last modified, and so on.
The PHP interpreter ignores any text in comments. Essentially, the PHP parser skips over the comments, making them equivalent to whitespace.
PHP supports three ways of commenting: C, C++, and shell script-style comments.
<!DOCTYPE html> <html> <head> <title>Comments</title> </head> <body> <?php // This is a single line C, C++ type comment /* This is a multiple lines C, C++ type comment block, which is more than one line */ # This is also a single line shell script style comment ?> </body> </html>