Variable Types in PHP

Rumman Ansari   Software Engineer   2020-02-04   5557 Share
☰ Table of Contents

Table of Content:


Declaring PHP variables

All variables in PHP start with a $ (dollar) sign followed by the name of the variable.

A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.

If a variable name is more than one word, it can be separated with an underscore (for example $employee_code instead of $employeecode).

'$' is a special variable that can not be assigned.

Example : Valid and invalid PHP variables  



<?php
$abc = 'Welcome';  //valid
$Abc = 'W3resource.com'; //valid
$9xyz = 'Hello world';  //invalid; starts with a number
$_xyz = 'Hello world';  //valid; starts with an underscore
$_9xyz = 'Hello world';  //valid
$aäa = 'Hello world';  //valid; 'ä' is (Extended) ASCII 228.
?>


PHP variable name is case-sensitive

Consider the following example :



<?php
$abc = 'Welcome';
echo "Value of abc : $abc";
echo "Value of ABC : $ABC";
?>


Output:

If you will rum the above code, you will get the following output.



Value of abc : Welcome
Value of ABC :


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 ?

  • Local variables

  • Function parameters

  • Global variables

  • Static variables