Syntax

PHP is a server-side scripting language, which means that PHP scripts are executed on the server and the result is sent to the client as plain HTML.

PHP Tags

PHP scripts are always enclosed within the PHP opening and closing tags. The default syntax for these tags is as follows:

<?php
// Your PHP code goes here
?>

Everything outside these tags is considered plain HTML (or other types of code, like JavaScript or CSS), and everything inside these tags is treated as PHP code.

Comments

Comments are a useful way to leave notes for yourself and others in the code. PHP supports single-line and multi-line comments:

<?php
// This is a single-line comment

/*
This is a multi-line comment
It spans multiple lines
*/
?>

Echo and Print Statements

The echo and print statements are used in PHP to output data to the screen. Both can be used interchangeably in most cases, but echo is slightly faster:

<?php
echo "Hello, Humans!";
print "Hello, Humans!";
?>

Semicolons

In PHP, semicolons (;) are used to denote the end of a statement, similar to languages like C, Java, and JavaScript:

<?php
echo "Hello, Humans!"; // Note the semicolon at the end
?>

Variables

Variables in PHP start with a $ sign, followed by the name of the variable:

<?php
$name = "JMartian";
echo $name; // Outputs: Martian
?>

Case Sensitivity

In PHP, all keywords (like if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive. However, all variable names are case-sensitive.

Last updated