Scope of global array in function [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I'm trying to add to, and print the contents of a global array that is being accessed within an individual function.
PHP
<?php
// Globals for data cache
$city_array = [];
/* printArray
* print the value of global array
*/
function printArray() {
print_r($city_array);
}
printArray();
?>
This is returning an error:
Notice: Undefined variable: city_array in /Applications/XAMPP/xamppfiles/htdocs/donorsearch/process.php on line 6
How can I get access to this global array within this local function?

To access global variable in function you must use global to tell PHP you want that:
function printArray() {
global $city_array;
....
}

Either use global:
$city_array = [];
function printArray() {
global $city_array
print_r($city_array);
}
printArray();
Pass via function:
function printArray($array) {
print_r($array);
}
$city_array = [];
printArray($city_array);

Related

Call a function inside a class method and use its variables [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I'm new to PHP, i made some research but couldn't find a clear answer.
In a functions.php file i have a function that contains some variables that i need, and a classes.php file containing a class with a method that needs the values from the variables of the function in functions.php.
The Question is: why the echoed variable is outputted but the other variables are called as Undefined.And how can i call and use them.
Example:
functions/functions.php
<?php
// functions.php file
function incFunc(){
$inc_var1 = "Marco";
echo $inc_var1; //this variable output Marco without problem
echo "<br>";
echo "<br>";
$inc_var2 = "Polo"; //this is the variable i want to use in the class method.
}
?>
classes/classes.php
<?php
// classes.php file
include '../functions/functions.php';
class theClass
{
public function classFunc(){
incFunc();
echo $inc_var2; //Notice: Undefined variable: inc_var2
}
}
$obj = new theClass();
$obj->classFunc();
?>
Outputs:
Marco
Notice: Undefined variable: inc_var2 in...
If you want to use variables from incFunc function scope in the class you have to return the value
Example:
function incFunc($returnVar){
$inc_var1 = "Marco";
$inc_var2 = "Polo";
if ($returnVar == 'var1') {
return $inc_var1;
} else {
return $inc_var2;
}
}
Than,in your class method, save the result of a function in an variable:
public function classFunc(){
$inc_var1 = incFunc('var1');
// do stuff
}
This is just a basic example, if you want to include multiple variables at once, you can return an array instead of single variables.

Access variable from outside function (PHP) [duplicate]

This question already has answers here:
PHP: how to access variables inside a function that have been declared outside of it?
(3 answers)
Closed 1 year ago.
I am trying to access this variable into function but its give me an error Undefined variable: ERROR.
<?php
$ERROR["emptyEmail"] = "empyt email";
$ERROR["emptyPassword"] = "empty password";
function validateLogin($data) {
if (empty($data["email"])) {
return $ERROR["emptyEmail"];
} else if (empty($data["password"])) {
return $ERROR["emptyPassword"];
} else {
return "valid";
}
}
?>
Insert this inside the function:
global $ERROR;
So, the variable can be accessed inside the function scope (see global keyword).
function validateLogin($data) {
global $ERROR;
...
}
Alternatively you can access to all variables that are outside the function using $GLOBALS:
$GLOBALS['ERROR']

Unable to access included variable from separate PHP script [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I'm not new to programming but I am very new to PHP. I just can't figure out why this variable is not being recognised. I have a file called utils.php in directory utils like this:
<?php
$the_var = 'A'
function foo($bar) {
echo $bar;
}
?>
...and another file called work.php in a parent directory of utils like this:
<?php
include('utils/utils.php');
function doIt() {
echo $the_var; // is always empty
foo('bar'); // no problem
}
?>
Why can't the variable $the_var be accessed?
Variable inside function is not global. If you can access to variable $the_var use
function doIt($the_var) {
echo $the_var;
foo('bar'); // no problem
}
or
function doIt() {
echo $GLOBALS['the_var'];
foo('bar'); // no problem
}

PHP functions and variables [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
index.php:
<?php
require("lib.php");
echo getName();
?>
lib.php:
<?php
$name = "Matej";
function getName() {
return $name;
}
?>
Code doesnt work, i think its becouse PHP cant get variable form outsite of function. how to fix it?
You can lean about variable scope here. If you want using global variables, then use global keyword.
function getName() {
global $name;
return $name;
}
But... well, global is evil. In your case, a function like this is useless. Simply use $name.
You're trying to get value of $name outside function, you could call the function sending the value to it and it should work.
The reason is because the variable does not have the global keyword!
$name = 'hello';
function name() {
return global $name;
}

In php class object not working inside normal function? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
<?php
class Door
{
public function __construct()
{
}
public function test(){
echo "welocme";
}
}
$obj=new Door();
get_data();
function get_data(){
$obj->test();
}
$obj->test(); work well outside function but i need inside function. I cannot access object inside function show error
Fatal error: Call to a member function test()
Try like this: it may work..
If u use any outer variable in a function, then decleare as global $use_variable_name . now u can understand...
function get_data(){
global $obj;
$obj->test();
}
another and better way:
get_data($obj);// call this way...
function get_data($object){
$object->test();
}

Categories