如何清理PHP代码

如何清理PHP代码,php,Php,所以昨天我很无聊,创建了一个flash卡web应用程序。我基本上使用了一个HTML登录页,它有10个不同的文本区域,将从文本区域输入的内容推送到PHP变量中。每个文本区域都有一个名称“问题一”“问题二”,依此类推。然而,有没有一种更干净、更简单的方法来做到这一点 <?php $questionone = $_POST['questionone']; $questiontwo = $_POST['questiontwo']; $questionthree = $_POST['questio

所以昨天我很无聊,创建了一个flash卡web应用程序。我基本上使用了一个HTML登录页,它有10个不同的文本区域,将从文本区域输入的内容推送到PHP变量中。每个文本区域都有一个名称“问题一”“问题二”,依此类推。然而,有没有一种更干净、更简单的方法来做到这一点

<?php

$questionone = $_POST['questionone'];
$questiontwo = $_POST['questiontwo'];
$questionthree = $_POST['questionthree'];
$questionfour = $_POST['questionfour'];
$questionfive = $_POST['questionfive'];
$questionsix = $_POST['questionsix'];
$questionseven = $_POST['questionseven'];
$questioneight = $_POST['questioneight'];
$questionnine = $_POST['questionnine'];
$questionten = $_POST['questionten'];
$answerone = $_POST['answerone'];
$answertwo = $_POST['answertwo'];
$answerthree = $_POST['answerthree'];
$answerfour = $_POST['answerfour'];
$answerfive = $_POST['answerfive'];
$answersix = $_POST['answersix'];
$answerseven = $_POST['answerseven'];
$answereight = $_POST['answereight'];
$answernine = $_POST['answernine'];
$answerten = $_POST['answerten'];

?>
<div class="two"><p><?php echo "$questionone"; ?></p></div>
<div class="three"><p><?php echo "$questiontwo"; ?></p></div>
<div class="two"><p><?php echo "$questionthree"; ?></p></div>
<div class="three"><p><?php echo "$questionfour"; ?></p></div>
<div class="two"><p><?php echo "$questionfive"; ?></p></div>
<div class="three"><p><?php echo "$questionsix"; ?></p></div>
<div class="two"><p><?php echo "$questionseven"; ?></p></div>
<div class="three"><p><?php echo "$questioneight"; ?></p></div>
<div class="two"><p><?php echo "$questionnine"; ?></p></div>
<div class="three"><p><?php echo "$questionten"; ?></p></div>
</div>





您可以在html中使用如下数组:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>

并用php处理这些数据,如下所示:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>
$name=$\u POST['name'];
foreach($v名称){
打印$v.“
”; }
您可以在html中使用如下数组:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>

并用php处理这些数据,如下所示:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>
$name=$\u POST['name'];
foreach($v名称){
打印$v.“
”; }
虽然其他答案似乎是正确的,但大多数答案都没有使用htmlspecialchars或ISSet来防止警告。试着这样做:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>


虽然其他答案似乎是正确的,但大多数答案并没有利用htmlspecialchars或issets来防止警告。试着这样做:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>


我建议你用一个名字回答所有问题,即
question[]
而且,所有答案都有一个名称,即
answer[]

<form method='POST' action='submitPage.php'>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    .
    .

    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    .
    .
    .
</form>




. .


. . .
submitPage.php

<?
$question=$_POST['question'];
$answer=$_POST['answer'];
$TotalQuestion=sizeof($question);

for($i=0;$i<$TotalQuestion;$i++)
{
    $Question=$question[$i];
    $Answer=$answer[$i];
?>

    <div class="Question"><p><?php echo $Question;?></p></div>
    <div class="Answer"><p><?php echo $Answer;?></p></div>

<?}?>


我建议你用一个名字回答所有问题,即
问题[]
而且,所有答案都有一个名称,即
answer[]

<form method='POST' action='submitPage.php'>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    .
    .

    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    .
    .
    .
</form>




. .


. . .
submitPage.php

<?
$question=$_POST['question'];
$answer=$_POST['answer'];
$TotalQuestion=sizeof($question);

for($i=0;$i<$TotalQuestion;$i++)
{
    $Question=$question[$i];
    $Answer=$answer[$i];
?>

    <div class="Question"><p><?php echo $Question;?></p></div>
    <div class="Answer"><p><?php echo $Answer;?></p></div>

<?}?>


如果您已经知道问题是什么(只有少数问题),并且不需要依赖发布的值,那么可以使用数组

$questions = array('question1', 'question2', 'question3');
$i = 1;
foreach($questions as $question) {
  echo "Question " . htmlspecialchars($i) . ":" . " " . htmlspecialchars($question) . '<br>';
  $i++;
}

如果您已经知道问题是什么(只有几个),并且不需要依赖于发布的值,那么可以使用数组

$questions = array('question1', 'question2', 'question3');
$i = 1;
foreach($questions as $question) {
  echo "Question " . htmlspecialchars($i) . ":" . " " . htmlspecialchars($question) . '<br>';
  $i++;
}

是的,使用数组和循环。嗨,肯尼,这是一种动态的方法。可以将动态窗体与动态数组一起使用。您可以让用户在需要时添加或删除文本区域。只要在一个拥有post数据和表单库的框架内查找如何在phpWorking中使用动态表单,您的代码就会大大简化。。。考虑一下LaaVelor或CooDigiTr.你是否需要从一个$POST中获取数据?我投票把这个问题关闭作为一个话题,因为这是一个问题,不一定是这样。是的,使用数组和循环。嗨,肯尼,有一种动态的方式来做这个。可以将动态窗体与动态数组一起使用。您可以让用户在需要时添加或删除文本区域。只要在一个拥有post数据和表单库的框架内查找如何在phpWorking中使用动态表单,您的代码就会大大简化。。。考虑一下LaaVelor或CooDigiTr.你是否需要从一个$POST中获取数据?我投票把这个问题关为离题,因为这是一个问题,不一定是这样。谢谢你的帮助!谢谢你的帮助!你的回答是正确的,但是提取$_POST不会有危险吗?@Christian。删除了
extract($\u POST)
。您的答案是正确的,但提取$\u POST不会有危险吗?@Christian。已删除
提取($\u POST)