Hướng dẫn what is reference operator php? - toán tử tham chiếu php là gì?

Dave tại SymmetricDesigns Dot Com ¶

14 năm trước

Another example of something to watch out for when using references with arrays.  It seems that even an usused reference to an array cell modifies the *source* of the reference.  Strange behavior for an assignment statement (is this why I've seen it written as an =& operator?  - although this doesn't happen with regular variables).
    $array1 = array(1,2);
   
$x = &$array1[1];   // Unused reference
   
$array2 = $array1// reference now also applies to $array2 !
   
$array2[1]=22;      // (changing [0] will not affect $array1)
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 22    // var_dump() will show the & here
    )

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )

Ivan tại mailinator dot com

13 năm trước

A little gotcha (be careful with references!):

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:

Array
(
    [a] => first
    [b] => second
    [c] => second
)

Add 'unset($a)' between the foreachs to obtain the 'correct' output:

Array
(
    [a] => first
    [b] => second
    [c] => third
)

Carlos ¶

17 năm trước

in the example below, you would get the same result if you change the function to something like:

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
0

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
1

gnuffo1 tại gmail dot com

12 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
3

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
4

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
5

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
6

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
7

midir ¶

13 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
9

0

1

Carlos ¶

17 năm trước

3

4

5

6

gnuffo1 tại gmail dot com

12 năm trước

8

9

A little gotcha (be careful with references!):0

A little gotcha (be careful with references!):1

midir ¶

17 năm trước

A little gotcha (be careful with references!):2

A little gotcha (be careful with references!):3

A little gotcha (be careful with references!):4

gnuffo1 tại gmail dot com

14 năm trước

A little gotcha (be careful with references!):6

A little gotcha (be careful with references!):7

A little gotcha (be careful with references!):8

A little gotcha (be careful with references!):9

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
0

12 năm trước

14 năm trước

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
2

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
3

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
4

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
5

midir ¶

14 năm trước

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
7

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
8

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:
9

Array
(
    [a] => first
    [b] => second
    [c] => second
)
0

Array
(
    [a] => first
    [b] => second
    [c] => second
)
1

Array
(
    [a] => first
    [b] => second
    [c] => second
)
2

Iryoku tại Terra Dot es ¶

18 năm trước

Array
(
    [a] => first
    [b] => second
    [c] => second
)
4

Array
(
    [a] => first
    [b] => second
    [c] => second
)
5

Array
(
    [a] => first
    [b] => second
    [c] => second
)
6

Array
(
    [a] => first
    [b] => second
    [c] => second
)
7

Array
(
    [a] => first
    [b] => second
    [c] => second
)
8

Alexander tại Gamerev Dot org ¶

17 năm trước

Array
(
    [a] => first
    [b] => second
    [c] => second
)
9

Add 'unset($a)' between the foreachs to obtain the 'correct' output:0

Add 'unset($a)' between the foreachs to obtain the 'correct' output:1

Add 'unset($a)' between the foreachs to obtain the 'correct' output:2

Add 'unset($a)' between the foreachs to obtain the 'correct' output:3

gnuffo1 tại gmail dot com

17 năm trước

Add 'unset($a)' between the foreachs to obtain the 'correct' output:4

Add 'unset($a)' between the foreachs to obtain the 'correct' output:5

Add 'unset($a)' between the foreachs to obtain the 'correct' output:6

Add 'unset($a)' between the foreachs to obtain the 'correct' output:7

Add 'unset($a)' between the foreachs to obtain the 'correct' output:8

Add 'unset($a)' between the foreachs to obtain the 'correct' output:9

Array
(
    [a] => first
    [b] => second
    [c] => third
)
0

gnuffo1 tại gmail dot com

17 năm trước

Array
(
    [a] => first
    [b] => second
    [c] => third
)
2

Array
(
    [a] => first
    [b] => second
    [c] => third
)
3

gnuffo1 tại gmail dot com

14 năm trước

Array
(
    [a] => first
    [b] => second
    [c] => third
)
5

Array
(
    [a] => first
    [b] => second
    [c] => third
)
6

Array
(
    [a] => first
    [b] => second
    [c] => third
)
7

Array
(
    [a] => first
    [b] => second
    [c] => third
)
8

Array
(
    [a] => first
    [b] => second
    [c] => third
)
9

0

12 năm trước

midir ¶

2

3

Add 'unset($a)' between the foreachs to obtain the 'correct' output:1

5

6

Iryoku tại Terra Dot es ¶

