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:
- <?php
- // Write the PHP code here.
- ?>
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:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- // My first PHP code
- echo "hello world";
- ?>
- </body>
- </html>
Output:
hello world
The 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:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- // My first PHP code
- echo "hello world <br>";
- ECHO "hello world <br>";
- EcHo "hello world";
- ?>
- </body>
- </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:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- // My first PHP code
- $name = "Shuvo ";
- echo "my name is " . $name . "<br>";
- echo "my name is " . $NAME . "<br>";
- echo "my name is " . $NaMe . "<br>";
- ?>
- </body>
- </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:
The purpose of writing your code is to make it easy for others to understand.
To write code documentation.
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:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- // A single line comment. This is also a single line comment. This comment method is used for writing documentation.
- /* This is multiple
- Line comments.
- */
- $name = "Shuvo "; // this is name
- echo $name;
- ?>
- </body>
- </html>
Output:
Shuvo
shelleyakter
nice post