Not fully understanding...
Posted: Wed Feb 05, 2014 1:44 pm
First post - I'm an absolute green noob to php, so I'm still trying to get a grasp on the whole concept.
I'm currently reading/doing the exercises in "PHP Solutions - Dynamic Web Design Made Easy" and came across some code that I'm lacking confidence in understanding. At the spot I'm currently at, the book says "If studying PHP code makes your brain hurt, you don't need to worry about how this works." which sucks because I want to know how it works heh. I've done some reading in the php manual to try and get a clue about a bit of it and it has helped, but I'm hoping for further clarification. I'm long winded, so apologies in advance.
First, a prelude - the lesson is working on a contact form that currently has 3 fields: name, email & comments. This is the original code:
Which I'm now instructed to update the $_POST processing code to where it checks for required fields and automates the naming of the variables at the same time. Here is where I get lost somewhat.
This is how I understand it so far. Pardon my poor terminology.
Step 1: foreach will grab all the $_POST associative array data and "form" a list of each key and value.
Step 2: $temp checks if $value is an array in the first argument, the second argument keeps/maintains it (after the "?"), then will trim() if not an array. (which seems like an odd step since it's my understanding that the result of $_POST is an array). Data now stored under the $temp variable.
Step 3: A check is made to $temp for empty data as well as a comparison between that and if the $key value within the array within $temp is listed in the $required variable (array). If so...
Step 4: The $key is added to the bottom of the $missing variable (array). If not,
Step 5: A check is made to see if the $key value is in the $expected variable (array). The comment obviously states the result, but I don't understand why there are {} around the $key variable.
Hopefully I'm not out in left field & thank you in advance for responses.
I'm currently reading/doing the exercises in "PHP Solutions - Dynamic Web Design Made Easy" and came across some code that I'm lacking confidence in understanding. At the spot I'm currently at, the book says "If studying PHP code makes your brain hurt, you don't need to worry about how this works." which sucks because I want to know how it works heh. I've done some reading in the php manual to try and get a clue about a bit of it and it has helped, but I'm hoping for further clarification. I'm long winded, so apologies in advance.
First, a prelude - the lesson is working on a contact form that currently has 3 fields: name, email & comments. This is the original code:
Code: Select all
//list expected fields
$expected = array('name', 'email', 'comments');
//set required fields
$required = array('name', 'comments');
//create empty array for any missing fields
$missing= array();
//process the $_POST variables
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];Which I'm now instructed to update the $_POST processing code to where it checks for required fields and automates the naming of the variables at the same time. Here is where I get lost somewhat.
Code: Select all
//process the $_POST variables
foreach ($_POST as $key => $value) {
//assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
//if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
//otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}Step 1: foreach will grab all the $_POST associative array data and "form" a list of each key and value.
Step 2: $temp checks if $value is an array in the first argument, the second argument keeps/maintains it (after the "?"), then will trim() if not an array. (which seems like an odd step since it's my understanding that the result of $_POST is an array). Data now stored under the $temp variable.
Step 3: A check is made to $temp for empty data as well as a comparison between that and if the $key value within the array within $temp is listed in the $required variable (array). If so...
Step 4: The $key is added to the bottom of the $missing variable (array). If not,
Step 5: A check is made to see if the $key value is in the $expected variable (array). The comment obviously states the result, but I don't understand why there are {} around the $key variable.
Hopefully I'm not out in left field & thank you in advance for responses.