Common PHP syntax 1: PHP script code mark
The PHP script is the content included in a pair of special tags in the file. For example, ASP is "<% .... %> ", PHP can be viewed as" <?...?> ".
However, to adapt to XML standards to embed PHP into XML or XHTML, PHP does not recommend the use of "<?...?>" in short format ", We recommend that you mark "<? Php...?>"
In addition, the PHP code block supports the markup form of <script language = "php">... </script>.
Common PHP syntax 2: PHP Command Separator
Each PHP statement needs to be separated by the Semicolon ";", but the PHP end mark "?>" Because it automatically implies a semicolon, you do not need to append the semicolon.
Therefore, the format of a PHP script is as follows:
<? Php
// Note that the last row can have no semicolon
?>
PHP common syntax 3: PHP comments
PHP multi-line comment using ""
Use "#" or "//" for a single line comment
Common PHP syntax 4: PHP output
Use "<% =... %>" in ASP to quickly output a single row, or use "<% Response. Write ("... ") %>"
Directly use "echo ()" or "print ()" in PHP, for example:
- < ?php
- echo "a";
- echo (b);
- echo ("c");
- echo d;
- ?>
The output is "abcd". All the above four types can be output normally.
However, in ASP, especially echo "a"; and echo d;, both are output as strings themselves, which is impossible. Therefore, you need to understand the definition of PHP variables.
Common PHP syntax 5: PHP Variables
Like ASP, PHP variables can be directly used without being defined first. Variable types are automatically generated when values are assigned.
Various variables in PHP Add "$" before the variable name to show the difference.
- < ?php
- $a="123";
- echo a;
- echo $a;
- ?>
Enter "a123"
Common PHP syntax 6: The difference between single quotes and double quotes in PHP
- < ?php
- $a="123";
- echo "$a";
- echo '$a';
- ?>
The output is "123 $ a", where echo "$ a" outputs the value of variable a, while echo '$ a' outputs the string itself in single quotes.
- < ?php
- $a="123";
- echo "$a'$a'";
- ?>
The output value "123 '123' is not" 123 $ ". Although it is '$ A', the variable placed under double quotation marks is still replaced.
Therefore, we can conclude that variables in double quotation marks will be replaced, while those in single quotation marks will not be replaced.
The content in double quotation marks must be escaped and prefixed with "", such as "", "$", and "". Therefore, if you enter "123 $ ",
- < ?php
- $a="123";
- echo "$a$a";
- ?>
For example:
- < ?php
- $a="123";
- echo "$a$a"";
- ?>
The output is "123 $ "".
Notes on common PHP syntaxes for ASP transfer to PHP:
1. The separator comma ";" is easy to forget to write.
2. Definition and use of variables.
3. single quotation marks and double quotation marks.