PHP Variable Declaration Tutorial

PHP - Variable Declaration

A PHP variable declaration process is used to declare the variable of type text, number, string, etc. It starts with the $ sign followed by the name of the variable.

PHP Variable is a container that is used to store information of type string of text, number, boolean, etc. The data stored in the variable can be changed and retrieved when needed.

Every variable is labeled with a meaningful name so that it could be easily understood by a screen reader.

PHP Variable's Features

  • PHP is a loosely typed language. It means that a variable does not need to be declared before adding a value to it.Php automatically converts the correct data type based on the available variable values.
  • Variable name should be meaningful.
  • It can store information of different data types.
  • It can also be reused throughout the code.
  • The variable value can be changed easily.
  • The assignment operator (=) is used to assign a value to a variable.

General Syntax

Try it now

Source Code

Try it now

PHP Variable Naming Convention

Followings are the most important rule regarding the variable naming convention.

  • Please keep in mind that PHP variables start with a $ sign, followed by the name of the variable.
  • PHP variable name must be started with a letter or underscores, followed by any number of letters, numbers, or underscores.
  • PHP variable names should not start with a number.
  • PHP declared variable must be reused throughout the code.
  • PHP variable names should not contain white spaces.
  • Please keep in mind that the PHP variable can contain alpha-numeric characters and underscores (A-z, 0-9, and _).
  • PHP variable names are case-sensitive. Please keep in mind that $age and $Age are two different variables.

Source Code

Try it now

PHP Variable - case sensitive

In PHP, variable names are case-sensitive. Therefore declaring variable name "learing_resource" is completely different from "learningResources", "learningresources",etc.

Source Code

Try it now

PHP Variable Declaration: String, Integer , Float

Let's see a PHP variable that stores string, integer, and float values.

Source Code

Try it now

PHP Valid/Invalid Syntax

PHP variables must start with a letter or underscore only.

Source Code

Try it now

PHP Variable Declaration

In, PHP creating a variable is known as declaring variables. Please keep in mind that there is no reserve keyword for declaring PHP variables hence it is known as a loose type language.

class="my-3"PHP variable declaration rule:

  • PHP variable name always lies a left side of the assignment operator.
  • The symbol = is known as the assignment operator that is used to assign provided value inside the variable.
  • =The variable lies on the right side of the assignment operator(=).

Source Code

Try it now

Please keep in mind that string value is assigned within quotes that will be either single quote or double quote.

PHP Reassigning Values to Variables

PHP variable values can be changed through the source code. Let us understand it with the help of an example.

Source Code

Try it now

Basically, reassigning means replacing the new variable value in place of the old value.

Displaying Output

The PHP echo statement is used to display output to the screen.

Source Code

Try it now

PHP variable($) and variables($$)

The $var is a normal variable with the name var that stores any value like string, integer, float, etc. And the $$var is a variable that stores the value of the $variable inside it.

Source Code

Try it now