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!

         /* 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 />";

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 ?

The following seems to be in pdo, which I haven’t started learning just yet:

You are setting the value of $value here:-

foreach( $numbers as $value )

This says: For each element in the array called $numbers, assign its value to the variable named $value. Thus `$value is declared, multiple times.


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.
However, saying all this, your reply was better as you PLAINLY pointed-out to me where the original value of $value is coming from.

My original 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.

You know, this forum only allows us to LIKE and LOVE posts but it doesn’t allow us to give POINTS/SCORES/CREDITS to repliers. Maybe, when I build my own forum, I add my unique feature so other members can give scores to each replier based on what they think the replier’s answer is worth ? :wink:
(Now, you understand why I’m learning php. So, I don’t have to suggest features to websites and wait for them to update. I can build my own with my own features which netizens of the world would appreciate and benefit from).


The thing is , $value IS being assigned a value in the statement foreach( $numbers as $value )

foreach essentially envokes the each function of an array; a function which returns the value stored at the CURRENT position of the array pointer and then moves the pointer forward. think of foreach as the looping version of writing: $value=each($values);

you can also get the current key of the array if you write:

foreach( $numbers as $key=> $value ) {
            echo $key."'s value is $value <br />";

From what I have learned about arrays and using the foreach loop, I find it easy to look at it this way.

print('<h1>First array</h1>');

$array1 = array(1, 2, 3, 4, 5); // This array doesn't have any keys

foreach($array1 as $key1 => $value1) {

    print('Key: ' . $key1);
    print('<br />');
    print('Value: ' . $value1);
    print('<br /><br />');


print('<br /><br />');

print('<h1>Second array</h1>');

$array2 = array(1 => 'First key', 2 => 'Second key', 3 => 'Third key', 4 => 'Fourth key', 5 => 'Fifth key'); // This array DOES have keys

foreach($array2 as $key2 => $value2) {

    print('Key: ' . $key2);
    print('<br />');
    print('Value: ' . $value2);
    print('<br /><br />');


Where you don’t declare a key in the array, PHP will increment a key for you starting from 0. The key itself can actually be anything you want. It can be a string or an integer. Take this third array for example.

print('<h1>Third array</h1>');

$array3 = array('first_key' => 'First key description', 'second_key' => 'Second key description', 'third_key' => 'Third key description', 'fourth_key' => 'Fourth key description', 'fifth_key' => 'Fifth key description'); // This array has custom keys.

foreach($array3 as $key3 => $value3) {

    print('Key: ' . $key3);
    print('<br />');
    print('Value: ' . $value3);
    print('<br /><br />');


The keys from this array will return

first_key, second_key, third_key, fourth_key, fifth_key

And the values from this array will return

First key description, Second key description, Third key description, Fourth key description, Fifth key description

Without actually increment the key itself. So if you specify the key in your array, PHP will use those keys. But if you don’t specify the keys in your arrays, PHP will increment the key starting from 0 for you. If you set the key manually starting from 1, it’ll most likely use 1 and so on unless you are inconsistent and use a string in between the numeric keys.

For slow learners and beginners, the first argument of the foreach loop ALWAYS HAS to be an array. The second argument is actually optional unless you want to do something with the keys. And the third argument is always the returned value of the array with or without the keys.


Your final paragraph was definitely worthy! :weee:

BTW…foreach will work on any object that implements the traversable interface, not just arrays.


Right on. I forgot to also mention objects as well. I kept thinking of arrays because I was working on a function of mine that consisted of 2 multidimensional arrays that got me a headache until I figured it out after hours of messing with the codes.

And as you will keep being reminded, the manual is your go to place for full and correct explanations of the various aspects of php.

Check my 41st post here Sammy:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.