PHP syntax

PHP syntax

PHP syntax:

The PHP script or code is executed on the server and returned to the normal HTML format in the browser.

Basic Syntax of PHP

PHP scripts can be placed anywhere in a document.

PHP scripts start with  <? php  and end with   ?> 

Example: 

  1. <?php 
  2.     // Write the PHP code here.
  3. ?>

PHP's default file extension is ".php".

HTML tags and some PHP scripting codes are commonly used in PHP files.

In the following example, we will look at an example of a simple PHP file, where a built-in function of PHP "echo" is used to take the output of "hello world" on a web page.

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         // My first PHP code
  12.         echo "hello world";
  13.     ?>
  14. </body>
  15. </html>

 

Output:

	hello world

enlightenedThe PHP statement ends with a semicolon (;).

 

PHP case-sensitive


That is, in PHP, lowercase letters and uppercase letters are considered separately.

Although PHP is case-sensitive, not all types of keywords (such as   if    ,   else   ,    for   ,   echo    etc   .), classes, functions, and user-defined functions are case-sensitive.

In the following example, all three echo statements are valid and equal

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         // My first PHP code
  12.         echo "hello world <br>";
  13.         ECHO "hello world <br>";
  14.         EcHo "hello world";
  15.     ?>
  16.     
  17. </body>
  18. </html>

 

Output:

hello world
hello world
hello world

However, all variables are case sensitive.

The following example shows only the value of the $ name variable. Because $ name, $ NAME and Name are three different variables.

Since the $ NAME and $ Name variables are not defined, an error will appear when we try to output them.

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.  
  11.     <?php 
  12.         // My first PHP code
  13.         $name = "Shuvo ";
  14.         echo "my name is " . $name . "<br>";
  15.         echo "my name is " . $NAME . "<br>";
  16.         echo "my name is " . $NaMe . "<br>";
  17.     ?>
  18.  
  19. </body>
  20. </html>

 

Output:

Notice: Undefined variable: NAME in D:\xampp\htdocs\onlylearn24\index.php on line 15
my name is

Notice: Undefined variable: NaMe in D:\xampp\htdocs\onlylearn24\index.php on line 16
my name is
my name is Shuvo

 


Use of comments in PHP:

A comment is used to explain PHP code and make it more readable.
Comments are also used in PHP code to prevent the execution. Its use is especially noticeable when testing an alternative code.

Use of comments:

yes The purpose of writing your code is to make it easy for others to understand.
yes To write code documentation.
yes To recall what you wanted to do with the code. Because most programmers come back to their previous projects after a long time. Then the text of the comment reminds him of what he thought of those codes.

Supports several types of comments in PHP:

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.  
  11.     <?php 
  12.         // A single line comment. This is also a single line comment. This comment method is used for writing documentation.
  13.         /* This is multiple
  14.         Line comments.
  15.         */
  16.         $name = "Shuvo "; // this is name
  17.         echo $name;
  18.     ?>
  19.  
  20. </body>
  21. </html>

 

Output:

Shuvo

 

Thank You.....