Basic PHP syntax
Following is a basic PHP syntax:
1 2 3 4 5 |
<?php // php code ?> |
A PHP code is enclosed within .
It always starts with <?php and ends with ?>.
<?php is called a opening tag and ?> is called closing tag.
<?php ?> tells PHP to start and stop interpreting the code between them.
All statements within code must end with semicolon ( ; ).
Otherwise, errors will be generated.
PHP Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <title>Example of PHP</title> </head> <body> <h1>PHP Heading</h1> <?php echo "Hello World"; ?> </body> </html> |
Output:
1 |
Hello World |
In this example we use HTML tags and PHP code.
Save this file with .PHP extension.
So to understand above code you should know HTML tags.
echo is used to print the outputs on screen in PHP.
PHP was designed to work with HTML and it can be embedded into the HTML code.
1 2 3 4 5 |
<html> <? php //PHP code ?> </html> |
But also you can create PHP files without any html tags and that is called pure PHP file.
Also note that if you are embedding PHP within XML, XHTML you need to use the <?php ?> tags.