PHP is a powerful embedded HTML scripting language that is often used by programmers as the basic language for website development. What are the basic syntaxes in PHP? In this article, I will summarize the common syntax of PHP for you.
PHP basic syntax 1, PHP script code tags
PHP script is the content included in a pair of special tags in the file, such as ASP is "<%. ...%>", PHP can be regarded as "...?>".
However, in order to adapt to the XML standard to embed PHP into XML or XHTML, PHP does not recommend using the short format "...?>", but recommends using the long format tag " php...?>”
In addition, the PHP code block also supports the tag form of .
PHP basic syntax 2, PHP instruction delimiter
Each statement in PHP needs to be separated by a semicolon ";", but for the PHP end mark "?> ;", because it automatically implies a semicolon, so there is no need to append a semicolon.
So, the format of a PHP script can be as follows:
<?php //注意最后一行可以没有分号 ?>
PHP basic syntax three, PHP comments
PHP multi-line comments use ""
Use "#" or "//" for single-line comments
PHP basic syntax 4, PHP output
Use "<%= in ASP ...%>" quickly output a single line, or use "<%Response.Write("...")%>"
Use "echo()" or "print directly in PHP ()", for example:
< ?php echo "a"; echo (b); echo ("c"); echo d; ?>
will be output as "abcd", and all the above four types can be output normally.
But this is impossible in ASP, especially when echo "a"; and echo d; are both output as strings themselves. This requires understanding PHP variable definitions.
PHP basic syntax 5, PHP variables
Like ASP, PHP variables do not need to be defined first and can be used directly. The type of the variable is automatically generated when assigning a value.
Various variables in PHP are distinguished by adding "$" before the variable name.
< ?php $a="123"; echo a; echo $a; ?>
The input is “a123”
PHP basic syntax six, 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, and echo '$a' outputs the string itself in single quotes.
< ?php $a="123"; echo "$a'$a'"; ?>
The output is "123'123'" not "123$a". Although it is '$a', the variable placed under double quotes is still replaced.
So, it can be concluded that as long as the variables are in double quotes, they will be replaced; while those in single quotes will not be replaced.
If the content in double quotes needs to be escaped, use the "" prefix, such as "", "$", """. So to enter "123$a", it is
< ?php $a="123"; echo "$a$a"; ?>
Another example:
< ?php $a="123"; echo "$a$a""; ?>
The output is "123$a"".
What you need to pay attention to when transferring from ASP to PHP is:
1. It is easy to forget to write the separator comma ";".
2, definition and use of variables.
3, the usage of single quotes and double quotes.
Related recommendations:
php A complete list of the most commonly used built-in functions
The above is the detailed content of What are the basic syntax of php? Introduction to basic usage of PHP. For more information, please follow other related articles on the PHP Chinese website!