PHP syntax rules quick start _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Introduction to PHP syntax rules. 1. simple syntax rules (variable names are defined using curly brackets, applicable to all PHP versions): Copy the code as follows: $ aflower; echoShereceivedsome $ as; invalid; the letter s will be treated as 1. simple syntax rules (variable names are defined using curly brackets, applicable to all PHP versions ):

The code is as follows:


$ A = 'flower ';
Echo "She has ed some $ as"; // invalid; the letter s is regarded as a valid variable name component element, but the variable here is $
Echo "She has ed some $ {a} s"; // valid
Echo "She has ed some {$ a} s"; // valid; recommended usage


What we want to express is that "she receives some flowers", and the flower in the context should adopt the plural form (that is to say, S should be added later), but if we do not define the variable, the first echo is displayed. Obviously, we want to output $ a instead of $. So how do we deal with this output?

The code is as follows:


Echo "She has ed some $ a". "s ";
Echo "She has ed some". $ a. "s"; // do you think these two habits are concise and clear without curly braces?


Note: No matter whether it is in front or back of $, curly braces are considered as defining symbols only when the two are close to each other. Do not add spaces between them. Otherwise, they will be processed as normal curly braces.

Echo "She has ed some {$ a} s"; // The output result is: She has ed some {flower} s


2. complex syntax rules (use curly brackets to define expressions, etc., with PHP4 + ):

The code is as follows:


Echo "effective syntax: {$ arr [4] [3]}"; // valid; defines multi-dimensional arrays
Echo "effective syntax: {$ arr ['foo'] [3]}"; // valid; when using a multi-dimensional array in a string, enclose it with parentheses.
Echo "valid syntax: {$ this-> width} 00"; // valid; otherwise, it will become $ this-> width00
Echo "valid syntax: {$ this-> value [3]-> name}"; // valid; this example demonstrates defining chained calls
Echo "valid syntax: $ name :{$ {$ name}"; // valid; the effect demonstrated in this example is actually a variable.
Echo "valid syntax: {$ {getName ()}"; // valid; this example demonstrates using the return value of the function as the variable name
Echo "valid delivery: {$ this-> getName ()}"; // valid; this example demonstrates using the return value of the function as the variable name


Note 1: echo "is this write valid: {getName ()}"; the output result is: 'Is this write valid:
{GetName ()}'. Because it does not contain $, curly brackets are not considered as delimiters.
Note 2: echo "is this valid? {$ arr [foo] [3]}"; before answering this question, let's start an experiment:

The code is as follows:


Error_reporting (E_ALL );
$ Arr = array ('A', 'B', 'C', 'D' => 'e ');
Echo "This is $ arr [d]"; // if we find that writing like This is okay, what about writing like below?
Echo $ arr [d];


This error occurs:
Notice: Use of undefined constant d-assumed 'D'
Note: an undefined constant d may be used as 'D'
If we modify the code as follows:

The code is as follows:


Error_reporting (E_ALL );
$ Arr = array ('A', 'B', 'C', 'D' => 'e ');
Define ('F', 'D ');
Echo $ arr [f];


We found no problem this time. It can be seen that there is no problem in adding single quotation marks to the index of the array in the string, but if this writing method does not appear in the string, an error will be reported, in the string, {$ arr [foo] [3]} is parsed in a non-string manner. Therefore, it is wrong to add only curly brackets to the array in the string but not to add single quotation marks to the index. Because the program will parse the index without single quotes as a constant, this produces an error. The correct statement should be:

Echo "valid syntax: {$ arr ['foo'] [3]}";

Note: echo "This is $ arr [d]"; This method can be parsed by the program, but it is only applicable to the case where the array is a one-dimensional array. The rigorous statement should be echo "This is {$ arr ['D']}". my students once argued with me at This point. He said: since the previous method can produce results, why must it be followed? Then, let's continue to modify the previous code.

The code is as follows:


Error_reporting (E_ALL );
$ Arr = array ('A', 'B', 'C', 'D' => array ('E' => 'F '));
Echo "This is $ arr [d] [e]";


Can this be correctly parsed? I just want to tell you that curly braces are strictly necessary. Of course, if you are not a student of mine, I cannot handle that much ......

Note 3:

The code is as follows:


Error_reporting (E_ALL );
$ Arr = array ('A', 'B', 'C', 'D ');
Echo "This is {$ arr [2]}
";
Echo "This is {$ arr ['2']}
";


Execute the above code. The results are the same. why? I can only tell you that PHP is a weak language. I will not talk about it here. Google it yourself. After talking about this, where can we best apply the advantages of these syntactic rules? ---- SQL statement

The code is as follows:


// Example 1:
$ SQL1 = "select * from table where id = {$ _ GET ['id']}"; // Example 2:
$ SQL2 = "select * from table where id = {$ this-> id }";


OK. We will be playing here.

Variable (variable names are defined in curly brackets, applicable to all PHP versions): The code is as follows: $ a = 'flower'; echo "She has ed some $ as"; // invalid; the letter s will be treated...

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: info-contact@alibabacloud.com 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.