PHP Variables and Constants

php-variable-and-constant-programming-pot
••• PHP Variables and Constants

After learning the PHP Keywords In this post, we learn about the PHP Variables and Constants, and also you will learn how to learn to store information in a variable and use constants for storing fixed values in PHP.

PHP Variables and Constants

PHP Variables


Variables are used to store the data, string, text, and numbers. Variable values can change over the course of a script. Here’re some important things to know about variables:

  • In PHP, a variable can not need to be declared before adding a value. PHP automatically converts the variable to the correct data type, depending on its value.
  • After declaring a variable it can be reused throughout the code.
  • The assignment operator (=) used to assign the value to a variable.

Variable comes to the rescue, A variable is name given to memory location which is used to store particular value. These values may be fixed or it can be dynamic.

In PHP variable can be declared as: $var_name = value;

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

PHP Variables and Constants

Naming Conventions for PHP Variables


These are the following rules for naming a PHP variable:

  • It starts with the $ sign, followed by the name of the variable
  • Name must start with a letter or the underscore character
  • Name cannot start with a number
  • It can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)
Remember that PHP variable names are case-sensitive.
# Example:
<?php 
// Declaring variables
 $txt = "Hello World!";
 $number = 10; 
// Displaying variables value
 echo $txt; 
// Output:
 Hello World! 
echo $number; 
// Output: 
10 
?>

In the above example, we have created two variables where the first one has assigned with a string value and the second has assigned a number. Later we’ve displayed the variables values in the browser using the statement. echo The PHP echo statement is often used to output data to the browser. We will learn more about this in the upcoming chapter.

Variable Types


The variables can be declared anywhere in your PHP program. Based on the declaration of your variable scope is decided for each variable. Scope of a variable means the part of program where variable can be accessed.

Based on scope there are three types of variables.

  1. Local variable
  2. Global variable
  3. Static variable

1. Local Variable

A variable which is declared within any function is known as local variable , it has local scope meaning it cannot be accessed outside the function.

#For Example:

<?php
       function localVariable()
      {
          $a = 6;
          echo $a;
      }
      localVariable();
       echo $a;  
   // Output
      6    
?>

Here in this example $a is declared inside function localVariable() so it can not be accessed outside the function body, so if we try to uncomment the code in our program it will not print the value of $a as its accessed outside the block.

2. Global Variable

A variable declared outside any function has global scope so it can be accessed from anywhere in the program.

#For Example:

<?php
       $a = 6; 
      // global variable
       function globalVariable()
       {
         echo $a;
       }
       globalVariable();
       echo $a;  
      // Output
        6           
?>

Here in this example $a is declared outside the function so it has global scope and it can be accessed inside function as well as anywhere outside in the program.

3. Static Variable

The variable declared with keyword static is known as static variable , it has local scope meaning it can not be accessed outside the function , the benefit of static variable is it retains its value between function calls.

#For Example:

<?php
       function staticVariable()
       {
          static $a = 6;
          echo $a;
          $a++;
       }
       staticVariable();
       staticVariable();
       staticVariable();
?>

For this example the value of $a will be initialized only once for all function calls and the value will be retained between function calls so the output will be 10 for first call , 11 for second call and 12 for the third call.

PHP Constants


Constant is a name or an identifier for a fixed value. Constant are like variables except for that one they are defined so they can not be undefined or changed.

PHP Variables and Constants

A constant is an identifier (name) for a simple value. Value not changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Constants are very useful for storing data that doesn’t change while the script is running. Common examples of such data include configuration settings such as database username and password, website’s base URL, company name, etc.

Constants are defined using PHP’s define() function, which accepts two arguments: the name of the constant, and its value. Defined the constant value the we can be accessed at any time just by referring to its name. Here is a simple example.

Create a PHP Constant


To create a constant, use the define() function.

   define(name, value, case-in-sensitive)

Parameters:

  1. name: Specifies the Name of the constant
  2. value: Specifies the Value of the constant
  3. case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
# Example:
<?php 
    define("GREETING", "Welcome to programmingpot.com !"); 
    echo GREETING; 
   // Output
    Welcome to programmingpot.com !
?>

Naming Conventions for PHP Constants


Name of constants must follow the same rules as variable names, which means a valid constant name must starts with a letter or underscore, followed by any number of letters, numbers or underscores with one exception: the $ prefix is not required for constant names.

So this way, we can use the PHP Variables and Constants in our PHP programming. The PHP Variables and Constants are very important for the core PHP programming. So the PHP Variables and Constants is very important.


Also Read:

PHP Switch Case Statement

PHP Control Structures