• Tutorial List

PHP Variable Declaration & Naming Conventions of a Variable

Last updated Mar 24, 2020

What is Variable in PHP?

PHP Variables are like containers that are used to store data or information.


How to create or declare a PHP Variable?

In PHP All variable names must start with a $ sign, followed by the name of the variable. Like this$variable_name

You can declare variables anywhere in the PHP script.


Storing/Assign Value to a Variable

The = equal sign is used to assign value for a variable.

This = equal sign is called Assignment Operator.

<?php
	$name = "John Doe"; // Declaring a variable
  	echo $name; // Displaying the variable value
?>
Browser Output
John Doe

Naming Conventions of a Variable

To Declare or Create a PHP Variable you have to follow some Rules –

  1. All Variables Must start with a $ sign.
  2. All Variables names must start with a letter or the _ underscore character.
  3. A variable name cannot start with a number.
  4. A variable name can only contain alpha-numeric characters and underscores (A to z, 0 to 9 and _ )

📝 In PHP, variable names are case-sensitive. In the following example, both are different variables –

<?php
 	$searchengine = "Google";
	$searchEngine = "Yahoo";
?>