PHP Constant and Variable

0 claytonvarley6 December 7, 2020

Variables are the symbolic name which is used to store data item, i.e. a numerical quantity or a character constant. Variables are defined in a computer to represent an item of data input by the user,  any intermediate calculations, or the ends results. Unlike a constant, the value of variables can change during the execution of the program. The same variable can store different values at different portions of a program.

Variables name may consist of letters, digits, or underscore characters.

Variable Declaration:

Any program should be defined before using it in a program. The variables are defined or declared using a $ sign followed by variable name as shown in the following syntax:

$variable_name = variable_value;

For Example:

$chp = 100;
$string = "Code Help Pro";

Rules For Variable Declaration:

  • The declared variable must start with the $ sign, followed by the variable name.
  • The variable name should be started with only letters or underscore.
$ab = 10;       //valid
$1ab = 20;   //invalid
  • White spaces are not allowed between characters of variables, but underscores are allowed.
$a b = 10;      // invalid
$ab = 10;     // valid
  • The variable name is case sensitive, i.e uppercase and lowercase are different. Thus variable $temp is not the same as $Temp or $TEMP.

PHP Variable: Declaring String, Integer, and float

In most programming languages, We have to declare the data type but here, we will not declare the data type to store the string or integer, or float. We can hold string, integer, and float in the following ways.

$string_var = "Code Help Pro";
$int_var = 20;
$float_var = 20.5;

PHP Variable : Scope

The scope of variables is to define as its range up to which it can access inside the program. We have categorized variables on the basis of scope.

  • Global Variables
  • Local Variables

Global Variables:

The global variables are those variable which are declared outside the function and can be accessed throughout the program. To access the global variable inside the function, We have to use the global keyword. Let’s take an example to clarify this statement.

<?php
    $website = "Code Help Pro";

    function getWebsite(){
         global $website;
        echo "Website name called inside the function : $website";
    }

    getWebsite();
    echo "Website name called outside the function : $website";
?>

The output of the above program is:

Website name called inside the function : Code Help Pro
Website name called outside the function : Code Help Pro

We can also access the global variable without using the global keyword. We can also access the global variable using a pre-defined $GLOBAL array.

<?php  
    $num1 = 6;      //this is global variable  
    $num2 = 5;     //this is global variable  
    function add_global_var()  
    {  
            $sum = $GLOBALS['num1'] + $GLOBALS['num2'];  
            echo "Sum of global variables is =  " .$sum;  
    }  
    add_global_var();  
?>  

The output of above program is

Sum of global variables is =  11

Local Variables:

Local Variables are those variable which is declared inside the user-defined function, main loop, or any control statement that have limited score inside the braces. Let’s take an example to clarify this statement.

<?php  
    function get_local_var()  
    {  
        $num = 7;  //this is local variable  
        echo "Local variable declared inside the function : ". $num;  
    }  
    get_local_var();  
?>  

The output of above program is

Local variable declared inside the function : 7

PHP Constant

Constants are the name or identifiers that can’t be changed during the execution of program. There are two ways to defined the constant in PHP.

  • Using defined() function
  • Using const Keyword

We can access the constant throughout the program. We can also use constant inside the function without using any keyword such as global.

PHP Constant: define() function

We can declare the constant using define() function. Let see an syntax for define() function.

define(name, value, case-insensitive) ;

Lets see an example to make clear concept on define() function.

<?php define("WEBSITE", "CODE HELP PRO"); ?>

We have defined the WEBSITE constant. Now, We can use WEBSITE as a variable anywhere inside the function or outside the function.

PHP Constant: const Keyword

We can also declare the constant using the const keyword. Let see an syntax for the const keyword.

<?php const WEBSITE = "Code Help Pro"; ?>

We have also defined the WEBSITE constant. Now, We can use WEBSITE as variable anywhere inside the function or outside the function.