PHP sentence rules are detailed

Source: Internet
Author: User

1. Simple syntax rules (use curly braces to define variable names for all versions of PHP):

=' flower ';" She received some $as ";  The letter S will be considered as a valid variable name, but here the variable is $ A"She received some ${a}s";  Effective "She received some {$a}s";  The recommended way to use          


What we want to express is "she received some flowers", the context of the flower should be in the plural (that is, should be appended with s), but if the variables do not define anything, there will be the first echo case. Obviously we want to output a $ A rather than a $as. So what do we usually do with this output?

"She received some $a". S ";" She received some ".  $a." S ";  These two habitual writing should not add curly braces to the wording of the simple and clear it? 


Note: Regardless of whether {it appears before or after, only the curly braces will be considered as delimiting symbols when they are next to each other. Do not add spaces between them, otherwise they will be treated as ordinary curly braces.

"She received some {$a}s";  The result of the output is: She received some {flower}s 



2. Complex syntactic rules (using curly braces to define expressions, etc., used with php4+):

Echo"Valid notation: {$arr [4][3]}";//valid; define multidimensional arrays echo  "valid notation: {$arr [' foo ' class=]}" //valid; When using a multidimensional array in a string, be sure to enclose it in parentheses echo ; If not defined, it becomes $this->width00echo  "valid notation: {$this->value[3]->name} "//valid; This example demonstrates the definition of a chained call echo  "valid notation: $name: {class= $name}}" //valid; the effect of this example is actually a mutable variable echo  "valid notation: {${getname ()}}" //valid; This example shows the return value of the function as the variable name echo //valid; This example shows the return value of a function as a variable name            


note 1:echo "Is this write valid: {getName ()}"; The output is: ' Is this write valid:
{getName ()} '. Because it does not contain $, the curly brackets are not used as the delimiter
Note 2:echo "is this valid: {$arr [foo][3]}"; Before we answer this question, let's start with an experiment:

error_reporting (e_all  $arr = Array ( ' a ' , ' B ' , ' C '   ' d ' => ' E ' );  "This is $arr [d]" ; We find it is no problem to write this, so what do we write like this? echo $arr [d;       


produced such an error:
Notice:use of undefined constant d-assumed ' d '
Note: An undefined constant d may be used as ' d '
So if we change the code like this,

error_reporting (e_all  $arr = Array ( ' a ' , ' B ' , ' C '   ' d ' => ' E ' );  ( ' F ' , ' d ' echo $arr [f;       


We found no problem with this time. You can see that the index of the array in the string does not have a problem, but if the notation does not appear in the string, the parsing of {$arr [foo][3]} in the string is parsed in a non-string manner. So it is wrong to say that an array is only enclosed in curly braces in a string and does not add single quotes to an index. This creates an error because the program interprets the non-single-quoted index as a constant. The correct wording should be:

"Valid notation: {$arr [' foo '][3]}";


A special reminder: echo "This is $arr [d]"; Although this notation can be parsed by the program, it is limited to the case that the array is a one-dimensional array. The rigorous wording should be: echo "This is {$arr [' d ']}"; My students have argued with me on this point, and he said: "Since the previous one can produce results, why must we use the latter one?" So, let's go ahead and revise the previous code.

error_reporting (e_all  $arr = Array ( ' a ' , ' B ' , ' C '   ' d ' =>array ( ' E ' Span class= "pun" >=> ' F ' ) );  "This is $arr [d][e]"          


Can this also be parsed correctly? I just want to tell you that the curly braces are strictly necessary. Of course, if you are not my student then I can not control so much ...

Note 3:

error_reporting (e_all  $arr = Array ( ' a ' , ' B ' , ' C '   ' d ' ); echo  "This is {$arr [2]} <br/>" ; "This is {$arr [' 2 ']} <br/>"        


Execute the above code. The result is the same, why is it so? I can only tell you that PHP is a weakly typed language, as to what is called a weakly typed language I am not here to say more. Go to Google for yourself. With so much to say, where is the specific application of the advantages of these sentence rules? ----SQL statements

Example one:="SELECT * from table where id={$_get[' id '}";  Example two:="SELECT * from table where id={$this->id}";      

PHP sentence rules are detailed

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.