views:

90

answers:

5

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?

+1  A: 

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.

npsken
To further elaborate: you may use certain methods to check the type of a variable before doing certain operations (standard "if" statements) in order to avoid getting errors.
npsken
I know I can use things like `is_numeric()` but how does he know? How are the datatypes naturally kept up with? Are they always strings and `is_numeric()` goes through a switch in the function? Or does it look at the size. That is more the kind of answer I am looking for. How does PHP know anything besides that it has a variable `$c` and `$c = "something";`?
Jason
Let me clarify my question a bit.
Jason
You have to be careful using `is_numeric` because if the variable is a string like `"18"`, `is_numeric` will return true even if internally the variable is a string. there *are* data types in php, they are just dynamically assigned
cambraca
@cambraca - how do I know if it is *truly* `int` or `float` etc?
Jason
You can use [gettype](http://www.php.net/manual/en/function.gettype.php) to get it.
cambraca
Oh and you can apparently force a var type too using [settype](http://www.php.net/manual/en/function.settype.php), I didn't know that..
cambraca
@cambraca - I sure don't like that warning on the front. ha.
Jason
@Jason haha yeah I didn't notice it.. I guess the idea there is to hide the implementation
cambraca
Where is Hexadecimal! Ugh. haha.
Jason
A: 

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

cambraca
Even JS can get unhappy if I do something to a variable that has not been declared. Does PHP automatically declare it if it hasn't been yet?
Jason
theatrus gave a good answer here about how php handles variables. If you try to use a variable and php doesn't know about it, it can throw an exception (a warning, I think, I'm not sure..). `isset($variable)` can tell you if a variable is defined, and you can do stuff like `unset($variable)` too. But the [manual](http://php.net/manual/en/index.php) explains it better than I do
cambraca
@cambraca - are you talking about `unset()` in the manual, or the manual to answer my question? I'm no seeing anywhere on the TOC where it talks about how it handles datatypes. Just that there are types? Maybe I'm overlooking something.
Jason
About the whole issue. I believe [this page](http://www.php.net/manual/en/language.types.type-juggling.php) can be helpful
cambraca
@cambraca - that is what I was wanting. Sweet.
Jason
I've linked the main page of this manual before :)
Lajos Arpad
@Lajos I was saying thanks to his second link.
Jason
Yes, the http://www.php.net/manual/en/language.types.type-juggling.php and I've linked the http://php.net/manual/en/ which is the main page of the manual :)
Lajos Arpad
This is what you've said about it: " PHP.net does not talk about how PHP does things. Just implementation. I am wanting to know on an Electrical Engineer's (or CompE) level (as that's what I am). "
Lajos Arpad
A: 

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.

akellehe
I wasn't looking at HOW to find var types. More of how PHP knows the datatypes.
Jason
Did I provide some inaccurate information?
akellehe
A: 

"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

http://stackoverflow.com/questions/2894087/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.

Lajos Arpad
Haha. Programming languages do care. Ever forgot a semi-colon? PHP.net does not talk about how PHP does things. Just implementation. I am wanting to know on an Electrical Engineer's (or CompE) level (as that's what I am).
Jason
The last sentence is not necessary. I am not trying to compare and contrast JS and PHP. I know JS is for client and PHP is server. Thanks for digging those links up.
Jason
No problem, I'm happy if I can help.
Lajos Arpad
+2  A: 

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.

Yann Ramin
This was a great answer and EXACTLY what I was looking for. Thanks!
Jason