Confusion in creation of global variables [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.
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.

Related

How to use a variable set inside 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 4 years ago.
Hi I am new to php and I just run into a problem. I would like to get local variable data outside the function after it was called.
function MyFunction() {
$x = set
}
if($x == "set"){
code...
}
You can't do that. Instead return the value from the function.
function MyFunction() {
$x = "set";
return $x
}
if(MyFunction() == "set"){
code...
}

Global Variables PHP

I have some problems with PHP global variables.
can i define global variables in the function and use that in another function?
can i Define global variables in another global variables and use second global variable?
can i define global variables in a function, and use this function in another functions?
how about this question?
global $y; // $y = 5
$x = 10 * $y; // $x = 50
function numbers() {
global $x;
echo $x; // $x = 0;
}
Please find a nice article on Global variables in php - It will tell you about various global variables that exist in Php Language and how to use and access these variables and their purpose.
Reference : https://www.w3elearners.com/php/global-variables/

Global variable inside of 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.
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>";

Using a variable outside of a 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 8 years ago.
I have the following (summarised) code
define pauls_code($a, $b)
{
$c = $a + $b;
echo $a;
echo $b;
}
pauls_code(1,2);
echo $c; // how do I get this to print outside of the function? I have tried everything. I am hoping to see 123
make your function like this and return the $c
function pauls_code($a, $b){
$c = $a + $b;
return $c;
}
echo pauls_code(1,2);
To declare a function in php you simply put function pauls_code($param1, $param2) not define
You can not access $c outside of the function, read more about scope: http://php.net/manual/en/language.variables.scope.php

PHP: how to get variable from function in another function? [duplicate]

This question already has answers here:
Accessing a variable defined in a parent function
(5 answers)
Closed 9 years ago.
Example
function a(){
$num = 1;
function b(){
echo $num; // how to get $num value?
}
}
In this case global not working, because $num isn't global variable.
function a() {
$num = 1;
function b($num) {
echo $num;
};
b($num);
}
a();
You could use the S_SESSION to get the variable?
function a(){
$_SESSION['num'] = 1;
function b(){
echo $_SESSION['num'];
}
}
Not sure nested function is the way to go btw.

Categories