Global scope not printing data - php

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

Related

Global modifier not working in variable scope of a function

I have code that looks something like this
<?php
$a = "text";
function hay() {
global $a;
return $a;
}
$b = hay();
var_dump($b); // outputs NULL
?>
It doesn't display any errors.
The global modifier just doesn't work.
Your scope is not global. I think that your file included inside some fumction. That's why variable $a is not global. You could add global keyword to globalize your variable.
global $a;
$a = "text";
you are missing a semicolon in return return $a. This should be return $a;. Try this and your code will work

Globalize variables in php

I have 2 files. Lets say :
first.php
$a = 'blah';
echo 'echo2='.$a;
function foo(){
global $a;
echo 'echo3='.$a;
return $a;
}
second.php
require_once(path/to/the/file/first.php);
echo 'echo='.$a;
$b = foo();
echo 'echo4='.$b;
running the second.php file I get the following output :
echo=blah
echo2=blah
echo3=
echo4=
My question is "why I can't access variable $a in the function foo !
Change $global to global. That should fix it.
http://php.net/manual/en/language.variables.scope.php
or use
$GLOBALS["Your_var_without_dollar_sign"];
http://php.net/manual/en/reserved.variables.globals.php

returning variable from function

This is probably not the wisest question, but is it possible to return a variable from a function without redeclaring it?
function test(){
$a = 1;
return $a;
}
Would it somehow be possible to access $a with just calling the function? test();
not $a = test();
Is that the only way? I want to make $a available after calling the function.
You have to put the variable into the global scope:
http://codepad.org/PzKxWGbU
function test()
{
$GLOBALS['a'] = 'Hello World!';
}
test();
echo $a;
But it would be better to use a return value or references:
function test(&$ref)
{
$ref = 'Hello World!';
}
$var = '';
test($var);
I assume you want to change some context when you call the function.
This is possible. For example, all global variables share the same context across your application. If a function set's a global variable, its available in the global scope:
function change_global_variable($name, $value)
{
$GLOBALS[$name] = $value;
}
change_global_variable('a', 1);
echo $a;
However, whenever you do something like this, take care that you're destroying the modularity of your code. For example, if you have 50 functions like this and 80 variables, and then you need to change something, you need to change many, many places. And even harder, you don't remember which places you need to change and how they belong to each other.
It's normally better to make the context an object and pass it to the function that changes it, so it's more clear what the function does:
class Context extends ArrayObject {}
$context = new Context;
function change_context(Context $context, $name, $value)
{
$context[$name] = $value;
}
change_context($context, 'b', 2);
echo $context['b'], "\n";
This example demonstrates something that's called dependency injection. Instead of modifying the only one (global) context that exists in PHP, you are telling the function what it should modify, instead that it modifies hard-encoded global variables
There is a mix form of it, using the global variable scope to contain the context, so you spare to pass it as an additional parameter, however I only show it here for completeness, because it's not helping to retain modularity that well (however it's better than using more than one global variable):
class Context extends ArrayObject {}
$context = new Context;
function change_global_context($name, $value)
{
global $context;
$context[$name] = $value;
}
change_global_context('c', 3);
echo $context['c'], "\n";
Rule of thumb: Avoid global variables. Consider them of being very expensive and you don't want your code to be expensive.
No, it is not due to the scoping.
An option would be to declare $a before and then making it a global. It will then be available inside test:
$a = '';
function test(){
global $a;
$a = 1;
return $a;
}
echo $a; // 1
But may I ask why you want to do this? Are you just playing around or do you have a use case? You're most probably doing it wrong if you need this.
Maybe if you just want to show the variable you can use
function test(){
$a = 1;
echo $a;
return $a;
}
in this way if you want you can save the variable $a in a variable if not you can just show it calling the test function.
Hope it helps.
Nicola.

Why is not a global variable recognized in a function

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();

Function doesn't return value

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;
}

Categories