php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

PHP Fundamentals

Variables and Arrays

Variables


-All variables start with a $
-There are four major types of variables: integers, floating point numbers, strings and arrays. Assigning values to variables is easy. The single and double quotes work the same as the echo statement above.

Integer: $integer = 123;
String: $string = "string $integer";
Floating Point: $float = 16.234

Arrays

Simple Arrays

Simply arrays store values in a numerical index.

<? 
$array=array("Monday","Tuesday","Wendsday","Thursday",
"Friday","Saturday","Sunday");
For ($a=0; $a > count($array); $a++) {
            echo "$array[$a]";
}
 ?>

The array function inserts the values passed to it into $array. We'll discuss the for loop later on but what happens is if $a is less than the number of values in the array $array the value of $array with an index of $a is displayed. Every time the loop is run the value of $a is incremented.

Associative Arrays


Associative arrays store value data in keys

<? 
$assoc=array("name" => "Jon Smith",
            "phone" => "555-555-1234",
            "address" => "1010 Home Cir",
            "city" => "Atlanta",
            "state" => "GA",
            "zipcode" => "30060");
$assoc["email"]="jon@smith.com";
foreach ($assoc as $key => $value) {
            echo "$key : $value - ";
}
 ?>
Outputs name : Jon Smith - phone : 555-555-1234 - address : 1010 Home Cir - city : Atlanta - state : GA - zipcode : 30060 - email : jon@smith.com

We'll get into the foreach loop a little later but for every value of $assoc it set the key equal to $key and the value to $value and then used the echo statement to display the values

Multidimensional arrays


<? 
$info = array("Jon" => array("name" => "Jon Smith","address" => "2 Little St","city" => "Atlanta","state" => "GA",
"zipcode" => "30060"),
"Bob" => array("name" => "Bob Jones","address" => "725 Big Cir","city" => "Miami","state" => "FL",
"zipcode" => "33027"),
"Mark" => array("name" => "Mark Klien","address" => "1315 Riverland Trace","city" => "Boston","state" => "MA",
"zipcode" => "11083");
 ?>

This stores an array inside an array. To retrieve a value use echo $info["Jon"]["city"] will display Atlanta.

Variables returned from browser

Getting variables from forms


HTML has two methods for passing form variables, POST and GET. GET displays the input in the URL while POST hides it. Variables passed via POST are represented by $_POST["{name of input tag}"] GET uses $_GET[] respectively. Also $_REQUEST[] will take the values of either POST or GET. $_REQUEST also will return variables passed in a URL not from a form.

To learn about form check out the Higherpass Forms Tutorial

To pass an array from a form set the NAME attribute of the input statement with an ending [] to designate it as an array. The example below demonstrates how to do this with the <SELECT> tag.

<? 
if (isset($_POST["Submit"])) {
  if (is_array($_POST["names"])) {
     $a=0;
     while (list($key,$value) = each($_POST["names"])) {
        echo "$key - $value<BR>";
     }
  }
}

echo '<FORM METHOD="POST" ACTION="test12.php">';
echo '<SELECT NAME="names[]" SIZE="4" MULTIPLE>';
echo '<OPTION VALUE="4">four';
echo '<OPTION VALUE="3">three';
echo '<OPTION VALUE="2">two';
echo '<OPTION VALUE="1">one';
echo '<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="SUBMIT">';
 ?>


Built in variables


PHP has a multitude of stored and enviromental variables, they are called superglobals. A complete list is available by calling the phpinfo() function from a page. This tutorial is going to assume you have register globals turned off which is more secure. These variables are stored in the $_SERVER["{key}"] array. Below is a list of common server variables.

REQUEST_URI - Like PHP_SELF except it also holds the any variables being passed through the URL
HTTP_REFERER - The URL of the page the visitor clicked through from to reach the current page is stored here
REMOTE_ADDR - The IP address of the visitor calling the page
REMOTE_HOST - The hostname of the visitor calling the page
PHP Basic Syntax <<  1 2 3 4 5 6  >> Math Functions and Flow Control
New Content