Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Introducing Functions Introducing Functions

Can someone explain to me more details about ¨global¨ in php

I don't Understand very well that global statement :(

1 Answer

Filipe Pacheco
seal-mask
.a{fill-rule:evenodd;}techdegree
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 Points

Hi Stefano Salazar , when you have a function, anything that is inside it, is specifically for that function. If you want to get something that is outside, to use inside that function, such as a variable, you use the "global" statement. Basically, you use the "global" statement to get anything that is outside, to use inside the function.

In the following example, $current_user is a variable created and used outside the is_mike() function. So for that function to know who the current user is, you need to use it inside the function, and for that you use the global statement followed by the variable name.

$current_user = 'Hampton';

function is_mike(){
  global $current_user;
  if($current_user == 'Mike'){
    echo 'It is Mike!';  
  } else {
    echo 'Nope, it is not Mike.';  
  }
}

is_mike();

Thanks Filipe! :) got it now!