PHP - Variables



Variables in PHP are used to store data that can be accessed and modified across the program. A variable can store a wide range of values, like numbers, text, arrays and even objects. One of PHP's unique features is that it is a loosely typed language, which means you are not required to declare the data type of a variable when you create it. PHP defines the variable's type based on the value assigned to it.

This freedom makes PHP easier to use, particularly for beginners, because it allows you to store and manage a variety of data types without having to design them yourself.

Table of Content

How to Declare a Variable in PHP?

To declare a variable in PHP, just assign a value by typing the $ symbol followed by the variable name. PHP variables are case-sensitive and should begin with a letter or an underscore, followed by any number of letters, numbers or underscores.

Syntax

   $variable_name = value;

Example

Here is an example showing how to declare variables in PHP −

   // A string value
   $name = "John";  
   
   // A number (integer)
   $age = 25;       
   
   // A decimal number (float)
   $price = 12.50;  

PHP Variable Rules

Here is the list of rules to define a variable in PHP −

  • A variable must start with a $ symbol, then its name.

  • The variable name must start with a letter or underscore (_).

  • A variable name cannot start with a number.

  • The variable name can contain letters, digits or underscores.

  • PHP is case sensitive, so $Name and $name are distinct variables.

Example

See the below example showing how both the variables $Name and $name are different to each other and see the output of the code.

   $Name = "Amit";
   $name = "Samay";
   echo $Name;  
   echo $name;  

Output

Following is the result of the above code −

Amit
Samay

Variable Types in PHP

In PHP, the primary variable types are string, integer, float (also known as double), Boolean, array, object, null, and resource. Below is the example of each type of variable.

  • String : A sequence of characters.

  • Integer : A whole number (without decimals).

  • Float (Double) : A decimal number.

  • Boolean : Represents true or false values.

  • Array : Stores multiple values in one variable.

  • NULL : Represents a variable with no value.

Automatic Type Conversion of Variables

PHP does a good job of automatically converting types from one to another when necessary. In the following code, PHP converts a string variable "y" to "int" to perform addition with another integer variable and print 30 as the result.

Take a look at this following example

<?php
   $x = 10;
   $y = "20";

   echo "x + y is: ", $x+$y;
?>

Output

It will generate the following outcome −

x + y is: 30

Variables are Assigned by Value

In PHP, variables are always assigned by value. If an expression is assigned to a variable, the value of the original expression is copied into it. If the value of any of the variables in the expression changes after the assignment, it doesnt have any effect on the assigned value.

<?php
   $x = 10;
   $y = 20;
   $z = $x+$y;
   echo "(before) z = ". $z . "\n";

   $y=5;
   echo "(after) z = ". $z . "";
?>

Output

It will produce the below result −

(before) z = 30
(after) z = 30

Assigning Values to PHP Variables by Reference

You can also use the way to assign values to PHP variables by reference. In this case, the new variable simply references or becomes an alias for or points to the original variable. Changes to the new variable affect the original and vice versa.

To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).

Take a look at this following example

<?php
   $x = 10;
   $y = &$x;
   $z = $x+$y;
   echo "x=". $x . " y=" . $y . " z = ". $z . "\n";

   $y=20;
   $z = $x+$y;
   echo "x=". $x . " y=" . $y . " z = ". $z . "";
?>

Output

It will produce the following output −

x=10 y=10 z = 20
x=20 y=20 z = 40

Variable Scope

Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −

Advertisements