18 năm trước

7

8

9

Alexander tại Gamerev Dot org ¶

8 năm trước

Another example of something to watch out for when using references with arrays.  It seems that even an usused reference to an array cell modifies the *source* of the reference.  Strange behavior for an assignment statement (is this why I've seen it written as an =& operator?  - although this doesn't happen with regular variables).
    $array1 = array(1,2);
   
$x = &$array1[1];   // Unused reference
   
$array2 = $array1// reference now also applies to $array2 !
   
$array2[1]=22;      // (changing [0] will not affect $array1)
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 22    // var_dump() will show the & here
    )

in the example below, you would get the same result if you change the function to something like:2

in the example below, you would get the same result if you change the function to something like:3

in the example below, you would get the same result if you change the function to something like:4

in the example below, you would get the same result if you change the function to something like:5

in the example below, you would get the same result if you change the function to something like:6

Iryoku tại Terra Dot es ¶

midir ¶

in the example below, you would get the same result if you change the function to something like:8

in the example below, you would get the same result if you change the function to something like:9

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
00

Iryoku tại Terra Dot es ¶

18 năm trước

Alexander tại Gamerev Dot org ¶

8 năm trước

18 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
03

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
04

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
05

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
06

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
07

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
08

Alexander tại Gamerev Dot org ¶

18 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
10

Alexander tại Gamerev Dot org ¶

8 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
11

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
12

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
13

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
14

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
15

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
16

Nathan ¶

12 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
18

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
19

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
20

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
21

midir ¶

14 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
23

Iryoku tại Terra Dot es ¶

12 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
24

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
25

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
26

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
27

midir ¶

17 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
28

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
29

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
30

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
31

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
32

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
33

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
34

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
35

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
36

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
37

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
38

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
39

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
40

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
41

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
42

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
43

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
44

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
45

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
46

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
47

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
48

Iryoku tại Terra Dot es ¶

17 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
50

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
51

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
52

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
53

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
54

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
55

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
56

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
57

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
58

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
59

gnuffo1 tại gmail dot com

12 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
61

Array
(
    [a] => first
    [b] => second
    [c] => second
)
7

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
63

midir ¶

17 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
64

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
65

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
66

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
67

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
68

Iryoku tại Terra Dot es ¶

18 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
69

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
70

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
71

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
72

Alexander tại Gamerev Dot org ¶

17 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
73

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
74

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
75

Add 'unset($a)' between the foreachs to obtain the 'correct' output:1

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
77

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
78

8 năm trước

8 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
79

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
80

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
81

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
82

Nathan ¶

14 năm trước

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
83

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
84

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
85

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
86

I fixed my bug by rewriting the code without references, but it can also be fixed with the unset() function:
    $array1 = array(1,2);
   
$x = &$array1[1];
   
$array2 = $array1;
    unset(
$x); // Array copy is now unaffected by above reference
   
$array2[1]=22;
   
print_r($array1);
?>
Produces:
    Array
    (
    [0] => 1
    [1] => 2
    )
87

Php có vượt qua bằng tài liệu tham khảo không?

vượt qua tham chiếu.Rõ ràng, PHP, giống như C ++, là một ngôn ngữ hỗ trợ vượt qua tham chiếu.Theo mặc định, các đối tượng được truyền bởi giá trị.PHP, like C++, is a language that does support pass by reference. By default, objects are passed by value.

& $ Có nghĩa là gì trong PHP?

Nó có nghĩa là bạn chuyển một tham chiếu đến chuỗi vào phương thức.Tất cả các thay đổi được thực hiện cho chuỗi trong phương thức cũng sẽ được phản ánh bên ngoài phương thức đó trong mã của bạn.you pass a reference to the string into the method. All changes done to the string within the method will be reflected also outside that method in your code.

Các toán tử gán trong PHP là gì?

Các toán tử gán PHP được sử dụng với các giá trị số để ghi một giá trị vào một biến.Toán tử gán cơ bản trong PHP là "=".Nó có nghĩa là toán hạng bên trái được đặt thành giá trị của biểu thức gán ở bên phải.used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Là loại tham chiếu mảng trong PHP?

Liên quan đến câu hỏi đầu tiên của bạn, mảng được truyền qua tham chiếu trừ khi nó được sửa đổi trong phương thức / hàm bạn đang gọi.Nếu bạn cố gắng sửa đổi mảng trong phương thức / hàm, một bản sao của nó được tạo trước tiên và sau đó chỉ có bản sao được sửa đổi.the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.