Basic PHP syntax, including case sensitivity, statements, and whitespaces.
As a programming language, PHP has a set of rules that governs how you write programs.
Start PHP
<?php
Enclosing tag
?>
Example
<html lang="en"> <head> <meta charset="UTF-8"> <title>PHP Syntax</title> </head> <body> <h1>echo 'PHP Syntax'; </h1> </body> </html>
PHP code, the enclosing tag is optional
echo 'PHP Syntax';
PHP is partially case-sensitive. Knowing what is case sensitive and what is not is crucial to avoid syntax errors.
If you have a function such as, you can use it as COUNT
. It would work properly.
The following are case-insensitive in PHP:
- PHP constructs are if, if-else, if-else, switch, while, do-while, etc.
- Keywords such as
true
andfalse
. - User-defined function & class names.
On the other hand, variables are case-sensitive. e.g., $message
and $MESSAGE
are different variables.
A PHP script typically consists of one or more statements. A statement is a code that does something, e.g., assigning a value to a variable and calling a function.
A statement always ends with a semicolon (;
). The following shows a statement that assigns a literal string to the $message
variable:
$message = "Hello";
PHP also has a compound statement that consists of one or more simple statements.
if( $is_new_user ) {
send_welcome_email();
}
The closing tag of a PHP block (?>
) automatically implies a semicolon (;
). Therefore, you don’t need to place a semicolon in the last statement in a PHP block. For example:
<?php echo $name ?>
The statement echo $name
doesn’t need a semicolon. However, using a semicolon for the last statement in a block should work fine. For example:
<?php echo $name; ?>
Whitespace & line breaks
In most cases, whitespace and line breaks don’t have special meaning in PHP. Therefore, you can place a statement in one line or span it across multiple lines.
login( $username, $password );
login(
$username,
$password
);
o";