Access a Global Variable in a PHP Function

Access a global variable in a PHP function

To address the question as asked, it is not working because you need to declare which global variables you'll be accessing in the function itself:

$data = 'My data';

function menugen() {
global $data; // <-- Add this line

echo "[" . $data . "]";
}

menugen();

Otherwise you can access it as $GLOBALS['data'], see Variable scope.

Even if a little off-topic, I would suggest you avoid using globals at all and prefer passing data as parameters.

In this case, the above code look like this:

$data = 'My data';

function menugen($data) { // <-- Declare the parameter
echo "[" . $data . "]";
}

menugen($data); // <-- And pass it at call time

Access global variable in function

@Adrien hope you want to access the variables inside the function which are outside the function so you can use $GLOBALS it's a super global variable which contain reference of all the variable present in the current script file ($GLOBALS an array which store variable name as key and variable value as value of key)

so try it like below:

Vars.php

$test = 'test';

Function.php

<?php
include 'test.php';
function add_content(){
echo 'This is a '. $GLOBALS["test"]; //$test variable store in this with the variable name test as key and value of the variable as value of key
}
add_content( 'woocommerce_single_product_summary', 'add_content');

Can't access global variable inside function

You have to pass it to the function:

<?php
$sxml = new SimpleXMLElement('<somexml/>');

function foo($sxml){
$child = $sxml->addChild('child');
}

foo($sxml);
?>

or declare it global:

<?php
$sxml = new SimpleXMLElement('<somexml/>');

function foo(){
global $sxml;
$child = $sxml->addChild('child');
}

foo();
?>

If the variable isn't global but is instead defined in an outer function, the first option (passing as an argument) works just the same:

<?php
function bar() {
$sxml = new SimpleXMLElement('<somexml/>');
function foo($sxml) {
$child = $sxml->addChild('child');
}
foo($sxml);
}
bar();
?>

Alternatively, create a closure by declaring the variable in a use clause.

<?php
function bar() {
$sxml = new SimpleXMLElement('<somexml/>');
function foo() use(&$xml) {
$child = $sxml->addChild('child');
}
foo();
}
bar();
?>

Accessing variable inside function - php

The preferred option is to pass as a parameter:

function fun($local) {
print_r($local['words']);
}

fun($global);

If for some reason you can't use that approach, then you can declare the variable as global:

function fun() {
global $global;
print_r($global['words']);
}

fun();

Or use the $GLOBALS array:

function fun() {
print_r($GLOBALS['global']['words']);
}

fun();

But in general, using global variables is considered bad practise.

Having a PHP function use a global variable as a default?

You could use a constant. Define it at the top of a file and let your functions use that. E.g.

define('CUSTOM_TIMEZONE', 'Mountain');

function tell_time($values, $timezone = CUSTOM_TIMEZONE) {
// Your code here
}

Just change the constants value and it's changed everywhere.



Related Topics



Leave a reply



Submit