Web Development /PHP Programming / Lesson
Web Development

PHP Syntax

Learn the fundamental syntax rules of PHP, including PHP tags, statements, semicolons, comments, whitespace, output, variables, strings, expressions, and embedding PHP inside HTML.

🎯

Learning Objectives

["Understand the basic structure of a PHP script.","Use PHP opening and closing tags correctly.","Understand how PHP statements are written and terminated.","Use semicolons correctly in PHP programs.","Write single-line and multi-line comments.","Understand how whitespace and indentation affect PHP code.","Display information using echo and print.","Understand how strings are written in PHP.","Embed PHP code safely inside HTML documents.","Identify common PHP syntax errors and correct them."]

📖

Lesson Content

PHP Syntax

PHP syntax refers to the rules and structure used to write valid PHP programs. Just as spoken languages have grammar rules, programming languages have syntax rules that tell the computer how instructions should be written.

Understanding PHP syntax is essential because even a small syntax mistake can prevent a PHP script from executing correctly.

PHP Opening and Closing Tags

PHP code is normally written between an opening PHP tag and, when necessary, a closing PHP tag.

<?php

echo "Hello World!";

?>

The <?php tag tells the server that PHP code begins at that point.

The ?> tag indicates the end of a PHP section.

When a file contains only PHP code, developers commonly omit the closing tag. This helps prevent accidental whitespace or output after the PHP code.

<?php

echo "Hello World!";

For a PHP-only file, this is often the preferred style.

PHP Statements

A PHP statement is an instruction given to the PHP interpreter. Most PHP statements end with a semicolon.

<?php

echo "Welcome to PHP";
echo "Learning PHP is useful";

?>

Each echo instruction is a separate statement.

The Semicolon

The semicolon ; marks the end of most PHP statements.

<?php

echo "First statement";
echo "Second statement";
echo "Third statement";

?>

If a semicolon is accidentally omitted where one is required, PHP may generate a syntax error.

PHP Comments

Comments are notes written inside source code. They help developers explain what code does without affecting the program’s normal output.

Single-Line Comments

Two forward slashes can be used to create a single-line comment.

<?php

// Display a welcome message
echo "Welcome to Dwallnet Technologies";

The hash symbol can also be used for a single-line comment.

<?php

# Display a welcome message
echo "Welcome to PHP";

Multi-Line Comments

Multi-line comments begin with /* and end with */.

<?php

/*
This is a multi-line comment.
It can contain several lines.
*/

echo "Hello";

Comments are useful for documenting complex sections of an application and explaining important decisions.

PHP Is Embedded in HTML

One of PHP’s useful features is the ability to combine PHP with HTML.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>PHP Page</title>
</head>
<body>

	<h1>Student Dashboard</h1>

	<?php
	echo "<p>Welcome to your dashboard.</p>";
	?>

</body>
</html>

The HTML defines the structure of the page while PHP generates dynamic content.

Using echo

The echo construct is commonly used to send output from PHP to the response.

<?php

echo "Hello World!";

Multiple pieces of output can be generated using multiple echo statements.

<?php

echo "Welcome ";
echo "to ";
echo "Dwallnet Technologies";

HTML can also be included in an echo statement.

<?php

echo "<h2>Welcome to PHP</h2>";
echo "<p>This content was generated by PHP.</p>";

Using print

PHP also provides the print construct for displaying output.

<?php

print "Hello from PHP";

Both echo and print can display strings, although they have some technical differences that become more important in advanced PHP programming.

Strings in PHP

A string is a sequence of characters. Strings can be written using single quotation marks or double quotation marks.

<?php

echo 'Hello World';

echo "Hello World";

Single and double quotes can behave differently when variables and special characters are involved, which will become important when working with PHP variables.

Single-Quoted Strings

<?php

echo 'Welcome to PHP programming';

Single-quoted strings are useful when the text should be treated mostly as literal text.

Double-Quoted Strings

<?php

echo "Welcome to PHP programming";

Double-quoted strings support variable interpolation and several escape sequences.

Escape Characters

Escape characters allow special characters to be represented inside strings.

For example, \n can represent a new line in suitable output contexts, while \” can be used to include a double quotation mark inside a double-quoted string.

<?php

echo "He said \"Hello\"";

Whitespace and Indentation

Whitespace includes spaces, tabs, and line breaks. PHP generally ignores unnecessary whitespace outside of strings, but good indentation makes code easier to read and maintain.

For example:

<?php

echo "Welcome";
echo "to";
echo "PHP";

Can also be written with additional spacing:

<?php

echo "Welcome";

echo "to";

echo "PHP";

