0Pricing
PHP Academy · Lesson

PHP Syntax and Structure

Understand the basic syntax and structure of PHP code.

PHP Syntax and Structure is a free PHP Academy lesson on CoddyKit — lesson 1 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

PHP Syntax and Structure

Welcome to the next step in your PHP journey! In this lesson, you’ll learn the foundational syntax and structure of PHP, which will form the backbone of your programming skills. Let’s dive in!

PHP Syntax and Structure — illustration 1

2

PHP Tags

PHP code must be written inside PHP tags. This tells the server where the PHP code starts and ends:

  • <?php is used to open a PHP block.
  • ?> is used to close it.

Tip: PHP can be embedded in HTML or used in standalone files with the .php extension.

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

3

Case Sensitivity in PHP

PHP keywords, like echo, are not case-sensitive, but variable names are. Example:

Key Point: Be consistent with your variable names to avoid errors.

<?php
  $greeting = "Hello, World!";
  echo $greeting; // Correct
  echo $Greeting; // Error: Undefined variable
?>

4

Ending Statements

Every PHP statement must end with a semicolon (;). Forgetting this will result in a syntax error.

Example:

<?php
  echo "This is a valid statement.";
  // echo "This will cause an error" // Missing semicolon
?>

5

PHP Comments

Comments are ignored by the PHP interpreter and are used to explain your code:

  • Single-line comment: Use // or #.
  • Multi-line comment: Use /* */.
<?php
  // This is a single-line comment
  echo "Hello, PHP!";

  /*
    This is a multi-line comment.
    Use it for longer explanations.
  */
  echo "Comments are ignored by PHP.";
?>

6

Whitespace and Indentation

PHP ignores extra whitespace, but using consistent indentation makes your code more readable:

Example:

Tip: Use consistent spaces or tabs to structure your code for clarity.

<?php
  echo "Hello!"; // No indentation
  if (true) {
      echo "Indented block!"; // Proper indentation
  }
?>

7

Combining PHP and HTML

PHP can be seamlessly integrated into HTML to create dynamic content. Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>PHP and HTML</title>
</head>
<body>
  <h1><?php echo "Dynamic Content with PHP"; ?></h1>
</body>
</html>

8

Error Handling in Syntax

PHP will notify you of syntax errors during execution. Common mistakes include:

  • Missing semicolons.
  • Unmatched quotes or brackets.
  • Incorrectly named variables.

Tip: Use a code editor with syntax highlighting to catch errors early.

9

10

Great Job!

Congratulations! You’ve learned the basic syntax and structure of PHP, including tags, statements, comments, and integrating PHP with HTML. In the next lesson, we’ll explore how to embed PHP within HTML to create dynamic web pages. Let’s keep coding!

PHP Syntax and Structure — illustration 10

Frequently asked questions

Is the “PHP Syntax and Structure” lesson free?

Yes — the full text of “PHP Syntax and Structure” is free to read here on the web, and the PHP Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “PHP Syntax and Structure”?

Understand the basic syntax and structure of PHP code. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 2, so you can start here or from the beginning and move at your own pace.

How long does the “PHP Syntax and Structure” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. PHP Syntax and Structure
  2. Embedding PHP in HTML
← Back to PHP Academy