在php-else语句中添加时遇到问题


Having trouble adding in php else statement

我正在使用这个php移动检测脚本https://github.com/serbanghita/Mobile-Detect

我已经挑出了我想使用的代码,但我想加入一个"其他"选项,但我似乎无法做到

这是代码

<?php $check = $detect->isMobile(); if($check): ?>
   Hello you're on a phone
<?php endif; ?>

这就是我想要实现的

<?php $check = $detect->isMobile(); if($check): ?>
   You are phone 
<? else ?>
   You're something else
<?php endif; ?>

但当我这样做时,它会抛出一个Parse错误:语法错误

您可以尝试这种更简单的语法:

<?php $check = $detect->isMobile(); if($check){ ?>
   You are phone 
<? } else { ?>
   You're something else
<?php } ?>

上面的内容更好理解,但你也可以使用这个:

<?php $check = $detect->isMobile(); if($check): ?>
   You are phone 
<? else: ?>
   You're something else
<?php endif; ?>

我宁愿说,使用第一部分。虽然第二种语法很好,但第一种更容易理解。

您忘记了第三行的else之后的:

<?php $check = $detect->isMobile(); if($check): ?>
   You are phone 
<? else: ?> // here
   You're something else
<?php endif; ?>