phpif-statementglobal-variablesaccessible

Is it possible do use global variables in IF's?


Can somehow a variable defined in a part of an IF be accessible in another part of the same IF?

Ex:

if ($a == 1)
{
  $b = "ABC";
}
elseif ($a == 2)
{
 echo $b;
}

In functions i use global $variable but in IF-statements i dont know.

The reason why i'm asking this its because i'm making a registration page step-by-step.
It means that i need to check for that If-statement a lot of times and in my final step i need to gather all variables from all IFs.


Solution

  • There are no "global" variables the way you understand them.
    All the PHP variables doomed to die with all the PHP script after it's execution.

    You need some storage to keep your variables between requests.
    PHP sessions is a good choice.