【问题标题】:PHP foreach loop => [duplicate]PHP foreach循环=> [重复]
【发布时间】:2014-04-21 22:20:35
【问题描述】:

所以我一直在研究 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 =&gt; $b 部分,也不了解这些变量(如果它们是变量)在sprint() 函数中的使用。

在过去的 4 个小时里,我一直在 Google 上尝试了解我可以如何了解如何以这种方式使用 php,但没有运气。

谢谢!

【问题讨论】:

  • 花了这四个小时,请您概述一下您的问题在哪里可以理解?是什么让你无法理解?

标签: php


【解决方案1】:

这是一个键值对枚举。基本上,它在集合 $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"

返回一个输出a的格式化字符串:

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

  • 后跟冒号,

  • 后跟一个空格,然后

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

【讨论】:

  • 嗯,带有 PHP 手册链接的评论可能会在更多语言和更多用户 cmets 中提供更好的信息,但是,这是一个基本主题,值得给它一个个人感觉,为什么不呢。好吧,不是。
  • 我实际上认为这个问题与循环和 => 运算符的关系不如使用 sprintf 并在单行上循环以输出所有 $_REQUEST 值。不幸的是,在人们阅读并解析它之前,高级代表进来并责备这个家伙,每个人都在赶上潮流。 SO的根本问题。
  • @Hakre 与您提供的副本相比,在这种情况下使用 sprintf 是一个更好的答案。你和其他人在你完全理解之前投票结束这样的好问题充其量是可怜的。
  • @Matt - 感谢您的支持。有时,很容易忘记新手程序员除了使用参考文档之外还需要一些帮助。我知道我自己很快就解决了一些问题。
  • 问题清楚地表明缺乏努力,至少找出问题所在,声称一个人花了 4 个小时仍未找到答案,这是极不可能的,是一种 fu i> 致所有为 PHP 手册做出贡献的人。手册很清楚,如果人们看不懂,那就是另一个问题。无论如何为@MikyDinescu +1。
猜你喜欢
  • 2015-09-21
  • 2023-03-17
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多