PHP variables are declared using using dollar sign $. For example, you can declare empty string variable this way:
$varaibleName=""; or variable with text this way; $welcome="Hello Visitor. Welcome to my website.";.
Watch this program to see how variables are used:
<?php
$name="John Doe";
$email="johnd@pctalknet.com";
$age=356;
print"Your Name: $name <br>";
print"Your Email: $email<br>";
print"Your age: $age";
?>
The following is the result from the above example:
Your Name: John Doe Your Email: johnd@pctalknet.com Your age: 356
PHP Arrays
An array is an indexed list of things called elements or group of related variables.
For example, say that we want declare variables for list of cars like this;
$car1=""; $car2=""; $car3=""; ...
Here is how you would declare list of cars using array:
Dim cars(3); We simply declared array that takes
4 items. To assign values to this array, do these:
$cars(0)="Jeep Grand Cherokee"
$cars(1)="Jeep Wrangler"
$cars(2)="Jeep Liberty"
$cars(3)="Jeep Cherokee Briarwood"
Use this statement to write an item in the array:
echo"$cars(2)"; This will write the
3th car on the list.
Bellow is the example we did in code
This is simply illustration, NOT good example.
If you writing items from an array, you would probably write
all the items at one time using loops instead of listing echo statements.
Result
Jeep Grand Cherokee, Jeep Wrangler, Jeep Liberty, Jeep Cherokee Briarwood,
To write the array using for loop statement, do this.
The variable x which starts from 0 to 3 acts as an index for the
array.
Result
Jeep Grand Cherokee Jeep Wrangler Jeep Liberty Jeep Cherokee Briarwood
Two dimensional Arrays
You can define two or more dimensional arrays by puting numbers withing the parenthesis.
Here is how you would define two dimensional array $person["John D","123"].
The reason you may use this type of array is when you have records of each item. For example,
car, year, price,... and you want display one record at a time.
The following is an example of two dimensional array:
This array has three cars and each car has two fields or columns.
It's like a table, row 0 has record of one car, row 1 has record of another car and so on
We also define nested for loops. First one reads the row and second one reads the column.
Result
Jeep Grand Cherokee 2002 Jeep Wrangler 2002 Jeep Liberty 2003 Jeep Cherokee Briarwood 2003