PHP Variables & Constants

Previously we have learnt the basic structure of PHP, here we will discuss about variables and constant in this tutorial.

Variable

As human mind has memory cells inside the brain for remembering the values  similarly the computer too has memory for storing values.

A memory in computer is divided into number of cells each having a unique address which is used for differentiating each cell from another. The problem with such address is its numeric and it’s almost impossible to remember address of each memory cell so there needs another way through which we can access memory.

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.

Variables in PHP

PHP is loosely typed language , it doesn't require a variable to be defined.  In PHP we need not to specify the data type of a variable PHP automatically converts the given variable to the correct data type when a value is given to variable.

For Ex

<?php
         $a=”Pratik”;
         $b=12;
?>

As you can see in the above example $a & $b are variable, and  if you have noticed we have not specified data type for variables instead  we have directly assigned some value to variable , in our case here $a is of type String where as $b is of type Integer.

Rules for creating variables

  • A variable name is always preceded by $ (dollar) sign.
  • Variable name always starts with letter.
  • The variable name can consist of digits and letters.
  • No special symbols other than underscore ( _ ) is used.
  • Space is not allowed in variable name.
  • Variable name can’t be keyword.
  • The maximum length allowed for a variable name is 31 characters  

Examples of valid Variable names are:

$subject_1

$name

$n1

Examples of invalid Variable names are:

$1name (1 is used as 1st character of variable name)

$first name (space is not allowed)

$if (keywords not allowed)

$%ab (special symbol (%) other than underscore)

Outputting Variables

<?php
                $name=”Hello DiscussDesk”;
                echo $name;
? >

As shown in example for outputting a variable echo is used.

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

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 Ex.

<?php
       function myfunc()
      {
                $a=10;
                echo $a;
      }
      myfunc();
      // echo $a;         
?>

Here in this example $a is declared inside function myfunc() 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.

Global Variable

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

<?php
       $a=10; // global variable
       function myfunc()
       {
                echo $a;
       }
       myfunc();
       echo $a;              
?>

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.

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 Ex

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

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.

Constant in PHP

Constant is a special type of variable whose value remains fixed i.e it can’t be changed.

Syntax for creating Constants

define(var_name,value)

here var_name is name of constant variable, value is the value assigned to the variable

For Ex.

<?php
     define(name,”Learn PHP on DiscussDesk”);
     echo name;
?>

So to conclude we have learnt variables, its types and constant in this tutorial.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments