Learn PHP Variables

Just like in math or other programming languages, PHP variables are arbitrary placeholders for information that can be used and modified throughout your code. Unlike math, PHP variables can contain any type of data from numbers to strings of text and array’s. PHP variables are unique from other programming languages because they do not need to be declared prior to using them. PHP will know how to handle the variable based on its content.

Variable Naming Conventions

A dollar sign must always precede each variable name and the first character should always be either an underscore or a letter. Throughout the variable name, only alpha-numeric characters and underscores may be used. No hyphens, symbols or extended ASCII characters can be used in the variable name.

Example of PHP Variable Names

<!--Good PHP variable names-->         


<!--Bad PHP variable names-->      

MyVar=""; //note no "$" preceding the variable name

$1_my_var=""; //PHP variables cannot start with a number


Example of PHP Variable Data Types

$my_number=42; //note: no need to use quotes around number

$my_string="Hello Reader!"; //note: quotes around string.

//Array - Array's will be discussed in-depth later.
$my_array[0]="Array Item 0";
$my_array[1]="Array Item 1";
$my_array[2]="Array Item 2";


Troubleshooting Tip

Always’s double check your variable naming conventions and ensure that you have placed your strings within quotes.

Speak Your Mind