PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Aug 21, 2017 1:29 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat May 06, 2017 7:18 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 102
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
";
         }
         
         /* 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
";
         }
      ?>
     
   </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


Top
 Profile  
 
PostPosted: Sat May 06, 2017 7:32 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13421
Location: New York, NY, US
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:
Syntax: [ Download ] [ Hide ]
$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:
Syntax: [ Download ] [ Hide ]
 foreach( $numbers as $value ) {
        echo "Value is $value\n";
}
foreach( $numbers as $key => $value ) {
        echo "Value is $key - $value\n";
}

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun May 07, 2017 8:32 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 102
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.


Top
 Profile  
 
PostPosted: Mon May 08, 2017 5:11 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13421
Location: New York, NY, US
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:
Syntax: [ Download ] [ Hide ]
<?php
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $key => $value ) {
        echo "Value is $key = $value <br>";
}
Quote:
Output:
Value is 0 = 1
Value is 1 = 2
Value is 2 = 3
Value is 3 = 4
Value is 4 = 5

Syntax: [ Download ] [ Hide ]
$numbers = array( 'red'=>1, 'orange'=>2, 'yellow'=>3, 'blue'=>4, 'indigo'=>5);
foreach( $numbers as $key => $value ) {
        echo "Value is $key = $value <br>";
}
Quote:
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:
Syntax: [ Download ] [ Hide ]
<?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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group