Home     Wordpress     Codex

Learning PHP (3); Reserved vars and loops

May 28th, 2009 by admin | Filed under Learning PHP.

In my previous article I explained arrays and their use. But have you ever wondered what happens to those variables set in the url? They are contained within an array which is prefilled for your scripts.

The array variables $_GET, $_POST and $_SERVER are some of the interesting ones you can use.

The reserved $_GET variable
An example would be this type of URL.
http://www.example.domain/myphpscript.php?name=Espen

To catch the variable ‘name’ you can do this piece of PHP

<?php
echo “Hi ” . $_GET['name'];
?>

This will output “Hi Espen”.

Using the reserved $_POST variable
To get the variables from a form you can use $_POST['key'].
Look at this example:

Create this HTML form, change the path to myhelloscript.php to a place where you test your scripts.

<form action=”http://my.domain/myhelloscript.php” method=”post”>
<input type=”text” name=”name”>
<input type=”submit” value=”send it”>
</form>

Then create the file myhelloscript.php with the following contents

<?php
echo “hi ” . $_POST['name'];
?>

You see? The same result here, only now we’re using POST instead of GET (you can also notice that the URL does not contain the variables).

Looping over an array
Looping over an array can be very usefull in the case where you don’t know what the reference keys are.
Let us say you didn’t know that the key for the name in the previous post example was not called  ‘name’, how would you find it?

Here is an example of looping over an array, looping through all the key and value pairs.

<?php
$myarray = array(‘Espen’, ‘John’, ‘Lisa’, ‘Mark’);

foreach ($myarray as $value) {
echo $value . “<br>”;
}
?>

Now we’re saying for each ‘entry’ in $myarray get the entry as $value and do something with it between the { and }
So for each $myarray as $value: echo $value and a ‘<br>’ (to get a newline).

This example will output

Espen
John
Lisa
Mark

Getting the key
But that’s not the key, is it?  How do you find the keys?
This example will use the foreach command in another way to get the keys also

<?php
foreach ($myarray as $key => $value) {
echo $key . ‘ is ‘ . $value . ‘<br>’;
}
?>

Now you can also use the variable $key within the loop for each value to get the corresponding key.

This is an example of looping over the $_GET array

<?php
foreach ($_GET as $key => $value) {
echo $key . ‘ is set to ‘ . $value . ‘<br>’;
}
?>

A Challenge
The $_SERVER variable contains a lot of variables about the request, can you figure out how to show them all?

If you get any weird syntax errors or such, post them as a comment and I will try to help you out!

Tags: , , , , ,

One Response to “Learning PHP (3); Reserved vars and loops”

  1. Learning PHP Part Four - If and else | 30/05/09

    [...] Learning PHP Part Four – If and else May 30th, 2009 by admin | Filed under Learning PHP. Have you been playing around with the GET and POST arrays now? If not, learn about foreach in PHP here. [...]

Share Your Thoughts