Global variable inside of 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 7 years ago.
Code below prints produces error:
Notice: Undefined variable: x in C:\wamp\www\h4.php on line 9
and output:
Variable x inside function is:
Variable x outside function is: 5
Code:
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
What gets wrong with x global variable inside of myTest function?

Change to this:
function myTest() {
global $x;
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
The global command says to use the global value of $x rather than a private one.

To access global variable you need to define with 'global' keyword with in the function.After define it you can access that global variable.
$x = 5; // global scope
function myTest() {
//use global keyword to access global variable
global $x;
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";

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.

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 variable global and static

I am new to PHP.
I'm studying variables scopes.
A variable declared outside a function has a GLOBAL SCOPE and can only
be accessed outside a function.
A variable declared within a function has a LOCAL SCOPE and can only
be accessed within that function.
The global keyword is used to access a global variable from within a
function.
To do this, use the global keyword before the variables (inside the
function)
Normally, when a function is completed/executed, all of its variables
are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
I need to declare variable within function to be global so I can get access to it from outside the function and to be static at the same time so I can keep the value of the variable after execution of the function and use it again.
I tried
global static $x;
but it doesn't work.
I need to know if I'm thinking in wrong way case I'm new to PHP.
<?php
$x = 5;
function myTest() {
echo "x is equal to".$GLOBALS['x']."";
$GLOBALS['x']++;
}
myTest();
myText();
?>
it executes only the first myTest().
and the second one display an error
Fatal error: Uncaught Error: Call to undefined function myText()
just declare it in global scope then use $GLOBALS[] array or global keyword to use that variable in a function. And as they hold the value even after function execution you don't need static keyword as well.
study $GLOBALS, Variable scope
you can use static or global to keep the value:
function doStuff() {
$x = null;
if ($x === null) {
$x = 'changed';
echo "changed.";
}
}
doStuff();
doStuff();
the result would be: changed.changed.
if you use:
function doStuff() {
static $x = null;
if ($x === null) {
$x = 'changed';
echo "changed.";
}
}
doStuff();
doStuff();
the result would be changed. because static keeps last value even if you call function in multi times
also global have the same result because of it's definition so you can also use:
global $x;
in the function and result would be same: changed.
You have typo problem in your code (second calling of your function):
function myTest() ....
Then you called it:
myTeXt();

Scope of global array in 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 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);

Confusion in creation of global 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 7 years ago.
How is $x and $y global variables if they don't have global written before them?
<!DOCTYPE html>
<html>
<body>
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
</body>
</html>
Because they are defined in the global namespace.
A variable declared in a function can only be used within that function. You can overrule this by using the global operator that looks for the variable in the global name space.
function addition() {
global $x, $y;
$GLOBALS['z'] = $x + $y;
}
However the $GLOBALS variable is a place where all globals are stored. Since you define it in that function the $z variable is set.

Categories