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 betweenecho
"Hello, PHP!"
;
?>
- is the standard PHP tag.
- echo is used to output content.
- A semicolon must be added at the end of each statement.
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:
- Starting from // or #, until the end of the line.
- Commonly used for brief explanations.
2. Multi-line comments (block comments)
Use /* ... */
Characteristics:
- Starts with /* and ends with */.
- Can be written across multiple lines.
- Commonly used for longer code explanations, function comments, etc.
3. DocBlock comments
This is a special form of multi-line comment, used to generate API documentation, suitable for describing classes, functions, etc.
Characteristics:
- Using
/** ... */Enclosed with (note the two asterisks) - Supports @param, @return, @var, @author
- Commonly used with IDEs or documentation generation tools (such as phpDocumentor)
| 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 »