Đôi khi thuận tiện để có thể có tên biến có thể thay đổi. Đó là, một tên biến có thể được đặt và sử dụng động. Một biến bình thường được đặt với một câu lệnh như:
Một biến có thể lấy giá trị của một biến và coi đó là tên của một biến. Trong ví dụ trên, Hello, có thể được sử dụng làm tên của một biến bằng cách sử dụng hai dấu hiệu đô la. I E.
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
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
Nathan Hammond ¶ ¶
14 năm trước
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
<?php
$this
0
$this
1 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
$this
2
$this
3
$this
4
$this
5
$this
6
$this
7
$this
8
J. Dyer ¶ ¶
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
<?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 J. Dyer ¶ ¶
Tội lỗi ¶
<?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
9
//... and so on ...//
0
//... and so on ...//
1
15 năm trước //... and so on ...//
2 ¶
Ở đây (Ta tại TA) [Iwonderr]
//... and so on ...//
3
<?php
//... and so on ...//
5
//... and so on ...//
6 6 năm trước ¶
jefrey.sobreira [at] gmail [dot] com ¶
//... and so on ...//
7
//... and so on ...//
8
7 năm trước ¶
12 năm trước
?>
0
?>
1
?>
2
Ẩn danh ¶ ¶
17 năm trước
?>
4
Ẩn danh ¶ ¶
20 năm trước
?>
5
?>
6
?>
7
J. Dyer ¶ ¶
Tội lỗi ¶
?>
9
15 năm trước ¶
//... and so on ...//
2
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
0
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
1
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
2
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
3
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
4 Ở đây (Ta tại TA) [Iwonderr] ¶
6 năm trước
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
5
jefrey.sobreira [at] gmail [dot] com ¶ ¶
7 năm trước
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
6
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
7
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
8
It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:
9
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
0
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
1 Mason ¶ ¶
14 năm trước
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
2
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
3
Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
5
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
6
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
7
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
8 J. Dyer ¶ ¶
12 năm trước
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
This is apparent from the notes others have left, but is not explicitly stated.
9
0
1
2
3
Ẩn danh ¶ ¶
17 năm trước
5
6
7
8 Ẩn danh ¶ ¶
17 năm trước
9
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
0
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
1
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
2
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
3
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
4
Nathan Hammond ¶ ¶
14 năm trước
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
6
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
7
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
8
These are the scenarios that you may run into trying to reference superglobals dynamically. Whether or not it works appears to be dependent upon the current scope.
9
<?php
0
<?php
1
Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
<?php
3
<?php
4
<?php
5
<?php
6 J. Dyer ¶ ¶
Tội lỗi ¶
<?php
7
<?php
8
<?php
9
$this
00
$this
01
$this
02 15 năm trước ¶
14 năm trước
$this
03
Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
$this
04
$this
05
$this
06
$this
07 J. Dyer ¶ ¶
20 năm trước
$this
08
$this
09
$this
10
$this
11 J. Dyer ¶ ¶
20 năm trước
$this
12
$this
13
J. Dyer ¶ ¶
14 năm trước
$this
15
$this
16
$this
17
$this
18
$this
19 Antony Dot Gian hàng tại Nodomain Dot ở đây ¶ ¶
20 năm trước
$this
20
$this
21
J. Dyer ¶ ¶
6 năm trước
$this
23
$this
24
$this
25 jefrey.sobreira [at] gmail [dot] com ¶ ¶
7 năm trước
$this
26
$this
27
$this
28
Mason ¶ ¶
PHP tại Ianco dot co dot uk ¶
$this
30
$this
31
Một biến trong PHP là tên của vị trí bộ nhớ chứa dữ liệu. Trong PHP, một biến được khai báo bằng cách sử dụng $ Sign theo sau là tên biến. Cách chính để lưu trữ thông tin ở giữa chương trình PHP là sử dụng một biến.a name of memory location that holds data. In PHP, a variable is declared using $ sign followed by variable name. The main way to store information in the middle of a PHP program is by using a variable.
Toán tử độ phân giải phạm vi (còn được gọi là paamayim nekudotayim) hoặc theo cách đơn giản hơn, dấu hai chấm, là một mã thông báo cho phép truy cập vào các thuộc tính hoặc phương thức tĩnh hoặc được ghi đè của một lớp.Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
Php $ _Request là biến Super Global Php được sử dụng để thu thập dữ liệu sau khi gửi biểu mẫu HTML.Ví dụ dưới đây hiển thị một biểu mẫu có trường đầu vào và nút gửi.a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button.