-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-assignment.php
29 lines (24 loc) · 1.06 KB
/
02-assignment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/**
* Variable assignment
*
* PHP will assign values to many variables with a reference. This allows
* multiple variables to share a space in memory; however these are not actually
* referenced variables. When a value changes, PHP will create a copy in memory
* to track the now-divergent values; a process called called copy-on-write.
*
* The example below illustrates reference counting using strings. In PHP 7
* static strings and immutable arrays are stored as interned values and are
* more performant. The lesson below is still valid even though the simple
* example produces different results in PHP 7.
*/
// Variable assignment.
echo "Creating variable through simple assignment.\n";
$a = 'Time'; // Create a variable $a.
$b = $a; // Assignment shares memory value in memory, refcount++
$c = $b; // Assignment shares memory value in memory, refcount++
xdebug_debug_zval('a', 'b', 'c');
// Altering one of the variables.
echo "\n\nChanging one of the variables will copy the value on write.\n";
$b = 'Space';
xdebug_debug_zval('a', 'b', 'c');