Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Syntax

PHP Syntax


PHP scripts are executed on the server, and then the plain HTML results are sent back to the browser.

PHP uses to enclose code.


Basic PHP Syntax

PHP scripts can be placed anywhere in a document.

A PHP script starts with and ends with:


The default file extension for PHP files is .php.

PHP files usually contain HTML tags and some PHP script code.

Example

In between

echo

"Hello, PHP!"

;

?>

Below, we provide a simple PHP file example that can output the text "Hello World!" to the browser:

My first PHP page

Each line of code in PHP must end with a semicolon. The semicolon is a separator used to distinguish sets of instructions.

With PHP, there are two basic instructions for outputting text to the browser:echo and print.


Comments in PHP

In PHP, comments are used to explain what the code does; they are not executed and are not output to the page.

There are mainly three forms of comments:

1. Single-line comments (2 forms)

Use //


Use #


Characteristics:

2. Multi-line comments (block comments)

Use /* ... */


Characteristics:

3. DocBlock comments

This is a special form of multi-line comment, used to generate API documentation, suitable for describing classes, functions, etc.


Characteristics:

Purpose Recommended comment forms Example
Briefly explain a single line of code // or # // 输出信息
Explain a block of code /* ... */ Comments for logical grouping
Function/class documentation /** ... */ DocBlock Used to describe parameters, return values, purpose, etc.

Example

Run Example »