PDA

View Full Version : Php


M12_GTO
05-03-2005, 11:50 PM
Hi,

I've got this code from a lecturer that demonstrates sessinos but I can't understand one line of it. I've looked on the Net but to no avail - this guy certainly does things differently!

<?php
session_start();
session_register();
?>
<HTML>
<HEAD></HEAD>
<BODY>
<?PHP
extract($_POST);
$ages[$name] = $age; //LINE I DON'T UNDERSTAND!!!
if(!isset($ages))
$ages = array();

foreach($ages as $name=>$age) {
print("<P>Name $name Age $age</P>");
}
?>
<FORM ACTION="dave.php" METHOD="get">
<P>Name:<INPUT TYPE="text" NAME="name"/></P>
<P>Age:<INPUT TYPE="text" NAME="age"/></P>
<INPUT TYPE="submit" VALUE="Add to array"/>
</FORM>
</BODY>
</HTML>

On the commented line I can't understand why it says $ages[$name] Where does this [$name] come from? Is this an array within the session?

I'll be very grateful if anyone can shed some light.

Mist
06-03-2005, 10:05 AM
$ages[$name] = $age; //LINE I DON'T UNDERSTAND!!!

$ages is an array. $name identifies the element of the array, $age is the age from the POST submitted data. This assigns ages to names.

$name and $age are empty until you submit the form.

M12_GTO
06-03-2005, 10:30 AM
OK, it makes more sense now: :wave:

$age[$name] is just a regular array (nothing to do with sessions) :thumb:
The session variables are sent to pages in the POST data :thumb:
Session variables are retrieved from POST data and stored in the regular array :thumb:

Just a thought but shouldn't the lecturer have registered $ages in session_register()? :confused:

Also if the $age is populated from the POST data then why send use method="GET" and append data to the querystring? :confused:

Mist
06-03-2005, 10:34 AM
Hmm, those questions I am not so sure of, my knowledge of PHP is a bit limited.

Personally I'm not sure of the need for the session in the example, and what it achieves. I think that the posted/geted data can be retrieved in the same method regardless.

Could just experiment I guess to see what changing various things does.