14

I'm a PHP developer since many years but I don't know just one detail of how PHP handles variables and their types behind the scenes. I mean: in PHP - in theory - I could use the same variable to store an integer, and then a string, and then a boolean, and then an array... etc...

Personally, I loathe this way of "poorly-casted" programming, but I'm wondering how can PHP store and manage the variables and their types as I asked. I imagine the interpreter creates and handles C variables in behind, but I can't figure out how.

Thank you.

tobia.zanarella
  • 1,246
  • 1
  • 17
  • 25

2 Answers2

12

Behind the scenes, PHP variables are stored in a "zval" structure, which consists of a union between all of the types of data which the variable could store (e.g, a long, a double, a string pointer/length, an object pointer...), and a couple of other fields outside the union which indicate which type it is and keep track of a reference count.

There's some further discussion of this at:

http://devzone.zend.com/317/extension-writing-part-ii-parameters-arrays-and-zvals/

-1

If I recall correctly, PHP will initiate several variables in memory for each variable in PHP:

$test = 0;

Translates to...

int test = 0;
float test = 0;
char test = 0;
bool test = false;
pointer test = null;
...
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • If that's true, it's horribly inefficent for no good reason. Citation? –  May 02 '12 at 17:00
  • See duskwuff's answer, it's better than mine. – Madara's Ghost May 02 '12 at 17:01
  • His answer says something different. In fact, his answer implies only one variable exists. –  May 02 '12 at 17:01
  • @delnan Nobody said efficiency was the goal. It's not. This is PHP, a scripting language developed for building dynamic websites. Efficiency is way down the list of concerns. – user229044 May 02 '12 at 17:06
  • @meagar Taking (roughly) five times as much memory *for every single value* as necessary isn't just inefficent, it's plain evil, regardless of how important performance is. It's like deliberately implementing multiplication as PHP function that loops and adds. It's just stupid. –  May 02 '12 at 17:10
  • @meagar As before, I refuse to believe the PHP implementors are that inefficent and ask for a citation. And how is it faster than a `union`? Either way, one needs a tag and dispatch on it. –  May 02 '12 at 17:13
  • @meagar The `zval` definition at this link, as duskwuff also describes, uses a [**union**](http://stackoverflow.com/q/346536/395760) for the possible values. A union may only contain *one* of its members at any given time, and thus only has as much space as the largest member needs. Which is definitely *not* what you are trying to tell me -- it's vastly more efficent, and it's standard practice in dynamic language interpreters. Lua does the same thing, and is pretty much *the* fastest dynamic languages around. Believe me, I fully understand how one implements dynamic languages. –  May 02 '12 at 17:18
  • @delnan You are of course correct. I forgot how unions work. I was dead wrong and I apologize, consider me chastised. – user229044 May 02 '12 at 17:29