The rules of the PHP sentence

Source: Internet
Author: User
Tags define array constant modify return variable valid

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


$a = ' flower ';


echo "She received some $as";


//invalid; the letter S will be used as a valid variable name element, but the variable here is $a


echo "She received some ${a}s"; Effective


echo "She received some {$a}s"; Effective; Recommended Use method


we want to express "she received some flowers", the flower in context should be in the plural (that is, it should be followed by s), but if you do not define the variable, the first echo will appear. Obviously we want the output to be $a rather than $as. So how do we usually handle this output?


echo "She received some $a". S ";


echo "She received some". $a. " S ";


//These two habitual ways of writing should be simple and concise without curly braces?


Note: Whether {It appears in front of the $ or behind it, only the curly braces are considered as defining symbols if they are next to each other. Do not add spaces between them, or they will be treated as normal curly braces


echo "She received some {$a}s";

The result of
//output is: She received some {flower}s





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


echo "valid notation: {$arr [4][3]}";


//effective; define multidimensional array


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


//valid; When you use a multidimensional array in a string, be sure to enclose it in parentheses


echo "Effective writing: {$this->width}00";


//effective; if not defined, it becomes $this->width00


echo "valid notation: {$this->value[3]->name}";


//Effective; This example illustrates the definition of a chained call


echo "Valid wording: $name: {${$name}}";


//Valid; This example demonstrates an effect that is actually a variable variable


echo "valid notation: {${getname ()}}";


//Valid; This example demonstrates the return value of a function as a variable name


echo "effectively issued: {${$this->getname ()}}";


//Valid; This example demonstrates the return value of a function as a variable name


Note 1:echo "This write is valid: {GetName ()}"; The output is: ' Does this work:


{getName ()} '. Because the inside does not contain $, the curly braces are not treated as a qualifier


Note 2:echo "This write valid: {$arr [foo][3]}"; Before answering this question, let's start with an experiment:


error_reporting (E_all);


$arr = Array (' A ', ' B ', ' C ', ' d ' => ' e ');


echo "This is $arr [d]";


//We find that writing is no problem, so what do we write as follows?


echo $arr [d];


produced such a mistake:


notice:use of undefined constant d-assumed ' d '


Note: The undefined constant d should probably be used for ' d '


So 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 there was no problem this time. You can see that the index of an array in a string is no problem without a single quotation mark, but if it does not appear in the string, an error occurs, and the parsing of {$arr [foo][3]} in the string is parsed in a way that is not a string. So it's wrong to say that in a string, the array is delimited only with curly braces but not the index plus single quotes. This creates an error because the program parses the index without the single quotation mark as a constant. The correct wording should be:


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


a special reminder that the echo "This is $arr [d]"; Although this writing can be resolved by the program, it is limited to the case that the array is a one-dimensional array. The rigorous writing should be: echo "This is {$arr [' d ']}"; My student once argued with me at this point, he said: "Since the previous writing can produce results, why must use the latter one?" So, let's go ahead and 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 resolved correctly? I just want to tell you that curly braces are strictly necessary. Of course, if you're not my student then I don't care so much ...





Note 3:


error_reporting (E_all);


$arr = Array (' A ', ' B ', ' C ', ' d ');


echo "This is {$arr [2]}";


echo "This is {$arr [' 2 ']}";


executes the above code. The result is the same, why is this so? I can only tell you that PHP is a weak type language, and I'm not going to say anything about weak-type languages. Go Google yourself. Said so much, then the most embodiment of these rules and regulations are the advantages of the specific application where? ----SQL Statement


//Example one:


$SQL 1 = "SELECT * from table where id={$_get[' id ']}";


//Example two:


$SQL 2 = "SELECT * from table where id={$this->id}";







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.