Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Simple Variable Questions

    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"?):
    PHP Code:
    echo $errno
    What about this one? (My guess: instance variable?):
    PHP Code:
    for ($i=0$i<= 10$i++) {} 
    And this one?:
    PHP Code:
    $text "a"// <-- This one
    if ($text == "a") {} 
    And finally, this one? (Pretty sure it's called a local variable?):
    PHP Code:
    if () {
       
    $text "a";

    Thank you!
    Last edited by RonnyNishimoto; 07-20-2012 at 01:41 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by RonnyNishimoto View Post
    PHP Code:
    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


    Quote Originally Posted by RonnyNishimoto View Post
    PHP Code:
    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:
    PHP Code:
    $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:
    PHP Code:
    for (int i 010; ++i){}
    for (
    int i 010; ++i){} 
    Where you cannot normally redeclare a variable. In PHP however, this is not the case.

    Quote Originally Posted by RonnyNishimoto View Post
    PHP Code:
    $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.

    Quote Originally Posted by RonnyNishimoto View Post
    And finally, this one? (Pretty sure it's called a local variable?):
    PHP Code:
    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!
    Last edited by Fou-Lu; 07-20-2012 at 01:26 AM.

  • Users who have thanked Fou-Lu for this post:

    RonnyNishimoto (07-20-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    I see I was wrong with everything :P

    Thank you so much Fou-Lu!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •