Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global keyword outside the function in php

Tags:

php

As we know, global keyword makes variable( or object, array) visible inside current function we are dealing with

<?php

$some_var = 'some string';

function __test(){
    global $some_var;
    echo $some_var; // some string
}

But some dev's still use the global keyword outside the functions at first look it doesn't make any sense to me.

Well, the question is: Does it make any sense to use 'global' keyword outside the function ???

like image 427
Yang Avatar asked Apr 25 '12 00:04

Yang


People also ask

Can I use global variable in function PHP?

Global variables refer to any variable that is defined outside of the function. Global variables can be accessed from any part of the script i.e. inside and outside of the function. So, a global variable can be declared just like other variable but it must be declared outside of function definition.

What does global keyword do in PHP?

The global keyword imports variables from the global scope into the local scope of a function.

Which keyword is used to access global variables PHP?

PHP The global Keyword The global keyword is used to access a global variable from within a function.

Can we declare variable outside function?

In Python and most programming languages, variables declared outside a function are known as global variables. You can access such variables inside and outside of a function, as they have global scope. The variable x in the code above was declared outside a function: x = 10 .


2 Answers

Global keyword outside of functions does not do anything at all. but that file might be included within a function.

Another thing i use it not is to make my codes readable and more structured. any variable that i want to bo accessed using global keyword in a function, i declare it using global in the main file, so just a quick glance and i know that it is referenced somewhere as a global. (meaning don't rename anyhow as other is used somewhere else... :) )

like image 37
iWantSimpleLife Avatar answered Nov 15 '22 18:11

iWantSimpleLife


From the docs:

Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.

Essentially you could have your function guts in a different file than your function declaration. These guts would then be included into the function. This would give the impression, if you view the guts alone, of a user using global outside of a function, however the fact is that when this code is interpreted, it will be interpreted from within a function.

$name = "Jonathan";

function doSomething () {
  include( 'functionGuts.php' );
}

Where the contents of our functionGuts.php file could be:

global $name;
echo "Hello, " . $name;

When viewed on its own, functionGuts.php will give the impression that global is being used outside of a function, when in reality it's being used like this:

$name = "Jonathan";

function doSomething () {
  global $name;
  echo "Hello, " . $name;
}
like image 77
Sampson Avatar answered Nov 15 '22 17:11

Sampson