W3docs

Understanding PHP Syntax: A Guide for Web Developers

PHP, which stands for Hypertext Preprocessor, is a server-side scripting language used for creating dynamic web pages. PHP syntax is crucial to understanding

PHP — which stands for PHP: Hypertext Preprocessor — is a server-side scripting language used to build dynamic web pages. Server-side means the code runs on the web server and only its output (usually HTML) is sent to the browser, so visitors never see the source. Getting the syntax right is the foundation for writing PHP that actually runs, because PHP, like most languages, is strict about how code is structured.

This chapter walks through the rules you meet on day one: where PHP code goes, how statements end, how to comment, and how variables, data types, operators, and functions are written.

PHP Tags: Where Code Lives

Every piece of PHP must sit between PHP tags. The server only treats text inside these tags as code; everything outside is sent to the browser untouched.

<?php
// PHP code goes here
echo "Hello from the server!";
?>

The standard opening tag is <?php and the closing tag is ?>. There is also a short echo tag, <?= ... ?>, which is shorthand for <?php echo ... ?> and is handy when you mix PHP into HTML:

<p>Welcome, <?= $name ?>!</p>

In a file that contains only PHP (no surrounding HTML), it is best practice to leave off the closing ?> tag. This prevents accidental whitespace after it from being sent to the browser, which can break headers and cause hard-to-find bugs.

Statements and Semicolons

A PHP program is a sequence of statements. Each statement must end with a semicolon (;). Forgetting it is the most common beginner error and produces a parse error.

<?php
$greeting = "Hello";   // statement 1
echo $greeting;        // statement 2

Whitespace and line breaks between statements are ignored, so you are free to indent for readability. Curly braces { } group statements into blocks (for example, the body of an if or a function).

Comments in PHP

Comments are notes for humans; PHP ignores them when running the script. They come in three forms:

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

# This is also a single-line comment

/*
  This is a
  multi-line comment
*/
echo "Comments do not appear in the output";

For a deeper look, see PHP Comments.

Variables in PHP

A variable is a named container for a value. In PHP, variable names always start with the $ symbol, followed by a letter or underscore, then any mix of letters, digits, and underscores. You do not declare a type — PHP infers it from the value you assign.

$firstName = "John";
$lastName = "Doe";
$age = 30;

Variable names are case-sensitive: $firstName and $FirstName are two completely different variables. (Keywords like echo, if, and function are not case-sensitive, but it is conventional to write them in lowercase.)

Learn more in PHP Variables.

Data Types in PHP

PHP supports several built-in data types. The value you assign determines the type, and PHP converts between types automatically when needed.

TypeExample
String"John Doe"
Integer42
Float3.14
Booleantrue / false
Array["a", "b", "c"]
Nullnull

Strings hold text and can use single or double quotes. The difference matters: double quotes parse variables and escape sequences, single quotes treat the content literally.

<?php
$name = "John";
echo "Hello, $name\n";  // Hello, John  (variable is parsed)
echo 'Hello, $name';    // Hello, $name (printed literally)

Arrays store multiple values under one name. You can build them with the array() function or the short [] syntax — they are equivalent, and [] is preferred in modern code.

$fruits = array("apple", "banana", "cherry");
$vegetables = ["carrot", "potato", "onion"];

echo $fruits[0];      // apple  (indexes start at 0)
echo $vegetables[2];  // onion

See PHP Data Types and PHP Arrays for the full picture.

Operators in PHP

Operators perform actions on values and variables. The most common groups are arithmetic, comparison, and logical operators.

<?php
$x = 10;
$y = 20;
$sum = $x + $y;        // 30  (arithmetic)

var_dump($x == $y);    // bool(false)  (loose comparison)
var_dump(5 == "5");    // bool(true)   (== compares value only)
var_dump(5 === "5");   // bool(false)  (=== also compares type)

Note the difference between == (loose equality, compares value) and === (strict equality, compares value and type). Using === avoids surprising results when comparing numbers with strings. The full list is in PHP Operators.

Functions in PHP

A function is a reusable block of code. You define one with the function keyword, a name, and parentheses that hold any parameters. Call it later by writing its name with arguments.

<?php
function greet($name) {
  return "Hello, " . $name . "!";
}

echo greet("John");  // Hello, John!
echo "\n";
echo greet("Jane");  // Hello, Jane!

Here the . is the string concatenation operator — it joins two strings together. Functions help you avoid repeating yourself; more detail lives in PHP Functions.

Try it Yourself isn't available for this example.

Conclusion

You now know the building blocks of PHP syntax: code lives inside <?php ... ?> tags, every statement ends with a semicolon, comments come in three styles, and variables are written with a leading $. From there, data types, operators, and functions let you store and process information. Master these basics and the rest of PHP — control flow, classes, and the standard library — will feel familiar.

A good next step is PHP Echo and Print to learn how to send output to the page.

Practice

Practice
In PHP programming, which of the following are correct ways to begin and end a PHP command block?
In PHP programming, which of the following are correct ways to begin and end a PHP command block?
Was this page helpful?