Tôi thực sự có một trường hợp sử dụng hợp lệ cho việc này.
Tôi có một hàm có thể kiểm soát được ($ var) (OK, tôi có bộ đệm chức năng ($ key, $ giá trị), nhưng tôi muốn có một chức năng như đã đề cập).
...
...
Nhưng điều này cũng thất bại ... :(
Chắc chắn, tôi có thể tiếp tục làm bộ nhớ cache ('màu', $ color), nhưng tôi lười biếng, bạn biết đấy ...;)
Vì vậy, những gì tôi muốn là một hàm có tên ban đầu của một biến, vì nó được chuyển đến một hàm. Bên trong chức năng không có cách nào tôi có thể biết điều đó, có vẻ như. Vượt qua get_defined_vars () bằng cách tham khảo trong ví dụ thứ hai ở trên đã giúp tôi (nhờ Jean-Jacques Guegan cho ý tưởng đó) phần nào. Hàm thứ hai bắt đầu hoạt động, nhưng nó vẫn chỉ tiếp tục trả về biến cục bộ ('biến', không phải 'màu').
Tôi chưa thử sử dụng get_func_args () và get_func_arg (), $ {}-Cấu trúc và khóa () kết hợp, nhưng tôi cho rằng nó cũng sẽ thất bại.
19 năm trước
Vwuhanqinb tại Gmail Dot Com ¶
Tại thời điểm này, hai biến đã được xác định và lưu trữ trong cây ký hiệu PHP: $ a với nội dung "Xin chào" và $ xin chào với nội dung "Thế giới". Do đó, tuyên bố này:
tạo ra đầu ra chính xác như:
tức là cả hai đều sản xuất: Xin chào Thế giới.hello world.
Để sử dụng các biến biến với các mảng, bạn phải giải quyết vấn đề mơ hồ. Đó là, nếu bạn viết $$ A [1] thì trình phân tích cú pháp cần biết liệu bạn có muốn sử dụng $ a [1] làm biến hay không, hoặc nếu bạn muốn $$ a làm biến và sau đó là chỉ mục [1] từ biến đó. Cú pháp để giải quyết sự mơ hồ này là: $ {$ a [1]} cho trường hợp đầu tiên và $ {$ a} [1] cho lần thứ hai.
Thuộc tính lớp cũng có thể được truy cập bằng tên thuộc tính biến. Tên thuộc tính biến sẽ được giải quyết trong phạm vi mà cuộc gọi được thực hiện. Chẳng hạn, nếu bạn có một biểu thức, chẳng hạn như $ foo-> $ Bar, thì phạm vi cục bộ sẽ được kiểm tra $ Bar và giá trị của nó sẽ được sử dụng làm tên của thuộc tính của $ foo. Điều này cũng đúng nếu $ Bar là một truy cập mảng.
Niềng răng xoăn cũng có thể được sử dụng, để phân định rõ ràng tên thuộc tính. Chúng hữu ích nhất khi truy cập các giá trị trong một thuộc tính chứa một mảng, khi tên thuộc tính được tạo từ nhiều phần hoặc khi tên thuộc tính chứa các ký tự không hợp lệ (ví dụ: từ json_decode () hoặc simplexml).json_decode() or SimpleXML).
Ví dụ #1 Ví dụ về thuộc tính biến
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
Ví dụ trên sẽ xuất ra:
Tôi là quán bar. Tôi là quán bar. Tôi là quán bar. Tôi là R.
I am bar.
I am bar.
I am r.
Cảnh báo
Xin lưu ý rằng các biến biến không thể được sử dụng với các mảng SuperGlobal của PHP trong các hàm hoặc phương thức lớp. Biến $this
cũng là một biến đặc biệt không thể được tham chiếu động.
userb at abertb dot org ¶ ¶
12 năm trước
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
//... and so on ...//
?>
Ẩn danh ¶ ¶
17 năm trước
// another session
0// another session
1// another session
2 Nathan Hammond ¶ ¶
14 năm trước
// another session
3// another session
4// another session
5// another session
6 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
// another session
7// another session
8// another session
9$myColour = getCachedVariable('colour');
0$myColour = getCachedVariable('colour');
1$myColour = getCachedVariable('colour');
2$myColour = getCachedVariable('colour');
3// another session
2 J. Dyer ¶ ¶
20 năm trước
$myColour = getCachedVariable('colour');
5$myColour = getCachedVariable('colour');
6$myColour = getCachedVariable('colour');
7$myColour = getCachedVariable('colour');
8$myColour = getCachedVariable('colour');
9function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
0function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
1function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
2function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
3 J. Dyer ¶ ¶
Tội lỗi ¶
function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
4function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
5function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
615 năm trước function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
7 ¶
Ở đây (Ta tại TA) [Iwonderr]
function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
8// another session
4function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
0function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
1 6 năm trước ¶
jefrey.sobreira [at] gmail [dot] com ¶
function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
2function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
3// another session
2 7 năm trước ¶
12 năm trước
function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
5function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
6function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
7// another session
2 Ẩn danh ¶ ¶
17 năm trước
function varName(&$var) {
$definedVariables = get_defined_vars();
$copyOfDefinedVariables = array();
foreach ($definedVariables as $variable=>$value) {
$copyOfDefinedVariables[$variable] = $value;
}
$oldVar = $var;
$var = !$var;
$difference = array_diff_assoc($definedVariables, $copyOfDefinedVariables);
$var = $oldVar;
return key(array_slice($difference, 0, 1, true));
}
9Ẩn danh ¶ ¶
20 năm trước
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
0
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
1
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
2
// another session
2
J. Dyer ¶ ¶
Tội lỗi ¶
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
4
15 năm trước ¶
function cacheVariable($variable) {
$key = ${$variable}; // This doesn't help! It only gives 'variable'.
// do some caching using suitable backend such as apc, memcache or ramdisk
}
7
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
5
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
6
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
7
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
8
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "\n";$arr = 'arr';
echo $foo->{$arr[1]} . "\n";?>
9 Ở đây (Ta tại TA) [Iwonderr] ¶
6 năm trước
$this
0
jefrey.sobreira [at] gmail [dot] com ¶ ¶
7 năm trước
$this
1
$this
2
$this
3
$this
4
$this
5
$this
6 Mason ¶ ¶
14 năm trước
$this
7
$this
8
// another session
2 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
0
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
1
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
2
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
3 J. Dyer ¶ ¶
12 năm trước
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
4
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
5
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
6
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
7
<?php//You can even add more Dollar Signs$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
8
// another session
2 Ẩn danh ¶ ¶
17 năm trước
//... and so on ...//
0
//... and so on ...//
1
//... and so on ...//
2
//... and so on ...//
3 Ẩn danh ¶ ¶
17 năm trước
//... and so on ...//
4
//... and so on ...//
5
//... and so on ...//
6
//... and so on ...//
7
//... and so on ...//
8
//... and so on ...//
9
// another session
2 Nathan Hammond ¶ ¶
14 năm trước
?>
1
?>
2
?>
3
?>
4
?>
5
?>
6
// another session
2 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
?>
8
?>
9
// another session
00// another session
01 J. Dyer ¶ ¶
14 năm trước
// another session
02// another session
03// another session
04// another session
05// another session
06// another session
07 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
// another session
08 J. Dyer ¶ ¶
Tội lỗi ¶
// another session
09// another session
10// another session
11// another session
12 15 năm trước ¶
20 năm trước
// another session
13// another session
14// another session
15// another session
16 J. Dyer ¶ ¶
20 năm trước
// another session
17// another session
18// another session
2 J. Dyer ¶ ¶
14 năm trước
// another session
20// another session
21// another session
22// another session
23// another session
24 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
// another session
25// another session
26// another session
2 J. Dyer ¶ ¶
6 năm trước
// another session
28// another session
29// another session
30 jefrey.sobreira [at] gmail [dot] com ¶ ¶
20 năm trước
// another session
31// another session
32// another session
33// another session
2 J. Dyer ¶ ¶
Tội lỗi ¶
// another session
35// another session
36// another session
2PHP |$ vs $$ toán tử $ var_name = "Hello World!";$ Var_name là một biến bình thường được sử dụng để lưu trữ một giá trị.Nó có thể lưu trữ bất kỳ giá trị nào như Integer, Float, Char, String, v.v. Mặt khác, $$ var_name được gọi là biến tham chiếu trong đó $ var_name là một biến bình thường.reference variable where $var_name is a normal variable.
Định nghĩa và cách sử dụng.Hàm is_String () kiểm tra xem một biến có thuộc loại loại hay không.Hàm này trả về true (1) nếu biến thuộc loại loại, nếu không nó sẽ trả về sai/không có gì.The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.