Home  >  Article  >  Backend Development  >  The most basic syntax for getting started with PHP_PHP tutorial

The most basic syntax for getting started with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:40813browse

1,从 HTML 中分离

凡是在一对开始和结束标记之外的内容都会被 PHP 解析器忽略,这使得 PHP 文件可以具备混合内容。 可以使 PHP 嵌入到 HTML 文档中去,如下例所示。

This is going to be ignored by PHP and displayed by the browser.



This will also be ignored by PHP and displayed by the browser.

这将如预期中的运行,因为当 PHP 解释器碰到 ?> 结束标记时就简单地将其后内容原样输出(除非马上紧接换行 - 见指令分隔符)直到碰到下一个开始标记;
例外是处于条件语句中间时,此时 PHP 解释器会根据条件判断来决定哪些输出,哪些跳过。

见下例,注意if 和 else 后面的 : 冒号

使用条件结构:
   
      This will show if the expression is true.
   
      Otherwise this will show.
   


上例中 PHP 将跳过条件语句未达成的段落,即使该段落位于 PHP 开始和结束标记之外。
由于 PHP 解释器会在条件未达成时直接跳过该段条件语句块,(:?> 跳出了 PHP 模式并返回了 HTML 模式) 因此 PHP 会根据条件来忽略之。
要输出大段文本时,跳出 PHP 解析模式通常比将文本通过 echo 或 print 输出更有效率。

    getData())):?>
       


           

               
                   

               
      
                                            

-------------------------------------------------- ----------------------------------

2, command separator

The closing tag in a piece of PHP code implicitly represents a semicolon

The last line in a PHP snippet does not need to end with a semicolon
If a newline follows, the end of the line is included in the snippet's closing tag.
echo "This is a test";
?>

Note:

The end tag of the PHP code segment at the end of the file is not required

Sometimes it is better to omit it when using include or require so that unwanted whitespace does not appear at the end of the file and the response headers can still be output later.

It's also convenient when using output buffering, so you don't see unwanted whitespace characters generated by include files.

342432353

otherwise this will show.

or

342432353

otherwise this will show.

-------------------------------------------------- ----------------------------------

PHP Comments

echo "This is a test"; // This is a one-line c++ style comment
/* This is a multi line comment
          yet another line of comment */
echo "This is yet another test";
echo 'One Final Test'; # This is a one-line shell-style comment
?>

Single-line comments only comment to the end of the line or to the current block of PHP code, whichever comes first.

This means that HTML code after // ... ?> or # ... ?> will be displayed

:?> exits PHP mode and returns to HTML mode, // or # does not affect this.

C-style comments end when the first */ is encountered.
/*
echo "This is a test"; /* This comment will cause a problem */

*/

echo 'kdfjal';

?>
An error will be reported and the output will be blank

-------------------------------------------------- ----------------------------------
String string

A string string is composed of a series of characters, where each character is equivalent to one byte. This means that PHP can only support character sets of 256=2 to the power of 8, and therefore does not support Unicode. See the detailed explanation of string types for details.

Note: The maximum size of string can reach 2GB.

Grammar

A string can be expressed in 4 ways:

single quote

     Double quotes
Heredoc syntax structure
Nowdoc syntax structure (since PHP 5.3.0)

-------------------------------------------------- ----------------------------------
Single quote

The simplest way to define a string is to surround it with single quotes (character ').

To express a single quote itself, you need to escape it by adding a backslash () in front of it.

To express a backslash by itself, use two backslashes (\).
Any other form of backslash will be treated as the backslash itself: that is, if you want to use other escape sequences such as r or n, it does not mean any special meaning, just the two characters themselves.

Note: Unlike double quotes and heredoc syntax constructs, escape sequences of variables and special characters within single quoted strings will not be replaced.

echo 'this is a simple string';

//Can enter multiple lines

echo 'You can also have embedded newlines in
strings this way as it is
okay to do';

// Output: Arnold once said: "I'll be back"

echo 'Arnold once said: "I'll be back"';

// Output: You deleted C:*.*?

echo 'You deleted C:\*.*?';

// Output: You deleted C:*.*?

echo 'You deleted C:*.*?';

// Output: This will not expand: n a newline

echo 'This will not expand: n a newline';

// Output: Variables do not $expand $either

echo 'Variables do not $expand $either';
?>
-------------------------------------------------- ----------------------------------

Double quotes

If the string is enclosed in double quotes ("), PHP will parse some special characters:
Escape character

Sequence Meaning
n Line feed (LF or 0x0A (10) in ASCII character set)
r Carriage return (CR or 0x0D (13) in ASCII character set)
t Horizontal tab character (HT or 0x09 (9) in the ASCII character set)
v Vertical tab (VT or 0x0B (11) in ASCII character set) (since PHP 5.2.5)
e Escape (ESC or 0x1B (27) in ASCII character set) (since PHP 5.4.0)
f Form feed (FF or 0x0C (12) in ASCII character set) (since PHP 5.2.5)
\ Backslash
$ Dollar mark
" " Double quotation marks
[0-7]{1,3} What matches this regular expression sequence is a character expressed in octal notation
x[0-9A-Fa-f]{1,2} What matches this regular expression sequence is a character

expressed in hexadecimal form

Like single-quoted strings, escaping any other characters will cause the backslash to be displayed. Prior to PHP 5.1.1, backslashes in {$var} were not displayed.

The most important feature of strings defined with double quotes is that variables will be parsed. See variable parsing for details.


-------------------------------------------------- ----------------------------------

Heredoc structure

The third way to express a string is to use the heredoc syntax structure: <<<. After the operator, provide an identifier and then a newline. Next is the string string itself, and finally the identifier defined earlier is used as the end mark.

The identifier quoted at the end of

must be in the first column of the line, and the naming of the identifier must follow the same PHP rules as other tags: it can only contain letters, numbers, and underscores, and must start with letters and Begin with an underscore.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477459.htmlTechArticle1, Separate from HTML Any content outside a pair of opening and closing tags will be ignored by the PHP parser , which allows PHP files to have mixed content. Can make PHP embedded in HTML...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn