我正在尝试编写一个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>");
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.
如果你有两个成年人,两个孩子,输出是:
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>");
如果您总是打印"You ordered…"行,则不需要else
条件。
if($childtickets >=2)
{
echo 'We hope your Kids enjoy the show';
}
else
{
echo 'sorry';
}