Globalize variables in php - 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

Related

Global scope not printing data

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

What is the scope of global PHP variables in a Drupal page?

Consider this PHP script:
<?php
$a = "ok";
function foo() {
global $a; print "[$a]";
}
foo();
?>
It prints [ok] when run with a PHP interpreter as one would expect. But it prints just [] if run in a Drupal page. To make it work in Drupal I have to add another global specification before the variable declaration thus:
<?php
global $a; // WHY IS THIS NEEDED IN DRUPAL?
$a = "ok";
function foo() {
global $a; print "[$a]";
}
foo();
?>
Likely because Drupal includes the file inside a function:
function render() {
include 'my_script.php';
}
That makes $a local to the function, not global.

Why php variable scope not working in function

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

PHP - Question about specifying variable with global scope outside of a function

I understand that if you declare a variable within a php function with the 'global' keyword it will reference a variable declared outside the function, but why would a php programmer want to declare a variable outside of a function scope as 'global?' Thanks!
I understand what this does:
<?
$a = 1;
function $boo() {
global $a;
echo $a;
}
?>
But what I'm getting at is why would I want to do this?
<?
global $a;
function $boo() {
//foo
}
?>
It has to do with php scope
If you have file a.php that has a class like this
<?
class test()
{
function test()
{
include('b.php');
}
}
?>
and a file b.php
<?
$a = 1;
?>
Then $a will only be accessible in the scope of function test()
if you have global $a in b.php, $a then becomes a global variable
here's the php doc about it : http://php.net/manual/en/function.include.php
I have no idea why you would want to do that. For all intents and purposes (unless I am very, very much mistaken) this is exactly the same as just:
<?
var $a;
function $boo() {
// foo
}
?>
Which in turn is the very same as
<?
function $boo() {
// foo
}
?>
because you generally don't have to instantiate your variables in PHP.
Very curious why you're using variably-named functions though? (function $boo() {} )
Well, IMHO the use of global variables is a poor programming practice. It can cause unintended side-effects in your program which are hard to debug, and makes it harder to maintain.

Inside function variables to be used in includes in PHP

I have a function that includes another file like so
// some function
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
}
// some_file.php
<?php echo $someData; ?>
How would I get this to work where the include file can use the variables from the calling function? I will be using some output buffering.
As long as $someData is defined in SomeFunction(), some_file.php will have access to $someData.
If you need access to variables outside of SomeFunction(), pass them as arguments to SomeFunction().
That's already available :)
See include()
The best would be to not do use globals at all, but pass the variable as parameter:
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
some_foo($someData);
}
Otherwise you risk transforming your code base in spaghetty code, at least on the long term.
Seems kinda unorganized to include files in functions...what about...
function SomeFunction()
{
$someData = 'SomeData';
return $someData;
}
$data = SomeFunction();
<?php include('file.php') ?> // file.php can now use $data
You don't have to do anything. Usage of include() (and it's siblings) is analogous to copy-pasting the code from the included file into the including file at the spot where include() is called.
Simple example
test.php
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
include 'test2.php';
}
test();
test2.php
<?php
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
Again, this is analogous to the combined
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
}
test();

Categories