How to Create a variable in PHP

A variable is used to store information and it is accessible later in the program.

It has the user-defined name.

In PHP you don’t need to specify the Datatype of a variable it automatically converts its type.

In PHP there are different types of variable are available –

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

How to Create a variable in PHP


Contents

  1. Rules
  2. Create variable
  3. Local variable
  4. Global variable
  5. Static variable
  6. Conclusion

 

1. Rules

  • A variable starts with the $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alphanumeric, character and underscores ( A-Z,0-9 and _ ).
  • Variable names are case sensitive ( $x and $X ) are two different variables.

 

2. Create variable

A variable is declared using $ following name of the variable and no need to define the datatype.

Syntax – 

$variablename;

Value is assigned with = operator.

Syntax –

$variablename = value;

Example – 

<?php
$num;
$num = 45;
$text = "Hello World";

echo "num : ".$num;
echo "<br>text : ".$text;

Output

num : 45
text : Hello World

 

3. Local variable

This type of variable scope only within the block. They are not accessible outside of a block.

Local variable gets destroyed on the function exit.

<?php

function addOne(){
  $num1 = 5; // Local variable
  $num2 = 10;  // Local variable
  $sum = $num1 + num3; // Local variable
  echo 'sum : '.$sum;
}

 

4. Global variable

This type of variable scope is within and outside of the block.

Normally if some variable is defined inside the function then its value not accessible outside of it.

Example

In the example, try to access the $sum value. When the program is run then it will show  Notice: Undefined variable: sum.

<?php

function sum(){
  $num1 = 10;
  $num2 = 5;
  $sum;
  $sum = $num1 + $num2;
}
sum();
echo 'sum : '.$sum;

In this type of cases, the global variable is used to increase the scope of a variable usage.


Define global variable

A global variable defined by adding global keyword at the front of a variable.

Syntax –

global $variablename;

Example

<?php

function sum(){
  $num1 = 10; // Local variable
  $num2 = 5; // Local variable
  global $sum; // Global variable
  $sum = $num1 + $num2;
}
sum = 5;
sum();
echo 'sum : '.$sum;

sum = 3;
sum();
echo "<br>sum : ".$sum;

Output

sum : 15
sum : 18

 

5. Static variable

A static variable is a variable that doesn’t lose its value on function exit.

Syntax –

static $variablename = value;

Example

<?php

function addOne(){
  static $num = 0;
  $num++;
  echo 'num : '.$num.'<br>';
}
addOne();
addOne();
addOne();

Output

num : 1
num : 2
num : 3

 

6. Conclusion

Use the variables to store data if you want to use it later in the program.

You don’t need to define the datatype of a variable because it is auto-initialized according to assigned value.

Only use the global variable when require otherwise avoid it and use the local variable or pass the variable as the parameter in the function.

Related Post

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  

Be First to Comment

Leave a Reply

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