...

View Full Version : Resolved Simple Variable Questions



RonnyNishimoto
07-20-2012, 01:27 AM
I'm getting a little confused on the terminology of each variable.

What is the term used for this type of variable? (It displays the error number, I think it's called a "predefined variable"?):


echo $errno;


What about this one? (My guess: instance variable?):


for ($i=0; $i<= 10; $i++) {}


And this one?:


$text = "a"; // <-- This one
if ($text == "a") {}


And finally, this one? (Pretty sure it's called a local variable?):


if () {
$text = "a";
}


Thank you! :D

Fou-Lu
07-20-2012, 02:23 AM
echo $errno;


Nothing. That's simply an output for the variable $errno. There is no predefined variable called $errno in PHP. I'm aware of only 5 non-superglobal variables (ignoring registered long arrays since those are gone now) in php which are predefined (and not necessarily guaranteed depending on config):
$php_errormsg
$HTTP_RAW_POST_DATA
$http_response_header
$argv
$argc






for ($i=0; $i<= 10; $i++) {}


I'd call that more of an inline assignment. An instance would refer to an object instance. $i in this case is still usable after the loop as well. $i is most typically used as the increment counter. So although its nothing special, it is the control of the loop itself.
One that does actually have a name is a sentient variable:


$bDone = false;
while (!$bDone)
{
// do stuff, add a condition
// . . .
$bDone = true;
}

But I wouldn't refer to the variable in use of a for loop sentient.
BTW, in most other languages that allow inline assignment, that would be instance only since $i is discarded after the loop. So you can do this:


for (int i = 0; i < 10; ++i){}
for (int i = 0; i < 10; ++i){}

Where you cannot normally redeclare a variable. In PHP however, this is not the case.





$text = "a"; // <-- This one
if ($text == "a") {}


Simply an assignment, and then a comparison. That simply states that $text will contain the value string(1)"a" within it.



And finally, this one? (Pretty sure it's called a local variable?):


if () {
$text = "a";
}



Also an assignment, but you are correct that its local. $text will only exist if the condition for the if is true (which in this case is a syntactical error :P). I wouldn't exactly say its local though, the reason why it only exists within that if branch is simply due to PHP's ability to use variables without predefining them. If this were something like C with a single pass preprocessor, this situation wouldn't occur.

Hope that helps!

RonnyNishimoto
07-20-2012, 02:35 AM
I see I was wrong with everything :P

Thank you so much Fou-Lu!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum