Jump to content

Question On The Variable (Array Value)


Go to solution Solved by requinix,

Recommended Posts

Php Gurus,

 

I have been doing guess work all this time. But not any more. Just gonna ask.

Look at the following code:

 

Concentrate on line 5.

It is obvious the "$colors" represent the variable name of the whole array thing (or "column" or "row" in MS Excell terminology).

But what does the "$value" represent ? Each array's values (or "cell" value in MS Excell terminology) ?

So, the value of the variable $value changes on each loop. Right ? From "Red" to "Orange" at the end. Correct ?

Example:

 

1st loop:

$value = Red;

 

2nd loop:

$value = Green;

 

And so on.

 

Correct ?

<?php
$colors = array("Red", "Green", "Blue", "Yellow", "Orange");
 
// Loop through colors array
foreach($colors as $value){
echo $value . "<br>";
}
?>
Edited by phpsane
Link to comment
https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/
Share on other sites

$value is an element in the array.

 

As for what happens, run the code and see for yourself.

 

Running the code showed me this:

 

Red

Green

Blue

Yellow

Orange

 

I figure, those values of the $value are related to the loop. 

Once I echo the value of the $value outside the loop then "Orange" (last value of the variable $value in the loop) is shown as the value.

What do I determine from all this ?

Well, I reckon the value of $value changed on each cycle and the echo inside the loop echoed each different value of the $value.

The final echo that was outside the loop, only echoed the final value of the $value.

 

<?php
$colors = array("Red", "Green", "Blue", "Yellow", "Orange");
 
// Loop through colors array
foreach($colors as $value){
echo $value . "<br>";
}
 
echo $value . "<br>";
 
?>

 

Result:

 

Red

Green

Blue

Yellow

Orange

Orange

 

That automatically means that the $value holds the values of each and every array. Right ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.