I have following code snippet but i dont understand what its do, but i think there is easy way to write this code again with a simple foreach like below, is that correct ?
$paypal_message="";
while( @list($key,$value) = @each($paypal_post_vars)) {
$paypal_message .= $key.":\t".$value."
";
if($key!='custom'){
$insert_sql.=" `".$key."`='".$value."' , ";
}
}
I think rewrite it like this ? is it exactly the same ?
foreach($paypal_post_vars as $key=>$value ){
$paypal_message .= $key.":\t".$value."
";
if($key!='custom'){
$insert_sql.=" `".$key."`='".$value."' , ";
}
}
Yes, this is functionally identical code, if you look at php site - control structures foreach have the explanation below
You may have noticed that the following are functionally identical:
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
// ^^ supressing key
echo "Value: $value<br />
";
}
foreach ($arr as $value) {
echo "Value: $value<br />
";
}
The following are also functionally identical:
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />
";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />
";
}