php interview topics --- 1, php variable storage principle and referenced

php interview topics --- 1, php variable storage principle and referenced

A summary

Sentence summary:

See variable storage structure may be mounted xdebug extended with xdebug_debug_zval () method of memory_get_usage not recommended () method

 

1. What is the role php in memory_get_usage () method is?

Returns the amount of memory allocated to PHP

 

2. What is the reference variable? In PHP them, what symbol is defined by reference variables? ?

Access the same variable content by different names: reference means to access the same variable content by different names in PHP.
Use ampersand

 

3. Note that the code examples php variable storage mechanism?

|||-begin

// definition of a variable 
$ A = Range (0,1000 );
 var_dump (of memory_get_usage ());

// define the variable b, the value of a variable is assigned to b
//cow Copy On Write 
Sb=$avar_dump(memory_get_usage());

// of a modified 
Sa = Range (E, 1000 );
 var_dump (of memory_get_usage ());

result
int(369184)
int ( 369 224) // and on a very close 
int (513728)

|||-end

cow: Copy On Write: Sb = $ a time when, Sb and $ a a shared memory, until $ a re-write operation, although the values ​​are the same, it has been pointed to different regions
memory_get_usage (): Returns the amount of memory allocated for PHP
When Sb = & $ a; when, $ a $ b and always point to the same space

 

 

 

4, xdebug extended print variable structure instance attention?

// the zval variable container 
$ A = Range (E,. 3 );
xdebug_debug_zval('a');

a:(refcount=1,is_ref=0)=array(0=>
(refcount=1,is_ref=0)=0,1=>(refcount=
1,is_ref=0)=1,2=>(refcount=1,is_ref=
0)=2,3=>(refcount=1,is_ref=0)=3)
refcount represents the variable pointing to this space: for example, after the Sb = $ a, such a $ a refcount is 2, then modified if $ a, $ a $ b and 1 are the refcount
is_ref indicating whether the referenced: for example, after Sb = & $ a, such a $ a refcount is 2, is_ref is 1, $ b is the same

 

5. What is the role unset, unset memory space will destroy it?

|||-begin

? < PHP
 // unset only dereference, will not destroy the space 
$ A = 1 ;
 $ b = & $ A ;
 unset ( $ b );
 echo  $ A "\ the n-";. // result was 1

|||-end

unset will only dereference, will not destroy space

 

6, the object is referenced in the assignment php, or do as copy on write as ordinary variables?

|||-begin

<?php

// object itself is passed by reference 
class the Person
{
    public $name = "zhangsan";
}

$p1 = new Person;
xdebug_debug_zval('p1');

$p2 = $p1;
xdebug_debug_zval('p1');

$p2->name = "lisi";
xdebug_debug_zval('p1');

|||-end

Object itself is passed by reference, but both $ p2 = $ p1 to point to the same space (even where a modified value), but is_ref (reference) 0 or a variable

 

 

 

7, following the results of the program is how much, and why?

|||-begin

<?php

/**
 * Output of a program written as follows
 * <?php
 *
 * $data = ['a', 'b', 'c'];
 *
 * foreach($data as $key => $val)
 * {
 *      $val = &$data[$key];
 * }
 * The program is running, each time after the end of the loop variable $ What is the value of data? please explain
 * After the program execution is completed, the value of $ data what? please explain
 */

$data = ['a', 'b', 'c'];

the foreach has to ( $ data, and is  is derived, as  $ key and => $ of the Val )
{
    $val = &$data[$key];
    var_dump($data);
}

var_dump ( $ data );

|||-end

[a,b,c],[b,b,c],[b,c,c],[b,c,c]
Paint Analysis: $ val = & $ data [$ key]; shows a $ data [$ key] to the address $ val

 

 

 

 

 

 

Second, content in summary

 

 

 

Reproduced in: https: //www.cnblogs.com/Renyi-Fan/p/11058450.html

Guess you like

Origin blog.csdn.net/weixin_34050519/article/details/93572352