Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use zval in PHP?

Tags:

variables

php

Could you suggest me any practical example showing how to make use zval containers? The only related function I know is debug_zval_dump, but I never really used it.

Edit:

I suppose, tracking zval containers I could see how to optimize the code, see how the memory is used by references. It seems it could be useful tool in some cases. There is certainly some good reason that debug_val_dump function exists for.

like image 378
takeshin Avatar asked Dec 07 '22 02:12

takeshin


2 Answers

Every PHP variable is stored in a zval so you see your question doesn't really make sense.

debug_val_dump is not a very well thought out function because it's difficult to interpret. By simply passing a variable to the function you're changing the reference count of the zval. If you pass a reference to debug_val_dump without passing it by reference, you'll be forcing a zval separation and you'll always get back a zval with reference count 1 with reference flag clear, and if you pass it by reference (which must be done on call time, which is deprecated) then you can't tell, just by the output, if it was originally a reference or not.

Xdebug has a much more useful function where you don't pass the variable, you pass its name in a string instead. It's called xdebug_debug_zval.

Unless you're debugging code that uses references and you want to know how many variables belong to the reference set, these functions are probably not very useful for you.

To make any sense of them, I advise you to read reference count basics in the manual.

like image 182
Artefacto Avatar answered Dec 24 '22 01:12

Artefacto


You can't really use zval's from php itself. It is a core implementation detail which is not (normally) accessible from userland php code.

like image 30
Dennis Haarbrink Avatar answered Dec 24 '22 02:12

Dennis Haarbrink