PHP Syntax rule

Updated on ... 10th February 2023

Every programming language has some syntax rule. PHP has also some syntaxt that can surprise.

In this article, we will discuss about below-mentioned point

  • What is PHP?
  • PHP code
  • Case sensitivity
  • Statements
  • Whitespace & line breaks
Tree directory structure  script With PHP

What is PHP?

Simply PHP is a server-side scripting language that is used for web development. so that PHP Syntax is not visible in the browser as output because it's a server-side scripting language.

Earlier PHP stands for  Home Page. now, it stands for Hypertext Preprocessor.

PHP code

With HTML, We need to have the opening tag to start the PHP code and the closing tag to close the PHP code.

                                                
                                                
                                                <?php

// php code

?>
                                                
                                            

However, if we have a file containing only PHP code, the PHP  closing tag is optional.

for example:

                                                
                                                
                                                <?php
echo 'Hello world';

    //.......
                                                
                                            

If we have a file with PHP and HTML mix code, We need to have the enclosing tag (?>).

For example:

                                                
                                                
                                                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Syntax rule - bootstrapfriendly</title>
</head>
<body>
        <h1><?php echo 'Hello World'; ?></h1>
</body>
</html>
                                                
                                            

Case sensitivity

Surprisingly PHP is a partially case-sensitive language. Let's understand what is case sensitive and what is not is very important and interesting to avoid syntax errors.

The following are case-insensitive in PHP:

  • PHP constructs such as echo, if, if-else, if-elseif, switch, while, do-while, etc.
  • Keywords such as true and false.
  • User-defined function & class names (But we should avoid using function name and class name case-insensitive for example we have a function count() so we can use it as count() NOT COUNT() or Count() However count(); COUNT(); or Count(); will work fine).

On the other hand, variables are case-sensitive. e.g., $message, $Message and $MESSAGE are different variables.

Statements

A PHP script typically contains one or more statements. A statement is a code of block that does something, e.g., defining a variable and assigning some value to it, and calling a function.

A PHP statement always ends with a semicolon (;).

For example

                                                
                                                
                                                <?php

$message = "Hello world";
echo $message;
                                                
                                            

We don’t need to place the semicolon after the curly brace (}).

We don’t need to place the semicolon to the last statement in a PHP block because the PHP closing tag (?>) automatically implies (;). However, using a semicolon for the last statement in a block should work fine, and it's a good practice.

For example:

                                                
                                                
                                                <?php
$message = "Hello world";
echo $message;
?>
<?php
echo  $message
?>
                                                
                                            

Leave a comment

  • ...
    Arbaj Alam

    That's good uncle for everyone Thank u😊