Unable to access global variable which is set in functions.php - php

I have mytheme-child/functions.php where I declared and assigned global variable:
global $mycustvar;
$mycustvar = "abc"
Now when I print_r() same variable in **mytheme-child/woocommerce/single-product/product-image.php** then not able to get output as abc. It should be right? As it's a global variable?.
Kindly correct me if I misunderstood somewhere.

For example, in functions.php:
function test() {
global $hello;
$hello = 'hello world';
}
add_action('after_theme_setup', 'test');
In single.php, this will not work:
echo $hello;
Because $hello is undefined. This however will work:
global $hello;
echo $hello;

Give it a try:
// function.php
function my_custom_data() {
global $mycustvar;
$mycustvar = 'abc';
}
add_action( 'after_theme_setup', 'my_custom_data' );
then call any where just as
global $mycustvar;
echo $mycustvar;

Related

Delete an element of array in function

I'm using some functions to delete vars. My code is like:
<?
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
global $arr;
unset($arr['var2']);
}
foo();
But in the PHP manual:
To unset() a global variable inside of a function, then use the
$GLOBALS array to do so:
unset($GLOBALS['arr']['var2']);
Doesn't unset anything because $GLOBALS['arr']['var2'] doesn't exist.
I only want to unset GLOBAL array element inside function.
It exists, because GLOBALS are supervariable and it has everything other var has.
edit:
I tried to do it but after I try to call foo() then i try to print_r($arr)
it show both var1 and var2, and if I try print_r($GLOBALS['arr']['var2']) it show undefined index.... Maybe it's be config...
edit2
I mistyped it in my script. So it's working...
Full working code:
<?
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
global $arr;
unset($GLOBALS['arr']['var2']);
}
foo();
You can pass variable by reference:
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(&$a){
unset($a['var2']);
}
foo($arr);
https://secure.php.net/manual/en/language.references.pass.php
unset($GLOBALS['arr']['var2']); is correct.
see here https://3v4l.org/rCN5h
<?php
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
unset($GLOBALS['arr']['var2']);
}
var_dump($arr);
foo();
var_dump($arr);

Why setting superglobal variable $GLOBALS['foo'] doesn't work?

The following code produces a warning:
<?php
$GLOBALS['foo'] = "Example content<BR><BR>";
echo $foo; // that works!
Test();
function Test()
{
echo $foo; // that doesn't work!
}
?>
The warning is:
Notice: Undefined variable: foo
How come ?
Inside the function, $foo is out of scope unless you call it as $GLOBALS['foo'] or use global $foo. Defining a global with $GLOBALS, although it improves readability, does not automatically reserve the variable name for use in all scopes. You still need to explicitly call the global variable inside lower scopes to make use of it.
function Test()
{
echo $GLOBALS['foo'];
// Or less clear, use the global keyword
global $foo;
echo $foo;
}
It's even possible to have both a local and a global $foo in the same function (though not at all recommended):
$GLOBALS['foo'] = "foo! :)";
function getFoo()
{
$foo = "boo :(";
echo $GLOBALS['foo'] . "\n"; // Global $foo
echo $foo; // Local scope $foo since it has no global keyword
}
getFoo();
// foo! :)
// boo :(
Review the PHP documentation on variable scope and the $GLOBALS documentation for more examples.
You will need to refer to it via the $GLOBALS array all the time. Read about variable scope to get the full descriptve answer.
<?php
$GLOBALS['foo'] = "Example content<BR><BR>";
echo $foo; // that works!
Test();
function Test()
{
echo $GLOBALS['foo']; // that doesn't work!
}
?>
you can change your code to:
function Test() {
global $foo;
echo $GLOBALS['foo'];
echo $foo;
}
you have to declare which global variables you access from your PHP function. See: http://php.net/manual/en/language.variables.scope.php

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

PHP: Using a variable that are inside a function

I have:
include ('functions.php');
check_blocked();
echo $blocked;
in functions.php, check_blocked(); exists. Inside check_blocked I got:
global $blocked;
$blocked = '1234';
I want to echo $blocked variable, that are inside check_blocked().
It doesnt work, no output..
This is an example of my original problem, so please dont say that I could just have the echo inside the function, as I cannot have in my original code.
Your code should look like
$blocked = 0;
include('functions.php');
check_blocked();
echo $blocked;
With functions.php looking like
function check_blocked(){
global $blocked;
$blocked = 1234;
}
You must define blocked outside of the scope of the function before you global blocked into the function.
Why not just returning the value?
function check_blocked() {
$blocked = '1234';
return $blocked;
}
then
include ('functions.php');
echo check_blocked();
Avoid globales wherever possible.
You can change check_blocked() to return $blocked or you can try use:
include ('functions.php');
check_blocked();
global $blocked;
echo $blocked;
instead, use a return value. globals are bad.
include ('functions.php');
echo check_blocked();
function check_blocked() {
return '1234';
}
Unless the variable is declared outside of the check_blocked() function, you can't.
$foo = null;
function check_blocked() {
global $foo;
$foo = "Hello";
// do some stuff
}
global $foo;
echo $foo; // prints "Hello"
If its defined inside the function like:
function check_blocked() {
global $foo;
$foo = "Hello";
// other work
}
global $foo;
echo $foo; // Nothing
then the variable is scoped locally, and is discarded as soon as the function completed.

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