Explanation of PHP syntax rules, explanation of php syntax _ PHP Tutorial

Source: Internet
Author: User
Explanation of PHP syntax rules and php syntax. Explanation of PHP syntax rules and explanation of php syntax 1. simple syntax rules (variable names are defined in curly brackets, applicable to all PHP versions): $ aflower; echoShereceivedsome $ as; invalid; explanation of PHP syntax rules, and explanation of php syntax

1. simple syntax rules (variable names are defined using curly brackets, applicable to all PHP versions ):

$ A = 'flower'; echo "She has ed some $ as"; // invalid; the letter s is regarded as a valid variable name component element, however, the variable here is $ aecho "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?

Echo "She has ed some $ ". "s"; echo "She has ed some ". $. "s"; // These two habitual writing methods should be 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 + ):

Echo "valid syntax: {$ arr [4] [3]}"; // valid; define multi-dimensional array echo "valid syntax: {$ arr ['foo'] [3]} "; // valid; when using a multi-dimensional array in a string, enclose it in parentheses. echo" valid syntax: {$ this-> width} 00 "; // valid; if not defined, it will become $ this-> width00echo" effective syntax: {$ this-> value [3]-> name} "; // valid; this example demonstrates how to define the chain call echo" effective syntax: $ name: {$ name} "; // valid; the result of this example is actually a variable echo" valid syntax: {$ {getName ()}}"; // valid; in this example, the return value of the function is used 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:

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:

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.

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:

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

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

Lifecycle 1. simple syntax rules (variable names are defined using curly brackets, applicable to all PHP versions): $ a = 'flower'; echo "She has ed some $ as"; // invalid ;...

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.