How Come Variable Not Assigned And Php Knows Where To Look F

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

How Come Variable Not Assigned And Php Knows Where To Look F

Post by UniqueIdeaMan »

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: Select all


<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/php_ ... ctions.htm
The following seems to be in pdo, which I haven't started learning just yet:
https://www.tutorialspoint.com/php/php_ ... _array.htm
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How Come Variable Not Assigned And Php Knows Where To Lo

Post by Christopher »

First, PHP is a dynamically typed language. So a variable takes its type from what is assigned to it, and is dynamically converted as necessary. So:

Code: Select all

$arrayvar = array( 1, 2, 3, 4, 5);
$stringvar = 'Hello';
$numbervar = 25;
$numbervar = 0.75;
$objectvar = new Foo();
Second, the foreach() loop iterates over an array and places an element in var after "as". You can also get array key if you want. So:

Code: Select all

 foreach( $numbers as $value ) {
        echo "Value is $value\n";
}
foreach( $numbers as $key => $value ) {
        echo "Value is $key - $value\n";
}
(#10850)
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How Come Variable Not Assigned And Php Knows Where To Lo

Post by UniqueIdeaMan »

I've understood from another 2 sources 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How Come Variable Not Assigned And Php Knows Where To Lo

Post by Christopher »

Because of they way your wrote your response I want to clarify a little:
UniqueIdeaMan wrote:I've understood from another 2 sources that the $numbers value would increment on each loop to $value.
$numbers is an array, so it is incorrect to say "the $numbers value would increment on each loop." The foreach loop will iterate over the loop by going through the keys of the array in sequence. In you case the keys are 0..4. But the keys could be any scalar values. Here are examples:

Code: Select all

<?php
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $key => $value ) {
	echo "Value is $key = $value <br>";
}
Output:
Value is 0 = 1
Value is 1 = 2
Value is 2 = 3
Value is 3 = 4
Value is 4 = 5

Code: Select all

$numbers = array( 'red'=>1, 'orange'=>2, 'yellow'=>3, 'blue'=>4, 'indigo'=>5);
foreach( $numbers as $key => $value ) {
	echo "Value is $key = $value <br>";
}
Output:
Value is red = 1
Value is orange = 2
Value is yellow = 3
Value is blue = 4
Value is indigo = 5
UniqueIdeaMan wrote:The $numbers value would remain the same on each loop but not the $value's value.
It's not that "$numbers value would remain the same", it is that $numbers is the array that is being iterated over.

See the for() loop for comparison on iterating over arrays with sequential integer keys:

Code: Select all

<?php
$numbers = array( 1, 2, 3, 4, 5);
$max = count($numbers);
for( $i=0; $i<$max; ++$i ) {
	echo "Value is $i = {$numbers[$i]} <br>";
}
(#10850)
Post Reply