English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Syntax

PHP scripts can be embedded in HTML web pages.

Standard PHP syntax

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

PHP delimiters <?php (?> as shown in the example) simply tell the PHP engine to treat the enclosed code block as PHP code, not as simple HTML.

<?php
// Some code to execute
echo 'Hello, world!';
?>
Test and see‹/›

Each PHP statement ends with a semicolon (;) - Tells the PHP engine that the current statement has ended.

Embedding PHP in HTML

PHP files are plain text files with the .html extension. In PHP files, you can write HTML as you would in a regular HTML page, and you can also embed PHP code to be executed on the server side.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"
    <title>A simple PHP file</title>
</head>
    <h1><?php echo "Hello, world!"; ?>/h1>
</html>
Test and see‹/›

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

What happens here? When you run this code, the PHP engine will execute the instructions between <?php … ?> tags and retain the rest. Finally, the web server will send the final output back to your browser in HTML format.

PHP Comments

Comments are simple text ignored by the PHP engine. The purpose of comments is to make the code more readable. It can help other developers (or you when editing the source code) understand what you are trying to do with PHP.

PHP supports single-line and multi-line comments. To write a single-line comment, start with two backslashes (//)or hash (#) at the beginning. For example:

<?php
//This is a single-line comment
#This is a single-line comment
echo 'Hello, world!';
?>
Test and see‹/›

However, to write multi-line comments, add a slash before the comment, and then add a star (/*),then add a star before the comment, and then add a slash (*/),as shown below:

<?php
/*
This is a multi-line comment block.
It spans more than.
One line
*/
echo 'Hello, world!';
?>
Test and see‹/›

Case sensitivity in PHP

Variable names in PHP are case-sensitive. As a result, the variables $color, $Color, and $COLOR are considered three different variables.

<?php
// Assign a value to a variable
$color = 'blue';
 
//Attempt to print the 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>';
?>
Test and see‹/›

If you try to run the example above, it will only display the value of the variable $color, and produce 'undefined variable' warnings for the variables $Color and $COLOR.

However, the names of keywords, functions, and classes are not case-sensitive. The result of calling gettype() or GETTYPE() will produce the same result.

<?php
//Assign a value to a variable
$color = 'blue';
 
// Get the type of a variable
echo 'gettype($color) . '<br>';
echo 'GETTYPE($color) . '<br>';
?>
Test and see‹/›
If you try to run the example code above, both gettype() and gettype() functions will produce the same output, that is: string.