The output is still generated correctly. However, developers should use consistent formatting rather than adding random whitespace.

Case Sensitivity

PHP has specific case-sensitivity rules. Variable names are case-sensitive, meaning $student and $Student are different variables.

For example:

<?php

$student = "John";
$Student = "Peter";

echo $student;
echo $Student;

These are treated as separate variables.

PHP language constructs and keywords have different case-sensitivity behavior, but developers should follow consistent lowercase coding conventions for readability.

PHP Code Blocks

PHP code can contain multiple statements inside a single PHP block.

<?php

echo "Line one";
echo "Line two";
echo "Line three";

Keeping related PHP statements together can make the structure of an application easier to understand.

Combining HTML and PHP

PHP can be used to dynamically generate HTML.

<h1>Dwallnet Student Portal</h1>

<?php

echo "<p>Welcome to the student portal.</p>";

?>

PHP can also be used to generate values inside HTML elements.

<?php

$name = "David";

?>

<h1>Welcome, <?php echo $name; ?></h1>

This technique becomes very useful when building dynamic websites.

Alternative PHP Syntax

PHP provides alternative syntax forms for some control structures. These are commonly encountered when PHP is mixed heavily with HTML.

For example, the alternative syntax for an if statement can use a colon and an endif statement.

<?php if ( true ): ?>

	<p>This content is displayed.</p>

<?php endif; ?>

This style can make large PHP templates easier to read, although developers should use a consistent style throughout a project.

PHP Syntax Errors

A syntax error occurs when PHP encounters code that does not follow the language’s rules.

For example:

<?php

echo "Hello"

The statement is missing a semicolon. Depending on the surrounding code and PHP version, this can cause a parse error.

The corrected version is:

<?php

echo "Hello";

Common Syntax Mistakes

Beginners commonly make several syntax mistakes when learning PHP.

  • Forgetting a semicolon.
  • Using incorrect PHP opening tags.
  • Forgetting quotation marks around strings.
  • Using mismatched quotation marks.
  • Using an opening bracket without the correct closing bracket.
  • Misspelling PHP keywords or function names.
  • Mixing PHP and HTML incorrectly.
  • Forgetting to save a PHP file with the .php extension.
  • Running PHP code without a PHP-enabled server environment.

PHP Syntax and Readability

Correct syntax is necessary for a PHP program to execute, but readable syntax is equally important for maintaining larger projects.

Developers should use meaningful formatting, indentation, comments, and consistent naming conventions.

For example:

<?php

echo "Welcome to Dwallnet Technologies.";

echo "You are learning PHP.";

echo "Keep practicing!";

This is easier to understand than placing every instruction on a single long line.

PHP Syntax in a Complete Example

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>PHP Syntax Example</title>
</head>
<body>

	<h1>Learning PHP</h1>

	<?php

	// Display a welcome message
	echo "<p>Welcome to Dwallnet Technologies.</p>";

	// Display another message
	echo "<p>You are learning PHP syntax.</p>";

	?>

</body>
</html>

Practical Exercise

Create a file named syntax.php and write a PHP program that displays the following:

  • Your name.
  • The name of your school or organization.
  • The course you are currently studying.
  • A short message about why you want to learn PHP.

Use at least two PHP comments and multiple echo statements in your solution.

Summary

PHP syntax defines how PHP instructions must be written. PHP code is commonly placed inside <?php tags, statements generally end with semicolons, comments can document code, and echo or print can be used to generate output.

PHP can be embedded within HTML, making it possible to build dynamic webpages. Understanding syntax carefully will help prevent common parse errors and provide a strong foundation for the more advanced PHP concepts that follow.

In the next lesson, we will learn about variables and constants, which allow PHP programs to store and work with information.

💻

Code Example

<?php

// Display a welcome message
echo "Welcome to Dwallnet Technologies!";

// Display another message
echo "<br>";
echo "This is my second PHP program.";

// Display HTML from PHP
echo "<h2>Learning PHP Syntax</h2>";

?>
⭐

Key Takeaways

["PHP code is normally written inside tags.","PHP-only files commonly omit the closing PHP tag.","Most PHP statements end with a semicolon.","Comments can be created using \/\/, #, or \/* *\/.","PHP can be embedded directly inside HTML documents.","The echo and print constructs can generate output.","Strings can be written using single or double quotation marks.","PHP variable names are case-sensitive.","Good indentation and formatting improve code readability.","Syntax errors can prevent PHP scripts from executing correctly."]

Enjoying this lesson?

Sign in to your Dwallnet Academy student account to track your learning progress and mark lessons as complete.

Sign In to Track Progress