Comprehensive Guide to PHP Syntax

PHP is a popular server-side scripting language designed for web development. A PHP script can be placed anywhere in the document, and it starts with <?php and ends with ?>.

The default file extension for PHP files is .php.

A PHP file normally contains HTML tags, along with some PHP scripting code.

Key Topics

PHP Structure

The basic structure of a PHP file includes HTML and PHP code. PHP code is executed on the server, and the result is sent to the client's browser.

Example: Get Your Own PHP Server

<!DOCTYPE html>
<html>
<body>
    <h1>My First PHP Page</h1>
    <?php echo "Hello World!"; ?>
</body>
</html>

Output:

Hello World!

Code Explanation

This example demonstrates the following:

  • The HTML structure of the page with a heading.
  • The PHP code block that outputs the string Hello World! using the echo function.

Explanation: The code combines HTML and PHP. The echo statement is used to output the text Hello World! on the web page.

Important Note

PHP statements end with a semicolon ;.