Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have a require("config.php") with arrays, but still get Undefined variable error

I hava a function that looks something like this:

require("config.php");

function displayGta()
{
    (... lots of code...)

    $car = $car_park[3]; 
} 

and a config.php that look something like this:

<?php
$car_park = array ("Mercedes 540 K.", "Chevrolet Coupe.", "Chrysler Imperial.", "Ford Model T.", "Hudson Super.", "Packard Sedan.", "Pontiac Landau.", "Duryea."); 
 (...)
?>

Why do I get Notice: Undefined variable: car_park ?

like image 522
ganjan Avatar asked Jun 28 '10 21:06

ganjan


People also ask

How do I fix undefined variable error in PHP?

The solution simply is to check if it has been set before referencing. We can use the isset() function, which checks if the variable 'is set' or not before referencing to them.

Why am I getting undefined variables PHP?

Undefined variable: the variable's definition is not found in the project files, configured include paths, or among the PHP predefined variables. Variable might have not been defined: there are one or more paths to reach the line with the variable usage without defining it.

How check variable is undefined in PHP?

$isTouch = isset($variable); It will return true if the $variable is defined. If the variable is not defined it will return false . Note: It returns TRUE if the variable exists and has a value other than NULL, FALSE otherwise.


3 Answers

Try adding

 global $car_park;

in your function. When you include the definition of $car_park, it is creating a global variable, and to access that from within a function, you must declare it as global, or access it through the $GLOBALS superglobal.

See the manual page on variable scope for more information.

like image 110
Paul Dixon Avatar answered Oct 02 '22 22:10

Paul Dixon


You could try to proxy it into your function, like:

function foo($bar){

(code)

$car = $bar[3];

(code)

}

Then when you call it:

echo foo($bar);

like image 26
misterte Avatar answered Oct 02 '22 22:10

misterte


Even though Paul describes what's going on I'll try to explain again.

When you create a variable it belongs to a particular scope. A scope is an area where a variable can be used.

For instance if I was to do this

$some_var = 1;

function some_fun()
{
   echo $some_var;
}

the variable is not allowed within the function because it was not created inside the function. For it to work inside a function you must use the global keyword so the below example would work

$some_var = 1;

function some_fun()
{
   global $some_var; //Call the variable into the function scope!
   echo $some_var;
}

This is vice versa so you can't do the following

function init()
{
   $some_var = true;
}

init();

if($some_var) // this is not defined.
{

}

There are a few ways around this but the simplest one of all is using $GLOBALS array which is allowed anywhere within the script as they're special variables.

So

$GLOBALS['config'] = array(
   'Some Car' => 22
);

function do_something()
{
   echo $GLOBALS['config']['some Car']; //works
}

Also make sure your server has Register globals turned off in your INI for security. http://www.php.net/manual/en/security.globals.php

like image 37
RobertPitt Avatar answered Oct 02 '22 22:10

RobertPitt