Hướng dẫn name variable php
OverviewIn PHP, you can just put an extra Show Nội dung chính
While I wouldn't recommend it, you could even chain this behavior :
You can but are not forced to put
Using
It is nevertheless recommended to always use Differences between PHP5 and PHP7Another reason to always use In PHP7, dynamic variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the mix of special cases in PHP5. The examples below show how the order of evaluation has changed. Case 1 : $$foo['bar']['baz']
Case 2 : $foo->$bar['baz']
Case 3 : $foo->$bar['baz']()
Case 4 : Foo::$bar['baz']()
5.5.3. DiscussionThe previous example prints 103. Because $animal = 'turtles', $$animal is $turtles, which equals 103. Using curly braces, you can construct more complicated expressions that indicate variable names: $stooges = array('Moe','Larry','Curly'); $stooge_moe = 'Moses Horwitz'; $stooge_larry = 'Louis Feinberg'; $stooge_curly = 'Jerome Horwitz'; foreach ($stooges as $s) { print "$s's real name was ${'stooge_'.strtolower($s)}.\n"; } Moe's real name was Moses Horwitz. Larry's real name was Louis Feinberg. Curly's real name was Jerome Horwitz. PHP evaluates the expression between the curly braces and uses it as a variable name. That expression can even have function calls in it, such as strtolower( ). Variable variables are also useful when iterating through similarly named variables. Say you are querying a database table that has fields named title_1, title_2, etc. If you want to check if a title matches any of those values, the easiest way is to loop through them like this: for ($i = 1; $i <= $n; $i++) { $t = "title_$i"; if ($title == $$t) { /* match */ } } Of course, it would be more straightforward to store these values in an array, but if you are maintaining old code that uses this technique (and you can't change it), variable variables are helpful. The curly brace syntax is also necessary in resolving ambiguity about array elements. The variable variable $$donkeys[12] could have two meanings. The first is "take what's in the 12th element of the $donkeys array and use that as a variable name." Write this as: ${$donkeys[12]}. The second is, "use what's in the scalar $donkeys as an array name and look in the 12th element of that array." Write this as: ${$donkeys}[12]. PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. Utilize wrapper functions to make use of any of these constructs as variable functions. Example #1 Variable function example
bar($arg = '') Object methods can also be called with the variable functions syntax. Example #2 Variable method example
Bar() When calling static methods, the function call is stronger than the static property operator: Example #3 Variable method example with static properties
Foo::$variable; // This prints 'static property'. It does need a $variable in this scope. Example #4 Complex callables
niemans at pbsolo dot nl ¶ 3 years ago
Anonymous ¶ 11 years ago
rnealxp at yahoo dot com ¶ 2 years ago
interface iface_dynamic_members{ anisgazig at gmail dot com ¶ 6 months ago
$a = new myCls; Anonymous ¶ 2 years ago
= $c->j(); josh at joshstroup dot xyz ¶ 6 years ago
|