Objective of this article: After going through this page, one would learn what are variables in PHP, how to define and use them. Examples of correct and incorrect PHP variable names and different types and categories of variables.
What is a variable? How to define a variable in PHP?
Variables in PHP or for that matter, in any other scripting language are basically memory location names which value can be changed (updated) during the execution of the program.
PHP Variable Declaration: In PHP, variables need not be declared separately before using them. You can introduce a new variable anytime you wish to use them. However, it is recommended to declare your variables in chunk before using them, to avoid using similar variable names at some other place in the PHP script.
PHP Data Types: PHP supports all basic data types of variables. e.g. String, Integer, Date etc. However, PHP does not require you to declare the data type of a variable unlike several other languages do. In PHP, any variable can directly be used without any need to define their data type beforehand. The data type of the variable becomes the same as the data type of the value being passed to it for the first time. So if a value of 5 is moved to a variable while declaring it, the variable will become integer.
The variable of data type String is created when some string value is passed to it. The simplest way to create a string variable is like this, $string = ""; Once introduced, this variable can be used at any subsequent PHP statement.
Rules to name PHP Variables
Variable names in PHP are case sensitive. So $category and $Category are different. Once a variable has been declared (i.e. used), it can be used anywhere within the program. Following rules should be kept in mind to make PHP variable names which are valid as well easy to remember.
- All variables in PHP start with a dollar ("$") sign.
- A variable name must start with a letter or an underscore "_".
- White space is not allowed in the name of a PHP variable so if we want to use multiple words while defining a PHP variable, it would be incorrect to write item category. To make easily readable multiple words variable names, we can use an underscore sign in between words(e.g. item_category). Another less frequently used alternative is to Capitalize the first letter of second word(e.g. ItemCategory). Though we can use all small or all capital letters also.
- I have never had to think about the maximum length of the name of a PHP variable. So frankly speaking, I do not know this.:)
- Only alphanumeric characters and underscores are allowed in PHP variable’s name. No special characters allowed (except the lone dollar sign to begin the variable declaration)
Examples of PHP variable names
Valid names of PHP variables
$categroy
$Category
$categorymaster
$categoryMaster
$category_master
$category_1
Invalid names of PHP variables
Category (Variable name does not begin with $)
$Category! (Only alpha numeric characters and _ allowed)
$category master (There should not be any space in between the two words)
Scope of PHP Variables:
By scope of a variable, we mean if we change the value of the variable at the given point, where else that new value would be reflected in the program. In other words, scope of a variable is the
context in which it's defined.
By this definition, PHP variables can be categorized as under. To understand the scope of variables, it's recommended that one goes thru all three types of variables in one go.
- Global PHP Variables: Any variable defined in the main line context has a global scope. This means that same variable is available through out the script. Global variables can be defined in PHP include files as well.
Example of PHP Global Variables:-
<?php
$test = '5';
global $x, $y, $z;
?>
- Local PHP Variables: Any variable which defined within a function, has a scope within that function itself.
- Super Global PHP Variables:
- PHP static Variables: