This section contains the detail about the basic PHP syntax
Syntax of PHP
PHP syntax is the set of rules which need to be followed to to write properly structured PHP code.
Given below Some rules of PHP sytax
- The PHP script Block always start with <?php and ends with ?>.
- You can use html, text, sql queries with in a PHP file.
- You can place PHP script anywhere in the html page. Given below the sample code :
<html> <body> <?php echo "Hello World"; ?> </body> </html>
OR, This will also display the same output as above :
<?php echo "Hello World"; ?>
- As you can see in the above code, each PHP statement ends with semicolon.The semicolon is a separator and is used to distinguish one set of instructions from another.
- The above code must be save in a .php extension file.If it will save in a .html file, it will not execute properly.
- There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World".
- PHP ignore blank spaces just like HTML. For example, see the below code :
<html> <head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World!"; echo "Hello World!"; ?> </body> </html>
- In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. See the below code :
<html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>