Crus4

logo

How to Declare a Variable in PHP


Variables are containers that temporary holds any type of value. To declare a Variable in PHP, we have to use a $ sign followed by the the variable name and assign it to any value. Here is a Syntax.

Syntax

$VariableName = "value"; 

In an above syntax you have get an idea how you can declare a variable and assign it a value. Now to understand it clearly let’s write a code below, where we declare and print a variable.

Example

<?php
$name = "John"; 
$age = 19;
echo $name;  // Outputs: John
echo $age;  // Outputs: 19
?>

Example Explained

In an above example we have declared a variable name and assign it to a string value John and variable age and assign it to an integer value 19. Then we use the PHP output statement ‘echo‘ to output the value of $name and $age variables.

Remember that PHP string values are always enclosed with single or double quotes. Likewise, we enclose the John value between the double quotes. If you didn’t enclose your PHP string values between the single or double quotes you will get an error.

PHP is a case sensitive language, so the variable $name and the variable $Name are two different variables.

Rules for PHP Variable Names

  • In PHP, Variable names always include letters, numbers and underscores ‘_’.
  • Variable always start with a letter or an underscore but not with an number.
  • Variable names cannot include spaces or any special character like ( $, %, #).

Below is an example of a PHP valid and invalid variables.

Example

<?php
$UserName = "John";  //Valid variable name
$User Name = "John"; //Invalid variable name
$U12 = "Hello"; //valid variable name
$12U = "Hello"; //Invalid variable name
?>

PHP Variable Scope

In PHP variables can have either Local or global scope. This scope of a variable depends upon where they are declared. Here is the difference between Local and Global variable in PHP.

Difference Between Local and Global Variables

S. No.Local Variables Global Variables
1. Local variables are declared within a function or block of code.Global variables are declared anywhere in the PHP script.
2.They cannot be accessed outside the function or block where they are declaredThey can be accessed anywhere in the PHP script, both inside and outside the function.
3.Local variables have a limited lifetime. They are created when the function is entered and destroyed when the function exits.Global variables can be executed throughout script.
Difference between local and global variables in PHP.

To understand it clearly let’s write a code in a below example and clear your all doubts about PHP local and global variables.

Example

<?php
$GlobalVar = "This is a Global variable";
function FunctionName(){
  $LocalVar = "This is a Local Variable";

}
echo $GlobalVar;  //Outputs: Global Variable
echo $LocalVar; //Outputs: Undefined variable  $LocalVar
?>

Example Explained

In an above example we have first declared a variable namely $GlobalVar and assign it to a string value “This is a Global Variable”. Then we create a function namely FunctionName and inside it we declare a variable namely LocalVar and assign it to a string value “This is a Local Variable”. Then outside the function we access both the variables and you have seen when we try to access local variable namely $LocalVar outside the function, it shows “$LocalVar is not defined “.

Accessing Global Variable inside a Function

In an above code if you try to access a Global variable namely $GlobalVar inside a function, like this:

<?php
$GlobalVar = "This is a Global variable";
function FunctionName(){
  $LocalVar = "This is a Local Variable";
  echo $GlobalVar;

}
echo $GlobalVar;  //Outputs: Global Variable
echo $LocalVar; //Outputs: Undefined variable  $LocalVar
FunctionName(); // Outputs:  Undefined variable  $GlobalVar
?>

It will show variable $GlobalVar is undefined. To access the global variable inside a function you have to use a Global keyword followed by the variable name. Here is an example

<?php
$GlobalVar = "This is a Global variable";
function FunctionName(){
  $LocalVar = "This is a Local Variable";
  Global echo $GlobalVar;

}
echo $GlobalVar;  //Outputs: Global Variable
echo $LocalVar; //Outputs: Undefined variable  $LocalVar
FunctionName();
?>

Now with the help of this Global keyword you can access this $GlobalVar variable inside the function.

PHP Constants

PHP Constants are variables too, but the difference is that the value of a constant cannot be changed during the whole script. To create a constant we have to use a define() keyword and inside it we can add the name and it’s value. Here is a Syntax:

Syntax

<?php
define(name, value)
?>

In an above syntax name defines the name of a constant and the value defines it’s value. Rules for naming a constant is same as naming a variable. You can set the value of constant anything you want, it may be a string value or an integer value.

Here is an example to define a constant.

Example

<?php
define("conName", "This is a constant");
echo conName;  // Outputs: This is a constant
?>

If you try to assign a different value to a constant you will get an error. Here is an example:

<?php
define("conName", "This is a constant");
echo conName;  // Outputs: This is a constant
define("conName", "This is new value to a constant");
echo conName; 
// Outputs: Constant conName already defined
?>

Are Constants Local or Global

Constants are automatically global, that means you can access them anywhere in the script. Here in an example below let’s create a constant and try to access it inside a block of code (Function).

Example

<?php
define("conName", "This is a constant");
echo conName;  // Outputs: This is a constant
function my_func() {
  echo conName;

}
my_func(); // Outputs: This is a constant
?>

Example Explained

In an above example we have created a constant namely conName . Then we have created a function with the name of my_func and inside the function we use the ‘echo’ to output the value of constant. At the end when we call our function, it outputs the value of a constant “This is a constant”.

NOTE:- We will learn about functions in our upcoming posts, for now you don’t need to go deeper in it.


Share This Post!

How to Declare a Variable in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *