#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1

    Question How Come Variable Not Assigned And Php Knows Where To Look For Value ?


    Good Weekend Folks!


    I have a question.
    I do not understand how php knows how to define this variable $value in the following example since it has not been declared. How does it know what that variable represents since no reference or pointer has been assigned to it ? Strange!

    https://www.tutorialspoint.com/php/php_arrays.htm

    Code:
    <html>
       <body>
       
          <?php
             /* First method to create array. */
             $numbers = array( 1, 2, 3, 4, 5);
             
             foreach( $numbers as $value ) {
                echo "Value is $value <br />";
             }
             
             /* Second method to create array. */
             $numbers[0] = "one";
             $numbers[1] = "two";
             $numbers[2] = "three";
             $numbers[3] = "four";
             $numbers[4] = "five";
             
             foreach( $numbers as $value ) {
                echo "Value is $value <br />";
             }
          ?>
          
       </body>
    </html>
    I thought "array_values()" was supposed to be there in the above example but "$value" is there instead.
    What is the difference between the 2 ?

    https://www.tutorialspoint.com/php/p..._functions.htm
    The following seems to be in pdo, which I haven't started learning just yet:
    https://www.tutorialspoint.com/php/p...tion_array.htm
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,350
    Rep Power
    630
    Not sure what you are really asking. In PHP the variable type is determined dynamically in context. Since $numbers is an array of integers, PHP knows that each value assigned to $value in the foreach loop is then an integer type. In the second case it is a string type. I don't see what confuses you about the 2 tutorialspoint links.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1
    Originally Posted by gw1500se
    Not sure what you are really asking. In PHP the variable type is determined dynamically in context. Since $numbers is an array of integers, PHP knows that each value assigned to $value in the foreach loop is then an integer type. In the second case it is a string type. I don't see what confuses you about the 2 tutorialspoint links.
    Elsewhere, I understood from another programmer's hints that, the $numbers value would increment on each loop to $value.
    The $numbers value would remain the same on each loop but not the $value's value.
    I get it now.

    My question was, since $numbers has been assigned a value then I know it's value. But since, $value's value has not been assigned then how would php know what value to assign to it at each loop since it has not been assigned any value in the first place. But, now I understand that it's original value (at the beginning of the loop) is the same value as $numbers value.

    Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1
    Mod, you may close this thread now.
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,494
    Rep Power
    111
    Just a note for future readers...
    But, now I understand that it's original value (at the beginning of the loop) is the same value as $numbers value.
    Just to clarify, this is not true in full. Its value IS assigned from the $numbers ARRAY, but not in full. At the beginning of each loop, just the next indexed item of that array is assigned to the $value VARIABLE. So, in the end, $value will have held everything from the array, but only 1 item at a time.

    Comments on this post

    • UniqueIdeaMan agrees
    Last edited by Triple_Nothing; May 12th, 2017 at 08:42 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1
    Closed.

IMN logo majestic logo threadwatch logo seochat tools logo