Variables are containers that contain information. You give a variable a name, then assign data to it. There are six types of data that you can assign to a variable
| Data type | Description | Example |
|---|---|---|
| Integer | A whole number | 25 |
| Float (also called Double) | A floating point number | 3.14592 |
| String | A collection of characters | "this is a string" |
| Boolean | TRUE or FALSE | FALSE |
| Array | A complex variable. A list of variables. | "lions", "tigers", "bears" |
| Resources (also called Handles) | A variable representing somethng that is being manipulated | Image resources
Color Resource File Handle Database Handle |
| Object | A variable representing acollection of variables and functions |
Variable names start with a $ and are followed by a combination of letters, numbers and underscores. Although you can use numbers in the name, the name may not start with a number. The first character cannot be a number and there are special variable names that start with an underscore. Rather than remembering the special variable names, you can avoid problems by just not starting the name with an underscore. Variable names are case sensitive so $x is not the same as $X.
| Valid | Not valid |
|---|---|
| $x | $1 |
| $name_1 | $1_name |
You assign a value to a variable with the Assignment Operator =. This is not to be confused with == which is a Comparison Operator.
<?
$x = 2; # assigns the number 2 to $x
$y = 3; # assigns the number 3 to $y
$z = $x + $y; # assigns the sum of $x and $y to $z
echo $z; # outputs 5
?>
If a variable is a integer or float, it is just like a variable in algebra and you can perform math operators and functions on them. If the variable is a string or other type of variable and you perform a math operators on it, you either will get a result that you didn't expect or an error message. When assigning an integer or float, you do not use quotes:
<?
$x = 2; # assigns the number 2 to $x
$y = 3.1415; # assigns the number 3.1415 to $y
$z = $x + $y; # assigns the sum of $x and $y to $z
echo $z; # outputs 5.1415
?>
When a variable is a string it contains a string of text. When assigning a string to a variable, enclose the string in quotes. You can perform string functions on a variable that is a string. If you perform any other type of function, you will get unexpected results or an error message
<?php
$x = "hello world"; # assigns "hello world" to $x
$y = ucwords($x);
echo "$y"; # outputs "Hello World"
?>
Boolian data types can only have one of two values: TRUE or FALSE. Boolean values are mainly used to evaluate conditions in conditional statements. PHP considers the following values to be FALSE:
All other values are considered TRUE
When an array is a number or string it is like a box that only contains one thing at a time. For example, a variable $x can either be 38 or 42 but not both at the same time. An array is like a file cabnet full of files with each file having a different name and possibly having different values. Arrays will be covered in more detail later but for now here is one short example:
<center><table border="1" cellpadding="5">
<tr><th colspan="2">Prices</th></tr>
<?
# make an associative array
$prices = array( "Thingies"=>12, "Widgets"=>48, "Whatnots"=>32 );
# sort the array by key
ksort($prices);
# loop through array, display key and value
foreach( $prices as $key=>$val )
{ echo "<tr><td>$key</td><td>\$$val</td></tr>"; }
?>
</table></center>
| Prices | |
|---|---|
| Thingies | $12 |
| Whatnots | $32 |
| Widgets | $48 |
Variables, as the name implies, can change:
<?
$x = 1;
echo $x; # outputs 1
$x = $x + 1;
echo $x; # outputs 2
?>
Sometimes, you want a constant; something that will stay the same through out your script. You can create a constant with the define() function.
<?
define("PAGE_TITLE", "DJ Mike's Tutorials: PHP");
echo PAGE_TITLE;
# outputs DJ Mike's Tutorials: PHP
?>
When using constants, you don't begin them with a $ like variables. By convention, constant names are uppercase so they are easily recognized as constants but they dont have to be. Constant names cannot be given names that are keywords; words that have meaning for PHP. Some words to avoid:
| and | as | break | class | const |
| continue | declare | default | die | do |
| echo | else | empty | eval | for |
| exit | for | foreach | function | global |
| if | include | list | new | or |
| require | return | switch | use | |
| var | while |
In addition, there are predefined math constants to avoid when you name your constants.
|
|
|