尽管有几个小时的谷歌搜索和阅读各种书籍,我似乎无法输出我正在尝试创建的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,
谢谢,如果有人可以提供帮助!!
答案 0 :(得分: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);
?>
然而,这不是编码此事的最佳方式
答案 1 :(得分: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)
不再为真,循环退出。
答案 2 :(得分: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 ...
}
答案 3 :(得分: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/>';
}