Variable declare in PHP

Declaring (Creating) PHP Variables
  • Variables are "containers" for storing information.
  • In PHP, a variable starts with the $ sign, followed by the name of the variable.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $hlo = "Hello PHP coders!";
    $a = 10;
    $b = 20.5;
    
    echo $hlo;
    echo "<br>";
    echo $a;
    echo "<br>";
    echo $b;
    ?>
    
    </body>
    </html>
    

    Note: When you assign a text value to a variable, put quotes around the value.

    Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

  • In a PHP variables name like $a, $b, $name, $total_products, $bikename.

PHP variables Rules
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • A variable name must start with a letter or the underscore character.
  • Variable names are case-sensitive ($name and $NAME are two different variables.
  • A variable starts with the $ sign.
PHP variables execute
  • The PHP echo statement is often used to output data.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $hello = "PHP Coders";
    echo "Hello $hello!";
    
    echo "<br/>";
    $hello = "PHP Coders";
    echo "Hello " . $hello . "!";
    
    echo "<br/>";
    $a = 5;
    $b = 4;
    echo $a + $b;
    
    ?>
    
    </body>
    </html>
    


PHP Variables Scope
  • In PHP, declared PHP anywhere in the webpage so variables can be declared anywhere in the webpage.
  • The scope of a variable is the part of the script where the variable can be used.
  • PHP has three types of a declared variable.
    • Local
    • Global
    • Static
  • Example
  • 1.Local Variable
  • The variables that are declared in a function are called local variables for that function, so outside of the function that variable is not used, it will show error - Undefined variable.

    • 1. A local variable declared in function example.
    • <!DOCTYPE html>
      <html>
      <body>
      
      <?php
      function loc_var()
      {
      $ab = 2;
      echo "Local variable 'ab' is : $ab";
      } loc_var();
      ?>
      
      </body>
      </html>
      
    • 2. A local variable declared out of function example.
    • <!DOCTYPE html>
      <html>
      <body>
      
      <?php
      function loc_var()
      {
      $ab = 2;
      echo "Local variable 'ab' is : $ab";
      }
      loc_var();
      echo "Local variable out of function 'ab' is : $ab";
      ?>
      
      </body>
      </html>
      

      Output:

      Local variable 'a' is : 2
      Notice: Undefined variable: ab in C:\xampp\htdocs\example\p4.php on line 13
      Local variable out of function 'ab' is :
  • 2.Global variable
  • The global keyword is used to access a global variable from within a function

  • 1. A global variable declared, defined in function example.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $var_name = "php coders point";
    function glob_var()
    {
    global $var_name;
    echo "Variable in the function: ". $var_name;
    echo "</br>";
    }
    glob_var();
    echo "Variable outside of the function: ". $var_name;
    ?>
    
    </body>
    </html>
    
  • 2. A global variable declared, not declared in function example.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $var_name = "php coders point";
    function glob_var()
    {
    echo "Variable in the function: ". $var_name;
    echo "</br>";
    }
    glob_var();
    echo "Variable outside of the function: ". $var_name;
    ?>
    
    </body>
    </html>
    

    Output:

    Notice: Undefined variable: var_name in C:\xampp\htdocs\example\p5.php on line 9
    Variable in the function:
    Variable outside of the function: php coders point
  • 3. A global variable declared Using $GLOBALS.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $number1 = 10;
    $number2 = 15;
    function glob_var()
    {
    $mul = $GLOBALS['number1'] * $GLOBALS['number2'];
    echo "Multiplication of global variables : " .$mul;
    }
    glob_var();
    ?>
    
    </body>
    </html>
    
  • 3. global variable, local variable has higher priority than the global variable.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $number1 = 10;
    $number2 = 15;
    function glob_var()
    {
    $number1 = 10;
    $number2 = 10;
    
    $mul = $number1 * $number2."<br/>";
    echo "Multiplication of global variables in a function : " .$mul."<br/>";
    }
    glob_var();
    
    $mul = $GLOBALS['number1'] * $GLOBALS['number2'];
    echo "Multiplication of global variables out of a function: " .$mul;
    ?>
    
    </body>
    </html>
    
  • 3.Static variable
  • Normally, when a function is executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.To do this, use the static keyword when you first declare the variable

  • 1. A global variable declared, defined in function example.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    function stati_var()
    {
    static $x = 3;
    echo $x;
    $x--;
    }
    stati_var();
    echo "<br/>";
    stati_var();
    echo "<br/>";
    stati_var();
    echo "<br/>";
    ?>
    
    </body>
    </html>
    
  • Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
  • Note: The variable is still local to the function.