PHP Syntax

In this tutorial, you will get a basic idea about PHP syntax with the help of examples.


PHP Syntax represents the way PHP is written in the script. Like every other language PHP also has its own syntax and rules.

PHP Syntax

All PHP code goes between PHP tags. It generally starts with <?php and ends with ?> and the file containing the tags should be saved with .php extension.

<?php
//your code here
?>

PHP Example

Now let’s see a simple example of PHP where we are going to write some text using the echo command.

<!DOCTYPE> 
<html> 
<body> 
<?php 
echo "<h3>Welcome to the World of PHP.</h3>"; 
?> 
</body> 
</html>

In the above example, we have used the echo statement which is used to print the given string. It is mandatory to close the double quotes here.

Every PHP statement is terminated with the semicolon(;) because by this PHP engine is get to know that script has ended.

Output

PHP Output

PHP Case Sensitivity

In PHP, all the keywords i.e. (echo, print, if, else), classes, functions, user-defined functions are not case sensitive. But variables in PHP are case sensitive.

In the below example, you can see that all the echo statements are valid and the same.

<!DOCTYPE> 
<html> 
    <body> 
         <?php 
         echo "Welcome to the World of PHP using echo </br>"; 
         EcHo "Welcome to the World of PHP using EcHo </br>"; 
         ECHO "Welcome to the World of PHP using ECHO </br>"; 
        ?> 
     </body> 
</html>

Output

PHP Output

Variable names in PHP are case-sensitive. So, $color, $ColoR, and $COLOR will be treated as three different variables. Consider the below example to understand the same.

<?php
// Assign value to variable
$color = "Black";

// Print variable value
echo "My Laptop color is " . $color . "<br>";
echo "My Laptop color is " . $ColoR . "<br>";
echo "My Laptop color is " . $COLOR . "<br>";
?>

Output

PHP Output

In the above example, the only variable $color has printed the expected output on the screen. Other two i.e. $ColoR and $COLOR has given errors on lines 7 and 8.

In the next tutorial, we are going to learn how to run PHP programs in XAMPP. But before that we recommend you check our tutorial on How to install XAMPP server in Windows.

Please get connected & share!

Advertisement