Basic PHP syntax

Source: Internet
Author: User
Tags closing tag

1. Separate from HTML

Any content except the start and end tags will be ignored by the PHP parser, which allows the PHP file to have mixed content. PHP can be embedded into HTML documents, as shown in the following example.

<P> This is going to be ignored by PHP and displayed by the browser. </p>
<? Php echo 'While this is going to be parsed. ';?>
<P> This will also be ignored by PHP and displayed by the browser. </p>

This will be as expected, because when the PHP interpreter encounters?> When the end mark is used, the original content is output (unless the line feed is immediately followed-see the command separator) until the next start mark is reached;
When the exception is in the middle of a Condition Statement, the PHP interpreter determines which outputs and skips Based on the condition.

See the following example. Note the colon after if and else.

Condition structure:
<? Php if ($ expression = true):?>
This will show if the expression is true.
<? Php else:?>
Otherwise this will show.
<? Php endif;?>

In the above example, PHP will skip the section not fulfilled by the condition statement, even if the section is out of the PHP start and end mark.
Because the PHP interpreter directly skips the condition block when the condition is not met, (:?> Out of the PHP mode and returns the HTML mode). Therefore, PHP will ignore it based on the conditions.
To output a large text segment, jumping out of the PHP parsing mode is usually more efficient than outputting the text via echo or print.

<? Php if (count ($ dataProvider-> getData ():?>
<Div id = "down" style = "display: block;">
<Input type = "button" id = "exportexcel" class = "button3" value = "Export Excel" url = "<? = Yii: app ()-> request-> getUrl ()?> ">
<Span id = "downloadlink"> </span> </div>
<? Php endif;?>


2. Command Separator

The end mark in a piece of PHP Code implicitly represents a semicolon
You can end the last line in a PHP code snippet without a semicolon.
If a new line is followed, the end mark of the code segment includes the end of the line.
<? Php
Echo "This is a test ";

<? Php echo "This is a test"?>

<? Php echo 'we omitted the last closing tag ';


PHP code segment end mark at the end of the file


In some cases, it is better to omit include or require so that unexpected blank characters will not appear at the end of the file, and then the response header can be output.
It is also convenient to use the output buffer, so you will not see unexpected blank spaces generated by the inclusion file.

<? Php if (0):?>
<? Php else:?>
Otherwise this will show.
<? Php endif;


<? Php if (0):?>
<? Php else:?>
Otherwise this will show.
<? Php endif?>


PHP comments


<? Php
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

A single-line comment is only commented to the end of the line or the current PHP code block, depending on which one appears first.


This means in //...?> Or #...?> The subsequent HTML code will be displayed.
:?> The PHP mode is jumped out and the HTML mode is returned. // or # does not affect this.

C-style comments end at the first.
<? Php
Echo "This is a test";/* This comment will cause a problem */


Echo 'dfjal ';
An error will be reported. The output is blank.


A string is composed of a series of characters, each of which is equivalent to a byte. This means that PHP can only support 256 = 2 to the power of the character set, so Unicode is not supported. For details, see the description of the string type.

Note: The maximum value of string is 2 GB.


A string can be expressed in four ways:

Single quotes
Double quotation marks
Heredoc syntax structure
Nowdoc syntax structure (from PHP 5.3.0)

Single quotes

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

To express a single quotation mark, you must add a backslash (\) before it to escape it.
To express a backslash itself, use two backslash (\\).
The backlash of any other method is considered as the backslash itself: that is to say, if you want to use other escape sequences such as \ r or \ n, it does not represent any special meaning, it is simply the two characters.

Note: Unlike double quotation marks and heredoc Syntax structures, escape sequences of variables and special characters in single quotation marks strings are not replaced.

<? Php
Echo 'this is a simple string ';

// You can enter multiple rows
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 quotation marks

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

Sequence meaning
\ N line feed (LF or 0x0A (10) In the ASCII character set ))
\ R press enter (CR or 0x0D (13) in the ASCII character set ))
\ T horizontal tab (HT or 0x09 (9) In the ASCII character set ))
\ V vertical tab (VT or 0x0B (11) In the ASCII character set) (from PHP 5.2.5)
\ E Escape (ESC or 0x1B (27) in the ASCII character set) (from PHP 5.4.0)
\ F form feed (FF or 0x0C (12) in the ASCII character set) (since PHP 5.2.5)
\ Backslash
\ $ USD mark
\ "Double quotation marks
\ [0-7] {} matches the regular expression sequence and is a character expressed in octal mode.
\ X [0-9A-Fa-f] {} matches the regular expression sequence and is a character expressed in hexadecimal notation.

Like a single quotation mark string, escaping any other character will cause the backslash to be displayed. Before PHP 5.1.1, The backslash in \ {$ var} is not displayed yet.

The most important feature of a string defined by double quotation marks is that the variable is parsed. For details, see variable parsing.


Heredoc Structure

The third way to express the string is to use the heredoc syntax structure: <. Provide an identifier after this operator, and then wrap the line. Next, use the string itself, and finally use the identifier defined above as the end mark.

The identifier referenced at the end must be in the first column of the row, and the name of the identifier must follow the PHP rules like other labels: Only letters, numbers, and underscores are allowed, it must start with a letter or underscore.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.