百科狗-知识改变命运!

debug_zval_dump() - php 变量处理函数

是丫丫呀2年前 (2023-11-21)阅读数 30#技术干货
文章标签例程

debug_zval_dump()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

Dumps a string representation of an internal zend value to output

说明

debug_zval_dump(mixed $variable[,mixed$...]): void

Dumps a string representation of an internal zend value to output.

参数

$variable

The variable being evaluated.

返回值

没有返回值。

范例

Example #1debug_zval_dump()example


以上例程会输出:

&string(11) "Hello World" refcount(3)
Note:Beware therefcount

Therefcountvalue returned by this function is non-obvious in certain circumstances. For example, a developer might expect the above example to indicate arefcountof2. The third reference is created when actually callingdebug_zval_dump().

This behavior is further compounded when a variable is not passed todebug_zval_dump()by reference. To illustrate, consider a slightly modified version of the above example:

以上例程会输出:

string(11) "Hello World" refcount(1)

Whyrefcount(1)? Because a copy of$var1is being made, when the function is called.

This function becomes evenmoreconfusing when a variable with arefcountof1is passed (by copy/value):

debug_zval_dump() - php 变量处理函数

以上例程会输出:

string(11) "Hello World" refcount(2)

Arefcountof2, here, is extremely non-obvious. Especially considering the above examples. So what's happening?

When a variable has a single reference (as did$var1before it was used as an argument todebug_zval_dump()), PHP's engine optimizes the manner in which it is passed to a function. Internally, PHP treats$var1like a reference (in that therefcountis increased for the scope of this function), with the caveat thatifthe passed reference happens to be written to, a copy is made, but only at the moment of writing. This is known as "copy on write."

So,ifdebug_zval_dump()happened to write to its sole parameter (and it doesn't), then a copy would be made. Until then, the parameter remains a reference, causing therefcountto be incremented to2for the scope of the function call.

参见

  • var_dump()打印变量的相关信息
  • debug_backtrace()产生一条回溯跟踪(backtrace)
  • References Explained
  • » References Explained (by Derick Rethans)
"Fatal error: Call-time pass-by-reference has been removed ..." since PHP 5.something. So the is now invalid PHP code.
Actual Output debug_zval_dump()
example :1
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump("&$var1"); //output : string(12) "&Hello World" refcount(1) 
debug_zval_dump($var1);  //output : string(11) "Hello World" refcount(3)
debug_zval_dump(&$var1); //output : Parse error: syntax error, unexpected '&' i
example :2
$var1 = 'Hello World';
debug_zval_dump($var1);//string(11) "Hello World" refcount(3)
example : 3
$var1 = 'Hello World';
$var2 = 'who';
$var2 =& $var1;
debug_zval_dump($var1); //output ://string(11) "Hello World" refcount(3)
The add of "Call-time pass-by-reference" as E_DEPRECATED makes it impossible to get the real refcount without getting an error, since  and even  doesn't change anything.
If you're finding the interpretation of refcount confusing, the Xdebug extension offers a function similar to this one, but because the variable name is passed as a string, xdebug_debug_zval() doesn't scribble on the refcount with its own references to the zval.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}