4

I have the following PHP code:

$car1 = new Car('Ford','Fusion');
$car2 = new Car('Chevy', 'Avalanche');
$car3 = new Car('Ford', 'F150');

$cars = array($car1, $car2, $car3);

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

getCarsByMake('Ford');

I get the error that $cars in the foreach statement is undefined. However, as I understand it, the $cars array should be global in scope? If I pass the array into the function through the constructor it works fine. But I'm wondering why I can't access the array in this way.

3
  • 2
    use global $cars; inside the function. Just above foreach loop Commented Jul 21, 2017 at 16:30
  • function is just like a room, and the cars is outside the room, so you need to drag the car to the room so you can modify it inside the room Commented Jul 21, 2017 at 16:37
  • Passing the $cars into the function makes it a lot easier to test, you can pass in various versions to check the function reacts as it should. Commented Jul 21, 2017 at 16:50

4 Answers 4

6

Along with Exprator's solution, you could also pass the $cars array to the function like this.

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

getCarsByMake('Ford', $cars);
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, I made a note in my original post that this method did work for me, I was just wondering about the scope of the variable itself. Thanks!
@Reed You should accept an answer, whichever one works the best in your case.
3

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.

2 Comments

Any reason why you'd suggest using it as a parameter? Another user advised another method over it.
Because IMHO it makes the code less readable and less maintainable. Within your function you can never be sure where the global variable comes from and where (or if) it is initialized. And from outside it is not obvious to a caller that the function needs and works on the global variable cars.
3
function getCarsByMake($carMake){
    global $cars;
    foreach($cars as $car){
        if($car->make == $carMake){
            echo 'Car: ' . $car->make . ' ' . $car->model . "<br>";
        }
    }
}

getCarsByMake('Ford');

its because the function is not getting the $cars , you need to globally access it inside the function

3 Comments

Is there any explanation to this besides just being a nature of the language?
@Reed Your variable is not in the global scope unless declared global.
the reason is OOPs @Reed
0

You can call $cars globally but you can also try by another way according to your code.Ex:

$car1 = new Car('Ford','Fusion');
$car2 = new Car('Chevy', 'Avalanche');
$car3 = new Car('Ford', 'F150');

$cars = array($car1, $car2, $car3);

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

getCarsByMake('Ford',$cars);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.