如果..Else声明

时间:2014-11-12 06:02:44

标签: php if-statement

我正在尝试编写一个If..Else语句,如果至少有两个孩子的票,则会写入     购买后,程序会添加消息

"We hope your kids enjoy the show!"

在现有输出的末尾。

    $adultTickets = $_POST['adultTickets'];
    $childTickets = $_POST['childTickets'];

    $totalCost = $adultTickets * 6.50 + $childTickets * 4.50;
    if ($childTickets >= 2);
    print("<p>We hope your kids enjoy the show!</p>");

    else 
    print("<p>You ordered $adultTickets adult tickets and
    $childTickets children's tickets.</p>");
    print("<p>Your cost is $$totalCost.</p>");

2 个答案:

答案 0 :(得分:1)

if ($childTickets >= 2);
                       ^  Remove this

在那里,它失败了

Line : 10,   Error type : 4
Message : syntax error, unexpected T_ELSE

如果您将其删除并且有2张成人票和1张儿童票,则输出为:

You ordered 2 adult tickets and 1 children's tickets.

Your cost is $17.5.

如果您有2名成人,2名儿童,则输出为:

We hope your kids enjoy the show!

Your cost is $22.

编辑:

在回复您的评论时,您需要这样的代码。

print("<p>You ordered $adultTickets adult tickets and
$childTickets children's tickets.</p>");

if ($childTickets >= 2)
    print("<p>We hope your kids enjoy the show!</p>");

如果您一直在打印&#34;您订购了...... {34},那么您不需要else条件。线。

答案 1 :(得分:0)

if($childtickets >=2)
{
   echo 'We hope your Kids enjoy the show';
}
else
{
   echo 'sorry';
}