Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you create a variable inside a if statement is it available outside the if statement?

Tags:

variables

php

If you have an if statement like this:

<?php
$a = 1;
$b = 2;
if ($a < $b) {
$c = $a+$b;
}
?>

Would you be able to access the $c variable outside of the if statement like so:

<?php
$a = 1;
$b = 2;
if ($a < $b) {
$c = $a+$b;
}
echo $c
?>
like image 902
Harigntka Avatar asked Jun 05 '11 15:06

Harigntka


People also ask

Can you assign a variable inside an if statement?

Yes, you can assign the value of variable inside if.

How do you use a variable outside of an IF statement?

If you define the variable inside an if statement, than it'll only be visible inside the scope of the if statement, which includes the statement itself plus child statements. You should define the variable outside the scope and then update its value inside the if statement.

Are variables declared inside of an IF block visible outside of the if statement?

Answer. No, variables declared inside an if statement would not be accessible directly outside an if statement because if statements have their own scope.

Can you declare a variable in an if statement in C?

No. That is not allowed in C. According to the ANSI C Grammar, the grammar for the if statement is IF '(' expression ')' statement . expression cannot resolve to declaration , so there is no way to put a declaration in an if statement like in your example.


3 Answers

PHP's scope is completely function-based. It's not the same as C or Java where it's local to what block that variables are nested in.

For PHP's scope:

// Global variable
$a = 0;

function f()
{
  // Cannot be accessed outside of f()
  if (true)
    $b = 0;

  // However, it can still be accessed anywhere in f()
  $b += 1;
}

If you want a variable to be global, simply use the global keyword:

// Global variable
$a = 0;

function f()
{
  // Use $a from global scope
  global $a;

  // Modifies global $a
  $a += 1;
}

function g()
{
  // Use $b from global scope, even though it hasn't been defined yet
  global $b;

  // Can be accessed outside of g()
  $b = 0;

  // Cannot be accessed outside of g(); this $a "shadows" the global version
  // The global $a is still 0
  $a = 1;
}
like image 45
RétroX Avatar answered Oct 09 '22 16:10

RétroX


In PHP, if doesn't have its own scope. So yes, if you define something inside the if statement or inside the block, then it will be available just as if you defined it outside (assuming, of course, the code inside the block or inside the if statement gets to run).

To illustrate:

if (true)  { $a = 5; }    var_dump($a == 5);   // true

The condition evaluates to true, so the code inside the block gets run. The variable $a gets defined.

if (false) { $b = 5; }    var_dump(isset($b)); // false

The condition evaluates to false, so the code inside the block doesn't get to run. The variable $b will not be defined.

if ($c = 5) { }           var_dump($c == 5);   // true

The code inside the condition gets to run and $c gets defined as 5 ($c = 5). Even though the assignment happens inside the if statement, the value does survive outside, because if has no scope. Same thing happens with for, just like in, for example, for ($i = 0, $i < 5; ++$i). The $i will survive outside the for loop, because for has no scope either.

if (false && $d = 5) { }  var_dump(isset($d)); // false

false short circuits and the execution does not arrive at $d = 5, so the $d variable will not be defined.

For more about the PHP scope, read the variable scope manual page.

like image 160
rid Avatar answered Oct 09 '22 18:10

rid


If the if statement containing the variable was executed, then yes, you can access the variable outside of the if statement. Here's a thought on why it works that way. In many programming languages you can "declare" a variable before you use it, just to let the compiler know that it's there. For example in Java you can declare an 'int', then use it like so:

int number;
if(true)
    number = 5;

In Java, you have to declare a variable like this before using it in an if-then statement. In php, however, there isn't really a way to do that. Since php is dynamically typed, you can't write int $number. In Java, the computer allocates a 32 bit block of memory (the size of an int) when the variable is declared. In php, I believe, the memory is not allocated until something is actually stored in the variable. The best equivalent to 'declaring' a php variable I could think of would just be to write:

$number;    //This is NOT needed
if(true)
    $number = 5;

But when you look at the code, it seems kind of strange to just write $number like that. I think the computer would think it was equally strange because as I said before, it is a dynamically typed language, and so it doesn't need to allocate a whole chunk of memory for the number. So you can just leave it like this:

if(true)
    $number = 5; 
like image 6
user3413723 Avatar answered Oct 09 '22 18:10

user3413723