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
Related
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...
}
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.
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.
Say I have...
function one($x){
return $a + $x;
}
function two(){
$a = 5;
echo one(3);
}
Will this show the answer "8" or "3"? In other words, will function one get the value of $a or do I need to declare it global somewhere?
N.B. I haven't tried it yet, but I'm asking it here to understand WHY it acts one way or the other.
No function one does not know about $a. But this can be done.
$a = 5;
function one($x){
global $a;
return $a + $x;
}
function two(){
global $a;
$a = 5;
echo one(3);
}
Now two() would echo 8
Functions do not inherent scope from the function that calls them. (Nor do they inherit global variables by default - that's what the global keyword is for.)
Thus, $a will be completely undefined inside of one() and you'll get a notice about it.
For more details, see the Variable Scope page in the PHP manual.
You won't get 8 or 3. You'll get a Notice since $a has not been defined in the scope of the function one, and you attempt to read it:
PHP Notice: Undefined variable: a in - on line 3
PHP Stack trace:
PHP 1. {main}() -:0
PHP 2. two() -:11
PHP 3. one() -:8
If you to use a class as close as to your example, Notice no global usage, just assign your variables $this->* then there global scope within the class and its methods/functions you can also access them from outside of the class like $functions->a:
<?php
Class functions{
function one($x){
return $this->a + $x;
}
function two(){
$this->a = 5;
echo $this->one(3);
}
}
$functions = new functions();
$functions->two(); //8
echo $functions->a;//5
?>
I have a php file:
<?php
$a = 1;
function test(){
echo $a;
}
test();
?>
And I get this error:
Notice: Undefined variable: a in X:\...\test.php on line 4
Using XAMPP # 32bit W7.
Variables have function scope. $a inside the function is not the same as $a outside the function. Inside the function you have not defined a variable $a, so it doesn't exist. Pass it into the function:
$a = 1;
function test($a) {
echo $a;
}
test($a);
You have trouble understanding variable scope. $a is defined in the global scope, but not in the function scope. If you want your function to know what $a contains, you have two choices :
Make it global (usually a bad solution)
Add a new argument to your function, and pass your variable to your function
You can use global as advised, but that is bad practice. If you need variables in a function from outside the function then pass them as parameters.
$a = 1;
function test($a) {
echo $a;
}
test($a);