If Statement Variable Scope In PHP

PHP Logo Variable scope is the context within your code in which a variable is defined and able to accessed. If you try to access a variable that is out of scope, the variable will be undefined and you will not get the results you are expecting.

In PHP, variables all exist within the same scope when your code is inline or included in an include or require statement. Classes and functions have their own scope. If statements do not have their own variable scope.

To better understand what all this means, here are a couple examples.

In this example we simply define a variable then access the contents of that variable. You get the expected result of being able to retrieve the expected value.

$message = 'Hello World!';
echo $message; // => Hello World!

Here, however, we are going to try to access the value of a variable defined outside of the scope of the echo statement. The result is that the variable being accessed is actually undefined.

$message = 'Hello World!';

function speak() {
  echo $message;

speak(); // => Nothing printed. $message isn't defined in the function speak()

The scope problem goes both ways. Here we’ll define a variable within a function then try to retrieve its value outside of the function. Again the variable is undefined.

function speak() {
  $message = 'Hello World;'

echo $message; // => Nothing printed. $message isn't defined outside of the function speak()

If statements do NOT have their own variable scope. This is a common “gotcha” in PHP because it is easy to assume that all code blocks set up their own variable scope. This is not the case with if/else statements.

$morning_message = 'Good Morning!';
$evening_message = 'Good Evening!';

if(date('G') < 12) {
  echo $morning_message;
else {
  echo $evening_message;

Since if/else statements do not create their own variable scope, this code will print the expected messages. Both $morning_message and $evening_message are defined in the if/else blocks even though they were declared outside the if/else block.

Likewise, you can access variables defined within an if/else block from outside of the block.

$name = 'Moses';

if(isset($name)) {
  $message = "Hello $name";
else {
  $message = "I don't know who you are.";

echo $message; // => Hello Moses

8 thoughts on “If Statement Variable Scope In PHP

  1. JP says:

    i have a code where i declared a variable inside an if statement and i have another if statement wherein i need to access the said variable i declared inside the first if statement.. problem is, i can’t get the result i need to when i try to print it to the second if statement.. how should i do it? thanks in advance! GOD bless! ^^

    • Lee says:

      It’s hard to know what to say without seeing your code but you could try declaring the variable outside of the “if” statements.

      $reward = '';
      if($behavior == 'good') {
        $reward = 'ice cream cone';
      elseif($behavior == 'excellent') {
        $reward = 'ice cream pie';
      if($store == 'open') {
        echo "Please may I have an $reward.";
  2. codywohlers says:

    I think this is the same for select-case, for and while loops as well right?

  3. Yasir says:

    Same problem with my code as well, as is said by “JP”.

  4. Dennis says:

    I’m also having the exact same issue. I can’t seem to access a variable declared in an if statement from another if statement. I have even tried moving the second if statement to another file and including it using include ‘file_name’; where the first if-statement is.

  5. ``Dennis says:

    I opted to use sessions. At the very top of the PHP file, i called this function `session_start()` then i put the variable i needed maintained after the if statement in a global associative array like this `$_SESSION[‘my_variable] = $my_variable`. On the new file, I just fetched my variable from $_SESSION[‘my_variable’].

  6. Sam says:

    To declare a variable within a function or if statement and make it global, you have to type global $x;

    if () {global x; x = “anything”}

    by doing so, you assigned the “x” variable in the global scope and can be accessed.

    *I’m not expert, just started learning PHP

Leave a Reply

Your email address will not be published.