How to Declare a Global Variable in PHP

How to declare a global variable in php?

The $GLOBALS array can be used instead:

$GLOBALS['a'] = 'localhost';

function body(){

echo $GLOBALS['a'];
}

From the Manual:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.


If you have a set of functions that need some common variables, a class with properties may be a good choice instead of a global:

class MyTest
{
protected $a;

public function __construct($a)
{
$this->a = $a;
}

public function head()
{
echo $this->a;
}

public function footer()
{
echo $this->a;
}
}

$a = 'localhost';
$obj = new MyTest($a);

How to make global variables in php

Your question is kinda ambiguous for me. If you mean setting a variable from one file then making it available from all your php files you can put it in one file then include the file it in all your scripts. Put it on the top. Or you can put it inside an $_SESSION variable. This is the simplest solution I know. :)

Sample: Assuming all files are in the root directory

vars.php:

$globalVar = 2;
//or
$_SESSION['global_var'] = 2; // if a session exists

otherfile.php:

include 'vars.php';
echo $globalVar; //outputs 2
//or
echo $_SESSION['global_var']; //outputs 2

I'm not sure if this is what you are trying to achieve :D

PHP define global variables for functions?

I suggest that you store your array globally using the $GLOBALS, and instead of using echo to print an array, use the print_r method instead.

<?php
$GLOBALS["titleArray"] = array('world');

function foo(){
print_r($GLOBALS["titleArray"]);
}

function boo(){
array_push($GLOBALS["titleArray"], "Hello");

print_r($GLOBALS["titleArray"]);
}

foo();
boo();

PHP - declare a global array

You can also pass the variable into the function:


$prev = [0,1,2];

function hello(array $array){

echo $array[1];

}

hello($prev);
hello($prev);
hello($prev);

?>

An other way is to pass the variable by reference.

function hello(&$array){

$array[1]++;
echo $array[1];

}

Declaring a global variable inside a function

you have to define the global var in the second function as well..

// global scope
$ref_id = 1;

grabReferral($rid){
global $ref_id;
$ref_id = $rid;
}

someOtherFunction(){
global $ref_id;
sendValue($ref_id);
}

felix

How to assign value to global variable in PHP

There is no combined "declare global and assign value" statement in php.

You'd have to do that in two steps, e.g.

<?php
function foo($str) {
global ${"check" . $str};
${"check" . $str} = "some value";
}

foo('bar');
echo $checkbar;

...but what you really should do is: avoid globals.

PHP global variable is null inside a function, but is not null outside it

$COMPONENT_NAME is not a global variable in getComments. Although you have declared it global in permissionsAllowed, it is not declared global in getComments and thus $COMPONENT_NAME in getComments is local to that scope and hence not visible via global $COMPONENT_NAME in permissionsAllowed.

Consider the following code (demo):

$b = 5;

function f1 () {
global $a;
$a = 4;
$b = 3;
$c = 2;
f2();
}

function f2 () {
global $a, $b, $c;
var_dump($a);
var_dump($b);
var_dump($c);
}

f1();

Output:

int(4)
int(5)
NULL

$a is not declared at the top level but is declared global in f1 and f2 - modifications made to it in f1 can be seen in f2.

$b is declared at the top level, but only global in f2. $b in f1 is local to that scope and changes to it have no effect on $b in f2.

$c is not declared at the top level and is only global in f2. Again, $c in f1 is local to that scope and changes to it have no effect on $c in f2, so the global $c that is referred to in f2 has no value (null) since it is not set anywhere.

How do declare global variable in PHP class

First up, you're not trying to create a "global variable" (as your title suggests), but a private member variable.

In order to do that you need to declare the private member variable outside of the constructor:

class Auth extends Controller {

private $pass;

function __construct(){
}

function auth()
{
parent::Controller();
$this->load->library('session');
$this->load->helper('cookie');
$this->load->library('email');
}
function index(){
..........
}
function loging(){
$this->pass = "Hello World";
}
function test(){
echo $this->pass;
}
}

Also:

  • Correct the name of your constructor
  • Chose a naming convention (e.g. lowercase first character of function names) and stick to it.

As a trivial answer / example of what you are asking. Try this:

<?php

class Auth {

private $pass;

function __construct(){
}

function loging(){
$this->pass = "Hello World";
}
function test(){
echo $this->pass;
}
}

$oAuth = new Auth();
$oAuth->loging();
$oAuth->test();

?>

It outputs:

C:\>php test.php
Hello World

How to declare global variable and initialization

This isn't a global variable, it's called a class property, which is defined in the class (see http://php.net/manual/en/language.oop5.properties.php)

When accessing these types of variables, you have to tell PHP which object contains the variable your referring to, and when it's the current object you have to use $this. So you class should be something like...

class CheckVerify extends Controller {
private $client;
public function __construct()
{
$this->client = app('Nexmo\Client');
}

public function mobile_verification($number)
{
$verification = $client->verify()->start([
'number' => $number,
'brand' => 'Mysite'
]);
}

public function check_verify($code)
{
$this->client->verify()->check($verification, $code);
}
}

As an extra option - consider rather than hard coding the value in the constructor ...

$this->client = app('Nexmo\Client'); 

Pass this in as a value to the constructor...

public function __construct( $client ) {
$this->client = $client;
}

This is called dependency injection (DI) and allows much more flexibility.



Related Topics



Leave a reply



Submit