PHP Syntax

Standard PHP Syntax

A PHP script begins with the <?php tag and ends with the ?> tag.

The PHP delimiters <?php and ?> in the following example instruct the PHP engine to interpret the enclosed code block as PHP code instead of plain HTML.

<?php
// Some code to be executed
echo "Hello, world!";
?>

Every PHP statement ends with a semicolon (;) — this indicates to the PHP engine that the end of the current statement has been reached.


Embedding PHP within HTML

PHP files are plain text files with a .php extension. Inside a PHP file, you can write HTML just like you do in regular HTML pages and also embed PHP code for server-side execution.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A Simple PHP File</title>
</head>
<body>
<h1><?php echo "Hello, world!"; ?></h1>
</body>
</html>

The example above shows how you can embed PHP code within HTML to create well-formed dynamic web pages. If you view the source code of the resulting web page in your browser, the only difference you will see is the PHP code <?php echo "Hello, world!"; ?> has been replaced with the output "Hello, world!".

What happened here? When you run this code, the PHP engine executes the instructions between the <?php … ?> tags and leaves the rest as it is. Finally, the web server sends the final output back to your browser, which is entirely in HTML.


PHP Comments

A comment is simply text that is ignored by the PHP engine. Comments are used to make the code more readable. They can help other developers (or you in the future when you edit the source code) understand what you were trying to do with the PHP code.

PHP supports single-line as well as multi-line comments. To write a single-line comment, start the line with either two slashes (//) or a hash symbol (#). For example:

<?php
// This is a single line comment
# This is also a single line comment
echo "Hello, world!";
?>

However, for multi-line comments, begin the comment with a slash followed by an asterisk (/*) and conclude it with an asterisk followed by a slash (*/), as demonstrated below:

<?php
/*
This is a multiple line comment block
that spans across more than
one line
*/
echo "Hello, world!";
?>

Case Sensitivity in PHP

Variable names in PHP are case-sensitive. Therefore, the variables $color, $Color, and $COLOR are considered as three distinct variables.

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

// Try to print variable value
echo "The color of the sky is " . $color . "<br>";
echo "The color of the sky is " . $Color . "<br>";
echo "The color of the sky is " . $COLOR . "<br>";
?>

If you attempt to execute the above example code, it will only display the value of the variable $color and generate an "Undefined variable" warning for the variables $Color and $COLOR.

However, keywords, function names, and class names are case-insensitive. Therefore, calling gettype() or GETTYPE() will yield the same result.

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

// Get the type of a variable
echo gettype($color) . "<br>";
echo GETTYPE($color) . "<br>";
?>

When you execute the above example code, both the functions gettype() and GETTYPE() will produce the same output: string.