-1

尽管花费了数小时的谷歌搜索和阅读各种书籍,但我似乎无法在我正在尝试创建的 PHP 程序中输出我的目标(我是新手!)。基本上,一开始我只是想创建一个简单的数组并尝试输出我最喜欢的足球队以及他们的位置或关于他们的事实,然后再进入任何太复杂的事情(不幸的是,即使这也有点困难!)。这是我到目前为止所得到的:

<?php
    <html>
    <head>
    <title>Football teams</title>
    </head>

    <body>
    <?php


    $numbers = array("one", "two", "three", "four");

    $counter = 0;
    do {
        if($counter > 0) {
            $verse = true;
        }
        echo "$numbers[$counter] " ;
        if ($verse) {
            echo "Team " ;
        }
        else {
            echo "Teams" ;
        }
        print "are the best teams in the world <br/>" ;
        $counter++;
    } while($counter < 4);
    $football = $counter;
    while($football) {
        echo "$numbers[$football] men, " ;
        if ($verse) {
            echo "n<br/>" ;
        }
        echo "and rarely lose any games\n\n<br/><br/>" ; 
        $football--;
    }
?>
    </body>
    </html>

现在它可以正常工作,但没有任何错误,但它到处都是,而且顺序不正确,所以我很感激任何帮助。当没有错误时,我自己看不出有什么问题,所以我怀疑其中一个循环有问题。

谢谢你。

我希望得到:

one team are the best team in the world
and rarely lose any games

two teams are the best team in the world
two teams
and rarely lose any games

three teams are the best team in the world
three teams
and rarely lose any games

four teams are the best team in the world
four teams
and rarely lose any games

等等

但我得到的是:

one team are the best team in the world
two teams are the best team in the world
three teams are the best team in the world
four teams are the best team in the world

更新:

$counter = 0;
do {
if($counter > 0) {  
$verse = true;     
    }
echo "$numbers[$counter] " ;
if ($verse) {`
       echo "team " ; 
    }
    else {
        echo "teams " ;
    }
    print "are the best teams in the world<br/>" ;
    $football = $counter;
    while($football) {
    echo "$numbers[$football] teams, " ;
    if ($verse) {
        echo "<br/>" ;
    }
    echo "and rarely lose any games\n\n<br/><br/>" ; 
    $mower--;
}
$counter++;
} while($counter < 10);
?></body>
</html>

使用这段代码我几乎完成了我的目标,这是我得到的输出:

one team are the best team in the world,
two teams are the best team in the world,
two teams,
and rarely lose any games

three teams are the best team in the world,
three teams,
and rarely lose any games

two teams,
and they rarely lose any games,

four teams are the best team in the world,
four teams,
and they rarely lose any games,

但是,我想要的是:

 one team are the best team in the world,
 and they rarely lose any games,

two teams are the best team in the world,
two teams,
and they rarely lose any games,

three teams are the best team in the world,
three teams, two teams,
and they rarely lose any games,

four teams are the best team in the world,
four teams, three teams, two teams,
and they rarely lose any game,

谢谢如果有人可以帮助!

4

4 回答 4

2

是的,亲爱的,你是对的。你只是提前退出循环。

<?php
$numbers = array("one", "two", "three", "four");
$counter = 0;
$verse = false;
do {
        if($counter > 0) {
            $verse = true;
        }
        echo "$numbers[$counter] " ;
        if (!$verse) {
            echo "Team " ;
        }
        else {
            echo "Teams" ;
        }
        print "are the best teams in the world <br/>" ;
        $football = $counter;
        if($football > 0){
           while($football > 0){
            echo "$numbers[$football] Teams, " ;
            $football--;
           }
        }
        if ($verse) {
            echo "<br/>" ;
        }
        echo "and rarely lose any games\n\n<br/><br/>" ; 
        $football--;
    $counter++;
} while($counter < 4);    
?>

然而,这不是编码这个东西的最佳方式

于 2013-03-19T20:51:51.560 回答
0

您错过了数组中的美元符号:

echo "$numbers[football] men, " ;

应该

echo "$numbers[$football] men, " ;

尽管以这种方式将数组包含在字符串中是一种不好的做法,因为它很容易出错。这会更好

echo $numbers[$football] . " men, " ;

另一个问题是当第一个循环结束$counter时将是 4,它高于$numbers数组的任何索引。在下一个循环中,您尝试访问$numbers[4]会产生所谓的越界错误。相反,$football--应该在第二个循环的开头,而不是结尾。现在第一次迭代,$football 将被设置为 3,然后$numbers[3]将被访问,在最后一次迭代中,1 将变为 0 并被$numbers[0]访问。之后while($football)不再正确,循环退出。

于 2013-03-19T20:37:55.510 回答
0

您正在寻找的接缝是如何通过数组循环并从中输出内容。像这样做:

$myArray = array( 'element1', 'element2', ... );

foreach ( $myArray as $element )
{
    echo $myelement;
}

如果你有一些更复杂的结构,就像你说的,关于足球队的事实,你可以这样做:

$teams = array(
    array(
        'name' => 'Team1',
        'fact1' => 'fact1',
        ... etc ...
    ),
    array(
        'name' => 'Team1',
        'fact1' => 'fact1',
        ... etc ...
    ),
    ... etc ...
);

foreach ( $teams as $team )
{
    echo "Teamname: {$team['name']}";
    echo "Fact1: {$team['fact1']}";
    ... etc ...
}
于 2013-03-19T20:39:57.313 回答
0
$numbers = array("one", "two", "three", "four");

$numbersrev = array_reverse($numbers);

foreach ($numbers as $numbersIten) 
{
    echo "Team " . $numbersIten . ' are the best teams in the world<br/>';
}
foreach ($numbersrev as $numbersrevIten) 
{
    echo '<br/>'.$numbersrevIten . ' men, <br/> and rarely lose any games <br/>';
}
于 2013-03-19T20:53:13.987 回答