Not fully understanding...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
i spel gud
Forum Newbie
Posts: 7
Joined: Mon Jan 27, 2014 12:11 am
Location: Anderson, CA

Not fully understanding...

Post by i spel gud »

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:

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;
    }
}
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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Not fully understanding...

Post by Christopher »

i spel gud wrote:Step 1: foreach will grab all the $_POST associative array data and "form" a list of each key and value..
I think you are saying the right thing, but to clarify: the foreach will loop through the array and in each iteration it will set the $key and $value variables to those of the next element in the array. So the $key and $value variables will change each loop.
i spel gud wrote: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..
Yes $_POST is an array, but since we are looping through $_POST then $value would be set to a specific element in the $_POST array, such as $_POST['name']. The values of form elements passed to PHP can be arrays.
i spel gud wrote: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....
Yes
i spel gud wrote:Step 4: The $key is added to the bottom of the $missing variable (array). If not, .
Yes
i spel gud wrote: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..
Well, this is not very good coding practice It is actually creating a variable with the name of the key. So if the value in $key is "name" then the variable $name would be created. A better practice would be to assign these verified values to an array, so I would rather see:

Code: Select all

//process the $_POST variables
$data = array();     // initialize the array of form fields found
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)) {
    $data[$key] = $temp;
  }
}
(#10850)
User avatar
i spel gud
Forum Newbie
Posts: 7
Joined: Mon Jan 27, 2014 12:11 am
Location: Anderson, CA

Re: Not fully understanding...

Post by i spel gud »

Christopher wrote:I think you are saying the right thing, but to clarify: the foreach will loop through the array and in each iteration it will set the $key and $value variables to those of the next element in the array. So the $key and $value variables will change each loop.
Sweet & thank you for adding clarification to my poorly worded statement. Got it.
Christopher wrote:Yes $_POST is an array, but since we are looping through $_POST then $value would be set to a specific element in the $_POST array, such as $_POST['name']. The values of form elements passed to PHP can be arrays.
I think I'm starting to follow. I read something security related as to why a check is needed to ensure the form element isn't an array, or something like that. I don't see anything that stands out security-wise in that regard since the initial check of ($value) ? $value maintains the array (if it were one). I guess that's later on in the chapter or I'm completely off heh. Anyway, I looked at the is_array on the php manual & am still a little confused. I think I just lack the understanding of the output.

Code: Select all

$temp = is_array($value) ? $value : trim($value);
In this case, it's dealing with just the value. So is_array looks at the $value (what is typed in the form elements). I'm guessing that it would look something like:

$value[0] = Christopher

or would it be different?
Christopher wrote:Yes
Christopher wrote:Yes
Awesome...whew.
Christopher wrote:Well, this is not very good coding practice It is actually creating a variable with the name of the key. So if the value in $key is "name" then the variable $name would be created....
Oh, I see it now. I misread the comment, but yeah I follow what you're saying.
Christopher wrote:A better practice would be to assign these verified values to an array, so I would rather see:

Code: Select all

//process the $_POST variables
$data = array();     // initialize the array of form fields found
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)) {
    $data[$key] = $temp;
  }
}
So I follow, what would be the result of the change to $data[$key] = $temp;? Sorry for the noobness, I'm just not familiar with that yet & I couldn't find anything in the manual via search.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Not fully understanding...

Post by Christopher »

i spel gud wrote:In this case, it's dealing with just the value. So is_array looks at the $value (what is typed in the form elements). I'm guessing that it would look something like:

$value[0] = Christopher

or would it be different?
That's correct (except there would be quotes around 'Christopher'). In practice, if the code encounters a POST/GET value that is an array it should know beforehand that it is supposed to be an array and loop through that array to check that each of those values is safe as well.
i spel gud wrote:So I follow, what would be the result of the change to $data[$key] = $temp;? Sorry for the noobness, I'm just not familiar with that yet & I couldn't find anything in the manual via search.
What they are using are called variable variables and I would recommend not using them unless your really know what you are doing and using them actually the best solution. The problem is that they create variables that do not appear in your code, so they make the code obscure and therefore difficult to maintain. You want the code to be as explicit as possible. A group of related variables should be in an array or be the properties of a class. Plus, both of those can be passed around as a group -- unlike the scattered, invisible variables being created in the code you posted.
(#10850)
User avatar
i spel gud
Forum Newbie
Posts: 7
Joined: Mon Jan 27, 2014 12:11 am
Location: Anderson, CA

Re: Not fully understanding...

Post by i spel gud »

Christopher wrote:That's correct (except there would be quotes around 'Christopher'). In practice, if the code encounters a POST/GET value that is an array it should know beforehand that it is supposed to be an array and loop through that array to check that each of those values is safe as well.
Oops. Blah, I read something regarding ' vs " and I also come across stuff that have neither. :banghead: I'll figure it out.

Thanks.
Christopher wrote:What they are using are called variable variables and I would recommend not using them unless your really know what you are doing and using them actually the best solution. The problem is that they create variables that do not appear in your code, so they make the code obscure and therefore difficult to maintain. You want the code to be as explicit as possible. A group of related variables should be in an array or be the properties of a class. Plus, both of those can be passed around as a group -- unlike the scattered, invisible variables being created in the code you posted.
Ah, got it & makes sense (being explicit). I don't get classes nor passed around as a group yet, but I'm sure at some point it'll be a lesson.

Though, what I was asking in my previous post was what the code meant with the change you made: $data[$key] = $temp; I've seen $data before, but am not sure of/don't follow its usage.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Not fully understanding...

Post by Christopher »

i spel gud wrote:Though, what I was asking in my previous post was what the code meant with the change you made: $data[$key] = $temp; I've seen $data before, but am not sure of/don't follow its usage.
$data is just a variable I created. It is initialized to an empty array before the loop and filled in within the loop. If you followed the loops and showed the values of $key and $value/$temp then it would be actually doing something like this:

Code: Select all

# first loop
$data['name'] = 'John Doe';
# second loop
$data['email'] = 'john@doe.com';
# third loop
$data['comments'] = 'My comment';
The code you posted would be doing:

Code: Select all

# first loop
$name = 'John Doe';
# second loop
$email = 'john@doe.com';
# third loop
$comments = 'My comment';
(#10850)
User avatar
i spel gud
Forum Newbie
Posts: 7
Joined: Mon Jan 27, 2014 12:11 am
Location: Anderson, CA

Re: Not fully understanding...

Post by i spel gud »

Ohh! Damned if I didn't miss the initializing of the array added. That answer took care of another question I came up with.

The fog clears a little more. Thank you very much!
Post Reply