Basic PHP Syntax and Hello World

Table of Contents

  • Introduction to PHP Syntax
  • PHP Tags and Code Structure
  • PHP Statements and Semicolons
  • Comments in PHP
  • Output in PHP: echo vs print
  • Hello World in PHP
  • Best Practices for Writing Clean PHP Code
  • Summary

Introduction to PHP Syntax

Before diving into variables, functions, and logic, it’s essential to understand the basic syntax of PHP. Knowing the structure and how PHP code is interpreted by the server lays the foundation for everything that follows in this course.

PHP code is embedded within HTML, making it ideal for creating dynamic web pages. The PHP parser looks for specific tags to identify and process PHP code.


PHP Tags and Code Structure

All PHP code must be written inside PHP opening and closing tags so that the server knows which part of the page to interpret as PHP.

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

This is the standard opening and closing tag, and it’s the most commonly used. There are a few other types of PHP tags (like <? ?>, <?= ?>), but using <?php ?> ensures maximum compatibility.

Example:

<?php
echo "This is a PHP script!";
?>

PHP scripts can be placed anywhere in the document — at the top, middle, or bottom of your HTML file. However, they’re typically placed inside the <body> tag when used for dynamic content rendering.


PHP Statements and Semicolons

Every PHP statement ends with a semicolon (;). This tells the PHP parser that the instruction is complete.

<?php
echo "Hello, World!";
echo "This is a second line.";
?>

If you forget to add a semicolon, PHP will throw a syntax error and the script may not run at all.


Comments in PHP

Comments are lines in code that are not executed. They help you or other developers understand the code later.

PHP supports three types of comments:

  • Single-line comment (using //)
  • Shell-style comment (using #)
  • Multi-line comment (using /* */)

Examples:

// This is a single-line comment
# This is also a single-line comment
/*
This is a
multi-line comment
*/

Use comments generously to explain your logic and improve code maintainability.


Output in PHP: echo vs print

The two primary ways to output content to the browser in PHP are echo and print.

echo:

  • Can output one or more strings.
  • Slightly faster than print.
  • Does not return a value.
echo "Hello from echo!";

print:

  • Can only output one string.
  • Returns a value of 1 (which makes it usable in expressions).
print "Hello from print!";

For general use, echo is more common due to its flexibility and speed.


Hello World in PHP

The classic “Hello, World!” program is the perfect starting point to understand PHP syntax.

Step-by-Step:

  1. Open your code editor and create a new file named hello.php.
  2. Write the following code:
<?php
echo "Hello, World!";
?>
  1. Save the file in your web server directory (htdocs if using XAMPP).
  2. Open your browser and go to:
http://localhost/hello.php
  1. You should see:
Hello, World!

Congratulations! You’ve just written your first PHP program.


Best Practices for Writing Clean PHP Code

  • Always use <?php ?> tags for compatibility.
  • End each statement with a semicolon.
  • Use comments to document your code.
  • Maintain consistent indentation and spacing.
  • Place PHP files in organized folders to manage large projects easily.
  • Avoid mixing too much HTML with PHP—separate logic and presentation where possible.

Summary

In this module, you explored the basic syntax of PHP, including how to open and close PHP tags, write statements, use comments, and output data with echo and print. You also created your first PHP file and ran the classic Hello, World! script in the browser.

Understanding PHP syntax is crucial as it serves as the building block for everything from functions and loops to advanced features like object-oriented programming.