Variables are essential to writing code in PHP.
Variables always start with the dollar sign followed by the variable name:
$today = “Friday, April 11 2025”; // this is a string variable
$price = 023.04; // this is a float (floating point number)
$quantity = 12; // this is an integer
$taxable = true; // this is a boolean
Rules for PHP variables:
1. A variable starts with the $ sign, followed by the name of the variable
2. A variable name 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
5. Variable names are case-sensitive ($age and $AGE are two different variables)
Arrays: an Array can be defined like this:
$myarray=array(‘Value1′,’Value2′,’Value3’); // $myarray contains 3 strings
Adding to an array is done like this:
$myarray[]=”John Jones”; // $myarray contains 4 strings
$myarray[]=”Mary Smith”; // $myarray contains 5 strings
Now $myarray contains 5 entries.
They can also be defined like this:
$myarray=array(); // produces an empty array
The PHP superglobal variables are:
1 $GLOBALS
2 $_SERVER
3 $_REQUEST
4 $_POST
5 $_GET
6 $_FILES
7 $_ENV
8 $_COOKIE
9 $_SESSION
Leave a Reply
You must be logged in to post a comment.