PHP for ; Foreach Variable Scope

Explanation of PHP array foreach scope within function

You have two options.

  1. Add the global keyword

    function getCarsByMake($carMake){
    global $cars;
    foreach($cars as $car){
    if($car->make == $carMake){
    echo 'Car: ' . $car->make . ' ' . $car->model . "<br>";
    }
    }
    }
  2. Use the $GLOBALS array:

    function getCarsByMake($carMake){
    foreach($GLOBALS["cars"] as $car){
    if($car->make == $carMake){
    echo 'Car: ' . $car->make . ' ' . $car->model . "<br>";
    }
    }
    }

Although I'd still recommend passing it as an explicit parameter as that makes the code more readable and maintainable, IMHO.

PHP: variable scope in multiple foreach

foreach loops do not have their own scope.

The way this is expected to work is:

Set $i to 0
Enter the first foreach loop with `$i = 0`
Enter the second foreach loop with `$i = 0`

Set $i to 1
Enter the first foreach loop with `$i = 1`
Enter the second foreach loop with `$i = 1`

etc.

I'll bet a beer that the loops work as expected, but there is nothing to do for the inner loops when $i reaches 1.

Why can I access foreach variable outside of its scope?

The foreach loop is a block level, not a function level.

Variables declared in function are not available outside.

But, variables outside block are always available.

Their values should be the latest value from iteration.

Reference:

PHP foreach variable scope issue

Simply use isset to check if variable $elem['comments'] exists:

<div>
<?php
foreach($arr as $key => $elem){
if(isset($elem['comments'])){
// Comments exists here
echo "<span>".$elem['comments']."</span>";
}else{
// Comments do not exists here, so don't echo anything
}
}
?>
</div>

Or use array_key_exists to check if comments key is in array elem:

<?php
foreach($arr as $key => $elem){
if(array_key_exists('comments', $elem)) {
// Comments exists here
echo "<span>".$elem['comments']."</span>";
}else{
// Comments do not exists here, so don't echo anything
}
}
?>

Take notice that:

isset() does not return TRUE for array keys that correspond to a NULL
value, while array_key_exists() does.

So in your use case I would recommend you to use isset as you discard existing comments keys with null values the same way as if comments keys do not exists.

how can i get foreach loop variable outside of loop

You have just to append your variable like this

$pages = get_pages();
$pagee = array();
foreach ($pages as $page) {
$pagee[] = $page->post_title;
}
echo implode(",",$pagee);

Variable scope in for-loop and while-loop

The $a on line 1 and the $a in the foreach() loop is one and the same object. And after the loop ends, $a has the value 3, which is echoed in the last statement.

According to php.net:

For the most part all PHP variables only have a single scope.

Only in a function does the variable scope is different.

This would produce your desired result '231':

$a = '1';
$c = array('2', '3');
function iterate($temp)
{
foreach($temp as $a)
echo $a ;
}
iterate($c)
echo $a;

Because in the iterate() function, $a is independent of the $a of the calling code.

More info: http://php.net/manual/en/language.variables.scope.php

PHP same name in foreach as outer scope causes overwrite

The scope in PHP is at global or function level, there is no block scope, see http://php.net/manual/en/language.variables.scope.php



Related Topics



Leave a reply



Submit