I executed following code but php says:
Notice: Undefined variable: b in ..\..\..\demo.php on line 4
Notice: Undefined variable: a in ..\..\..\demo.php on line 4
Php Code:
<?php
$a='a';$b='b';
function test(){
echo $a.$b;
}
test(); // error
?>
But i changed the code to this:
<?php
$a='a';$b='b';
function test($a,$b){
echo $a.$b;
}
test($a,$b); // ab
?>
Why $a and $b are undefined in first case, since i defined them before?
Why parameters need to pass in php? It's not require in other like JavaScript.
If variables are defined outside the function, you need to specify the global keyword. Such as:
<?php
$a='a';$b='b';
function test(){
global $a, $b;
echo $a.$b;
}
test(); // error
?>
But your second example is the recommended way of handling it, typically.
$a and $b in the first example you provided are attempting to access those variables respectively from the local scope not the global scope. You could try declaring them like this
function test() {
global $a, $b;
echo $a . $b; //or $GLOBALS['a'].$GLOBALS['b'];
}
and you will get the correct values.
Try this
$a = '101';
$func = function() use($a) {
echo $a;
};
function func_2() {
global $func;
$a = 'not previouse a';
$func();
}
func_2();
Related
Why is $a not printing?
And what is the alternate of this, and I dont want to use return.
function abc () {
$a = 'abc';
global $a;
}
abc();
echo $a;
The reason why it's not echoing is because of two things:
1) You need to declare global "before" the variable you wish to define as being global.
and
2) You also need to call the function.
Rewrite:
<?php
function abc()
{
global $a;
$a = 'abc';
}
abc();
echo $a;
For more information on variable scopes, visit the PHP.net website:
http://www.php.net/manual/en/language.variables.scope.php
You can get your variable as:
echo $GLOBALS['a'];
see http://php.net/manual/en/language.variables.scope.php
You can use define():
function abc() {
define("A", "abc");
}
abc();
echo A;
Make sure you call the function. I added that just above echo.
First you must create and assign a variable. And then in you function describe that is a global var you want to use.
$a = 'zxc';
function abc() {
global $a;
$a = 'abc';
}
abc();
echo $a;
This is not really good idea to use golbal such way. I don't really understand why I so much want to use a global var...
But my opinion is better for you to use a pointer to variable.
function abc(&$var){
$var = 'abc';
}
$a = 'zxc';
abc(&$a);
echo $a;
Or even would be better to create an object and then access variable with-in this object
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
?>
Why is an empty line printed instead of 5?
function test()
{
echo "$a <br/>";
}
$a = 5;
test();
Functions in PHP do not inherit global scope or parent scope (unless using an anonymous function with use()).
You can use the global keyword to access them.
function test()
{
global $a;
echo "$a <br/>";
}
CodePad.
Jared Farish also points out the use of the global associative array $GLOBALS which holds all global variables and which, like any super global such as $_POST, $_GET, etc, is in scope everywhere.
function test()
{
echo "$GLOBALS[a] <br/>";
}
$a = 5;
test();
CodePad.
You could use an anonymous function...
$a = 5;
$test = function() use ($a) {
echo $a;
};
$test();
CodePad.
As a footnote, try not to rely on global variables. They can be a sign of poor program design if you overly rely on them.
you forgot to use the global
function test()
{
global $a;
echo "$a <br/>";
}
$a = 5;
test();
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);
For some reason this function won't return the value ciao:
$a = "ciao";
function a() {
return $a;
}
I have no idea why.
Functions can only return variables they have in their local space, called scope:
$a = "ciao";
function a() {
$a = 'hello`;
return $a;
}
Will return hello, because within a(), $a is a variable of it's own. If you need a variable within the function, pass it as parameter:
$a = "ciao";
function a($a) {
return $a;
}
echo a($a); # "ciao"
BTW, if you enable NOTICES to be reported (error_reporting(-1);), PHP would have given you notice that return $a in your original code was using a undefined variable.
In PHP, functions don't have access to global variables. Use global $a in body of the function or pass the value of $a as parameter.
$a is not in scope within the function.
PHP does not work with a closure like block scope that JS works with for instance, if you wish to access an external variable in a function, you must pass it in which is sensible, or use global to make it available, which is frowned on.
$a = "ciao";
function a() {
global $a;
return $a;
}
or with a closure style in PHP5.3+
function a() use ($a) {
return $a;
}