PHP variable identifiers

Source: Internet
Author: User
Some rules of PHP variable identifiers provide very convenient PHP variable identifiers. However, many PHP programmers who have been engaged in PHP for many years cannot understand the specific usage of PHP, so they may not know where to make a mistake. So, let's make a summary. It is easy for you to quickly master. The running mechanism of PHP variables is to use the string after the variable identifier $ or the string of the expression operation result as some rules for the variable name PHP variable identifier.

The PHP variable identifier provides great convenience. However, many PHP programmers who have been engaged in PHP for many years cannot understand the specific usage of PHP, so they may not know where to make a mistake. So, let's make a summary. It is easy for you to quickly master.
The running mechanism of PHP variables is to take the string after the variable identifier $ or the string of the expression calculation result as the variable name and get the variable value from the variable pool.
It can be seen that PHP provides a variable "name pointer ". It is different from the address pointer of C ++ because it does not have any insecure addresses. There will be no problem of variable memory overflow. All these PHP scripts are complete.
Variable identifier:
$: The subsequent string is the variable name and the variable with the same name is used.
?? $ A = 'hello ';
?? $ A = 'world ';
{} Parses the expression into a string and obtains the variable of this string. {} cannot exist independently outside the expression. Otherwise, an error is reported as a process control error. This means that $ is required before {}, or quotation marks are required outside.
Echo $ a, $ {$ a}; echo $ a, $;

The difference between the two: $ always looks for the first string after it, and {} parses the internal expression into a string. PHP uses this method to implement the variable "name pointer ".

$ {}: The returned string is handed over to $ for further processing. Purpose: take a variable for the expression result.
? For example, $ {$ array [$ I] [$ j]}. if $ array [$ I] [$ j]} is used, PHP will find the variable $ array. Instead of looking for a variable whose name is $ array [$ I] [$ j.

{} In the function and expression
Guess: what is returned by the following program:

   $a='Hello';   $$a = 'world';   echo '1 ', $a, '{$a}', '
'; echo '2 ', $a, "{$a}", '
'; echo '3 ', $a, "{{$a}}", '
'; echo '4 ', $a, "${$a}", '
'; echo '5 ', $a, ${$a}, '
'; echo '6 ', $a, "{${$a}}", '
'; echo '7 ', $a, "{{${$a}}}", '
'; echo '8 ', $a, "$$a", '
'; echo '9 ', $a, "{a}", '
'; echo '10 ', $a, "${a}", '
';

?

Result:

?? Echo '1', $ a, '{$ }','
';
The output is: 1 Hello {$ }?

// Under normal conditions, single quotes are non-execution strings and are returned based on the original results. However, Smarty will also parse it !! Therefore, expressions like '{$ a}' in the Smarty template may still show results you don't want !!
Suppose: str_replace ('{$ foo}', $ foo, '{$ foo}. some ');
For the above reason: you need to change it:
? Str_replace (array ('{', '$ Foo',}'), array ('', $ foo,''), '{$ foo}. some ');

?? Echo '2', $ a, "{$ }",'
';
The output is: 2 HelloHello
Variables are always parsed regardless of whether {} exists in double quotation marks.
?? Echo '3', $ a, "{{$ }}",'
';??
The output is: 3 Hello {Hello }?
// Therefore, if you want to output results with {}, you need to add two layers.
?? Echo '4', $ a, "$ {$ }",'
';?
Output: 4 Helloworld
?? Echo '5', $ a, $ {$ },'
';
Output: 4 Helloworld
// $ {$ Expression}. The result is the same regardless of whether the double quotation marks are included on the outside?

?? Echo '6', $ a, "{$ }}",'
';?
Output: 6 Helloworld
?? Echo '7', $ a, "{{$ {$ }}}",'
';
Output: 7 Hello {world}
// To output results with {}, an additional layer is required.
Echo '8', $ a, "$ ",'
';
Output: 8 Hello $ Hello
// $ In double quotation marks is executed only once. Therefore, the result is not what you want.

Note: {} can also be used as an array to access the array subscript. That is
$ Array [$ I] [$ j] is equivalent to $ array {$ I} {$ j. But it is normal because the PHP document provides []. The string is a byte array, so we only use {} to access the string in byte array mode {}
$ A = 'ux: cache'; $ a {2} = ''; echo $ a; what is the result, do you know?
Echo '9', $ a, "{}",'
';
Output: 9 Hello {}
?? Echo '10', $ a, "$ {}",'
';
The output is: 10 HelloHello. {} has $, and {} will send the result to $ resolution.

Summary:
$ In double quotation marks is parsed only once. No multi-resolution is performed. Change the echo "$ a" to echo "$ {$ }";
{} Will be parsed when $ becomes {$ foo. To output results with {}, {$ foo} is required }}
$ Is a string to be searched backward. Therefore, the result of an array or expression: no
$ Array [$ I] [$ j] instead of $ {$ array [$ I] [$ j]}

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.