This article is forwarded from: https://blog.csdn.net/KH_FC/a…

PHP Basic Syntax

PHP contains PHP code using a special pair of tags, mixed in with HTML code. When the server parses the page, it can automatically filter out the PHP script and interpret it, and finally pass the generated static page to the client.

1. The PHP tags

In general, PHP code is embedded in HTML documents. PHP code can be embedded in HTML in several ways:

  1. The default tag

The default flag is passed by “<? PHP “and”? The >” tag separates HTML from PHP code. Such as:

<? This is the PHP code you want to write. >

Using this default tag, you can mix PHP and HTML code in an HTML document at will. For example:

<! DOCTYPE html> <html> <head> <title></title> </head> <body> <? php if ($test) { ? > <div> $test is true.</div> <? php } else { ? > <div> $test variable is false.</div> <? php } ? > </body> </html>

The PHP $test variable is false.

[Note] Using the above example method to output large HTML strings is much easier and faster than using echo() or print().

  1. A script tag

The script tag is used to set the scripting language for PHP using the language property in script. Such as:

<script language=" PHP "> </script>
  1. Short tag

The abbreviated tag is compared to the default tag by removing PHP keywords for easy writing. Such as:

<? # Write PHP code here! ? >
  1. ASP tags

The ASP tag is written in a Java-like manner, splitting PHP code with a pair of <% and %> tags. Such as:

<% # write code here! % >

Short_Open_Tag and Asp_Tags are set to ON in the PHP configuration file php.ini. If you want to write PHP code using this method, you should set short_open_tag and asp_tags to ON. Modify as follows:

short_open_tag = On
asp_tags = On

2. PHP comments

Any programming language has to write comments, so let’s take a look at what PHP has

  1. C++ language-style one-line comments

Such as:

<? PHP // Here is my comment! ? >

This single-line comment mode is often used, and it is also a relatively common comment mode

<? php echo 123; // Output 123? >

The output: 123 will not show the content of my comment.

  1. Shell scripting language-style comments

Such as:

<? PHP /* Here is a multi-line note to explain! * /? >

Multi-line comments are mainly used at the beginning of a brief description of the code

<? $a = 10; $a = 10; $b = 20; $c = $a * $b; echo $c; ? >

The output result is: 200 [Note] do not write the code in the multi-line comment to go Yo, otherwise it is not effective and cannot be explained by the baseline Yo!! What happens if I write my PHP code to multiple lines of comments

<? $d = 200; $d = 200; */ $a = 10; $b = 20; $c = $a * $b * $d; echo $c; ? >

The output is: 0. Here, because I put the variable d inside the comment, PHP will not interpret the code inside the comment

  1. C-style multi-line comments

Such as:

<? PHP # Here is my comment! ? >

Single line comment

<? php $a = 10; $b = 20; $b = 20; $c = $a * $b; $c = $a * $b; # echo $c; # This is the output C variable? >

The output is: 200 [Note] Do not write a single line comment? > yo, do not let the code behind will not be effective, there will be an error yo!! When a single line comment is added? What’s going to happen to >

<? php $a = 10; $b = 20; $b = 20; # b is this? $c = $a * $b; # echo $c; # This is the output C variable? >

The output is as follows:

When a single-line comment type is added? >At the end, PHP stops interpreting the rest of the code and the rest of the code is rendered as HTML

3. PHP directive delimiter

With C, Perl and other languages have the same delimiter; , after statements that need to be delimited, when a piece of PHP code contains a terminator? > can omit the delimiter; , so the last piece of PHP code can omit the delimiter because? The > terminator contains a delimiter. Use delimiters normally

<? PHP echo "first line of code "; Echo "second line of code "; ? >

The output is as follows:



The instanceThe last piece of code omits the delimiter

<? PHP echo "first line of code "; Echo "second line of code "; Echo "last line of code"? >

The output is as follows: