代码之家  ›  专栏  ›  技术社区  ›  Dennis G

整个HTML页面作为变量-有更好的方法吗?

  •  0
  • Dennis G  · 技术社区  · 14 年前

    我有一个大表单,在表单的末尾,用户会得到一个摘要:

    You have entered:
    <table>
        <tr>
            <td>First name</td>
            <td><?php echo $firstname ?></td>
        </tr>
        <tr>
            <td>Last name</td>
            <td><?php echo $lastname ?></td>
        </tr>
    </table>
    

    我首先设计了摘要页面,现在我想到将此页面作为确认电子邮件发送给用户是件好事。我现在做的是:

    <?php $summarypage = "<table>
            <tr>
                <td>First name</td>
                <td>".$firstname."</td>
            </tr>
            <tr>
                <td>Last name</td>
                <td>".$lastname."</td>
            </tr>
        </table>";
    echo $summarypage; ?>
    

    对于我使用的循环 $summarypage .= "blabla"; 在循环中。

    当发送电子邮件时,我可以 $summarypage 并将其附加到我的电子邮件正文。美丽的。

    我做的还好吗?我觉得这很“不优雅”。
    不是 $摘要页 当我在电子邮件中再次调用它时,它将被重新呈现,这意味着与它连接的所有变量(例如 $firstname )又会被称为“表演猪”?

    有什么“缓冲区”我可以写 $摘要页 变量为,所以后面有一个纯文本变量?会 $newsummarypage = string($summarypage) 耍花招?

    3 回复  |  直到 14 年前
        1
  •  4
  •   Pekka    14 年前

    HEREDOC

    <?php $summarypage = <<<EOT
    <table>
     <tr>
      <td>First name</td>
      <td>$firstname</td>
      </tr>
      <tr>
       <td>Last name</td>
       <td>$lastname</td>
      </tr>
    </table>
    EOT;
    ?>
    
        2
  •  2
  •   Ishtar    14 年前

    $s = "hello"; //stores the sequence 'h' 'e' 'l' 'l' 'o' in $s
    $s = $s." world";//take the sequence stored in $s add the sequence ' world', 
                     //and store in $s again
    echo $s; // prints 'hello world'. That is what $s contains, what $s is
    
    $summary = "<div>".$firstname."</div>"; // take '<div>', lookup what $firstname
                     //contains and add that, then add '</div>' and then store this 
                     //new string thing('<div>Ishtar</div>') in $summary.
    
    echo $summary; //$summary here knows nothing about $firstname, 
                   //does not depend on it
    

        3
  •  1
  •   Surreal Dreams    14 年前