PHP foreach loop =>

时间:2014-04-21 22:20:35

标签: php

所以我一直在研究PHP来创建一个基本的电子邮件表单,我遇到了一些代码,我决定看看并试验一下。

我正在使用的HTML表单是:

<form method="post" action="newsjoin.php"> 

<table align=center> 
<tr>
  <td><select name="sendto" hidden="hidden"> <option value="newsletter@myDomain.com" selected="selected">Newsletter</option> </select></td>
</tr> 
<tr>
  <td><font color=red>*</font> Name:</td>
  <td><input size=25 name="Name"></td>
</tr> 
<tr>
  <td><font color=red>*</font> Email:</td>
  <td><input size=25 name="Email"></td>
</tr>
<tr>
  <td><input type="radio" name="list" value="No" hidden="hidden" /> <input type="radio" name="list" value="Yes" hidden="hidden" checked="checked" /></td>
</tr>
<tr>
  <td colspan=2 align=left><input type=submit name="send" value="Submit"></td>
</tr> 
<tr>
  <td><br /></td>
</tr>
<tr>
  <td colspan=2 align=left><small>A <font color=red>*</font> indicates a field is required</small></td>
</tr> 
</table> 

</form> 

我的newsjoin.php文件包含:

<?php 
$to = $_REQUEST['sendto'] ; 
$from = $_REQUEST['Email'] ; 
$name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$subject = "Web Contact Data"; 

$fields = array(); 
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"list"} = "Mailing List";

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

$headers2 = "From: newsletter@myDomain.com"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for subscribing to our mailing list. You should recieve another email shortly confirming our reciept of this information.";

if($from == '') {
    print "You have not entered an email, please go back and try again.";
} 
else { 
    if($name == '') {
        print "You have not entered a name, please go back and try again.";
    } 
    else { 
        $send = mail($to, $subject, $body, $headers);       // this is the mail to site staff
        $send2 = mail($from, $subject2, $autoreply, $headers2);     // this is the mail to the user
        if($send) {
            header( "Location: http://www.myDomain.com/newsletter_joined.html" );
        } 
        else {
            print "We encountered an error sending your mail, please notify admin@myDomain.com"; 
        } 
    }
}
?> 

现在,这一切对我来说都没问题,除非我不理解这一行:

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

即,我不理解$a => $b部分,也不理解sprint()函数中这些变量(如果它们是变量)的使用。

我花了4个多小时在Google上试图了解我可以用这种方式使用php并且没有运气。

谢谢!

1 个答案:

答案 0 :(得分:3)

这是一个键值对枚举。基本上,它在集合$fields上进行迭代,并且在每次迭代时,它将变量$a绑定到键,将变量$b绑定到值。

  foreach($fields as $a => $b)
  {
      // iterates over all key-value pairs in the collection $fields
      //   at each iteration (for each key-value pair in the collection)
      //   $a is bound to the key
      //   $b is bound to the value
  }

如果你有一个像这样的关联数组:

  $collection = array(1 => 'one', 2 => 'two', 3 => 'three');

然后打印以下循环:1: one; 2: two; 3: three;

  foreach($collection as $key => $value)
  {
      echo $key.': '.$value.'; ';
  }

  

我也不理解sprint()函数中这些变量的使用(如果它们是变量)。

对于问题的第二部分,sprintf函数基本上根据格式模式和给定的变量生成格式化字符串。所以:

sprintf("%20s: %s\n", $b, $_REQUEST[$a]);
           ^           ^     ^
           |           |     +--- second variable parameter
           |           |                                |
           |           +---- first variable parameter   |
           |                                       |    |
           |                                       V    V
           +----------- string format            "%20s: %s\n"

返回一个输出:

的格式化字符串
  • 右对齐,空格填充,固定宽度(20个字符)第一个参数的字符串表示形式(变量$b,如上所述,是键值枚举中的值),

  • 后跟冒号,

  • 后跟一个空格,然后

  • 后跟第二个参数($_REQUEST[$a]的字符串表示形式,它是$_REQUEST数组中的值,由变量$a的值索引,再次,如上所述,绑定到键值对枚举中的键