Question

I haven't done much programing in many languages, but I know in C(++), you have to declare a variable type (int,char,etc).

In PHP you, of course, don't have to do that. You can start with $str = "something"; then later $str = array("something" => "smells"); and she is happy.

How does PHP compile? How does it know what the variable type is going to be? Does it even care?

This question has no relevance to anything I am doing. I am just curious.

EDIT.

I need to clarify this question a little bit.

In C, if I say:

int y;

It reserves x amount of bytes for y. If y overflows, bad news.

PHP doesn't have this nature (at least I don't think it does).

$i = "a number";
$i = 8;
$i = 1000000000000000000;

It's all the same to the language. How does it know how much to reserve? Or am I comparing Apples to Oranges? If I am going about this wrong, do you have any good topics that I can read to better understand?

Was it helpful?

Solution

Since you have C experience, consider a PHP variable to be like the following:

typedef struct {
 int varType;
 void* data;
} variable_t;

when you create a new variable, it will create a variable_t, give it a type tag (lets say 1 for int, 2 for string), and store it in a list somewhere (by scope, reference by name). The actual contents will be stored in *data. When the variable is again accessed, the type can be determined from int varType, and the appropiate action taken on void* data, such as using it as an int or string.

Imagine that the PHP snippet:

 $data = 12;
 $data2 = $data + 1;
 $data = "Hello";

produces instructions like the following (pseudo-Cish):

Line 1:
variable_t* var = new_variable(TYPE_INT, data);
store_variable("data", var);

Line 2:
variable_t* var = get_variable("data2");
if (var->varType == TYPE_INT)
   int value = 1 + *(int*)var->data);
else
   error("Can't add non int");
var = new_variable(TYPE_INT, value);
store_variable("data2", var);

Line 3:
variable_t* var = get_variable("data");
if (var)
  destroy_variable(var);
// Do like line 1, but for TYPE_STRING

This type of augmented data works in bytecoded, compiled, or direct interpreted languages.

There are more details in regards to different virtual machine implementations (local allocation, heap allocation, register VMs, etc). If you actually want to understand how virtual machines work, the best reference is Lua. Very clean language, very clean bytecode, and very clean virtual machine. PHP is probably the last implementation you should look at.

OTHER TIPS

PHP doesn't really compile -- it is interpretted (into op-codes).

Pretty much if you try to do something on a certain data type that can't be done, it'll give you an error. There is no type checking.

It doesn't compile. It is an interpreted language, like Javascript.

I realize this is an old question but here is some more specific information on how PHP handles the questions asked by the OP.

This is the page from the PHP reference that you'd want to start with:

Introduction to Variables

I know linking isn't preferred but that link should be stable and I don't want to wholesale copy PHP reference documentation. Here are the highlights.

OP: How does PHP know what type of variables it uses (or does it)?

PHP is written in C and uses a C struct typedef which it calls a zval along with a C union typedef which is calls a zval_value to represent all variables.

typedef struct _zval_struct {
    zvalue_value value;        /* variable value */
    zend_uint refcount__gc;    /* reference counter */
    zend_uchar type;           /* value type */
    zend_uchar is_ref__gc;     /* reference flag */
} zval;

"The engine attempts to cover up the complexity of the concept of a variable that can be any type by providing a uniform and intuitive set of macros for accessing the structures various fields."

"PHP is a dynamic, loosely typed language, that uses copy-on-write and reference counting." Reference Counting and Copy-on-write (COW) are two powerful concepts PHP uses which I won't go into here but are worth reading about.

"Weak typing is implicit of the engine's preference to convert, or coerce variables into the required type at execution time. Reference counting is the means by which the engine can deduce when a variable no longer has any references in the users code, and so is able to free the structures associated with the variable."

"The zval_value is a union which can represent all types a variable may hold."

" ... a variable can be of one type, the variable data is represented by the appropriate field in the zval_value union. The zval itself holds the type, reference count and a flag to indicate if a variable is a reference."

How does PHP compile?

"Compile" is a broad word that can have different meanings and PHP doesn't compile in the traditional sense. It does do a sort of pre-compilation which converts the source code into opcodes which are instructions that can be executed by the processor. These opcodes are cached which prevents PHP from have to parse frequently called scripts.

How does it know what the variable type is going to be? Does it even care?

As already quoted above it is the PHP engine's "preference to convert, or coerce variables into the required type at execution time." Baiscally PHP does always store what it determines a variable's type to be when it's created but when a variable is referenced PHP makes another determination of what the type is based on the context in which it is being used.

"PHP is weakly typed, as such the engine provides API functions for converting variables from one type to another."

The engine has a set of macros it uses for working with the zvals to convert a variable's type so that you usually don't have to deal with that.

If you want to see what zvals look like in action they can be dumped with:

debug_zval_dump($variableName);

"How does PHP compile? How does it know what the variable type is going to be? Does it even care?

This question has no relevance to anything I am doing. I am just curious."

PHP is an interpreted language and it doesn't compile.

PHP doesn't know what type the variable is going to be, because the type of the variable is determined by the type of the value which was assigned last time to that variable.

You can do this:

$foo = 5;
var_dump($foo);
$foo = "5";
var_dump($foo);
$foo = array();
$foo[] = 0;
$foo[] = 1;
$foo[] = 2;
var_dump($foo);

As you can see, whenever a value is assigned to foo, the type might be changed.

PHP doesn't care about the type of your variable, because it's a programming language and it doesn't have feelings.

EDIT:

"Or am I comparing Apples to Oranges?"

Yes, you are. Here you can learn more about the language.

EDIT2:

PHP is a scripting language with an interpreter and without a compiler. This means that in PHP you can only get runtime errors. It's a liberal language if we consider types, because you have the right to do anything with your variables and their types will be modified according to the usage of them.

These links might be useful for you:

http://www.gidforums.com/t-11866.html

http://www.webmaster-talk.com/coding-forum/186350-fundamental-differences-between-php-c-c.html

Variable scope difference between PHP and C: block scope is not exactly the same?

Note, that PHP is executed on the server, if you want to create client events, Javascript is my suggestion.

PHP now supports type hinting, you can include things like 'string' or 'array' in function definitions that are caught as the scripts are parsed to indicate there is a type mismatch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top