Latest posts

10/recent/ticker-posts

What are Variables in PHP? | How to declare variables in PHP | Rules for declaring variables in PHP | Types of variables in PHP

What are Variables in PHP ?

variables it is used to store the value in a memory location. In PHP variables are declared by using "$" this dollar sign. its also used to assign value to variables.

The syntax for declaring variables in PHP:
$variable_name = value;

Example :
<?php
    $a=10;
    echo "value of a is :$a";
?>

In this example, the variable can be declared using the ' $ ' symbol. Value 10 is assigned to the variable ' a '. it means value 10 is stored in variable a. the "echo" is the method or function than can be used to print or display the output.

Some rules for  declaring variables :

  1. PHP variables are always declared by using the '$' dollar Symbol.
  2. PHP variables can start with alphabets, underscore (_), and alphanumeric but they can not start with numerical values and white spaces.
  3. It can not consist the white spaces.

Types of variables in PHP :

  1. Local Variables
  2. Global Variables
  3. Static Variables

1. Local Variables :

The variables that can be declared in a function or within a function, can be known as Local Variables. These variables can be accessed only inside the function, which can not be accessed outside the function. It is completely different than Global variables and Static variables.

Example :
<?php
    function local_test()
    {
        $a=10;
        echo "The value of a is:".$a;
    }
    local_test();
?>


2. Global Variables :

The variables that can be written outside the functions are known as Global Variables. I can be accessed anywhere in Program.
Declaration of Global Variable can be done by using the keyword GLOBAL.

Syntax :
GLOBAL $variable_name;

Example :
<?php
    $a=10;
    function test()
    {
        GLOBAL $a;
        echo "The variable of a is inside the function:".$a;
        echo "\n";
    }
    test();
    echo "The variable a outside the function:".$a
?>


Note: If you can access a variable inside a function without using the GLOBAL keyword, then it displays the output as the variable is undefined.

3. Static Variables :

The variables that can be declared inside or outside the functions and the variables whose values cannot be changeable are known as Static Variables.
Declaration of StaticVariable can be done by using the keyword STATIC.

Syntax :
STATIC $variable_name;

Example :
<?php
    $a=10;
    function static_test()
    {
        STATIC $a=1;
        echo $a;
        $a++;
    }
    static_test();
    echo "\n";
    static_test();
    echo "\n";
    static_test();
?>


PHP is a loosely typed language :

PHP is a loosely typed language, which means it's no need to declare the data type to the variable, it automatically converts the variable to the proper or correct data type.

Hope it will help you to learn PHP programming More..!

Thank You...!!!




Also Visit


Post a Comment

0 Comments