PHP Syntax
PHP scripts are executed on the server and the plain HTML results are sent back to the browser.
Basic PHP syntax
PHP scripts can be placed anywhere in the document.
PHP scripts start with <?php and end with ?>:
<?php // PHP 代码 ?>
The default file extension for PHP files is ".php".
PHP files usually contain HTML tags and some PHP script code.
Below, we provide a simple PHP file example, which can output the text "Hello World!" to the browser:
Example (1)
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
Example ( 2)
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "my php!"; ?> </body> </html>
Example (3)
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php print "my php!"; ?> </body> </html>
Every line of code in PHP must end with a semicolon. A semicolon is a delimiter used to separate sets of instructions.
Through PHP, there are two basic instructions for outputting text in the browser: echo and print.
Comments in PHP
Examples
<!DOCTYPE html> <html> <body> <?php // 这是 PHP 单行注释 /* 这是 PHP 多行 注释 */ echo "Hello World!"; //echo "Hello World!"; /*echo "Hello World!"; */ ?> </body> </html>
Notes:
Every code in PHP Lines must end with a semicolon. A semicolon is a delimiter used to separate sets of instructions.
Through PHP, there are two basic instructions for outputting text in the browser: echo and print.
In actual use, the functions of print and echo are almost identical.
It can be said that wherever one can be used, the other can also be used. However, there is still a very important difference between the two:
In the echo function, multiple strings can be output at the same time, while in the print function, only one string can be output at the same time. At the same time, the echo function does not require parentheses, so the echo function is more like a statement than a function.
echo is more efficient.
Next Section