简体   繁体   English

两次使用后相同的PHP代码返回服务器错误

[英]Same PHP code returning server error after being used twice

The following code snippet checks whether the user is using a mobile browser or not.If so,the icons are shown in new lines using a tr tag. 以下代码段检查用户是否正在使用移动浏览器。如果是,则使用tr标记在新行中显示图标。

function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

Problem: 问题:

It only works for the first two times. 它仅适用于前两次。 In fact, as you can see, the function is commented out in the last 2 times it was being used as to not get a server error (500). 实际上,如您所见,该函数在最近两次被使用时被注释掉,以免出现服务器错误(500)。

I also tried putting the echo part in curly brackets but this still returned a server error when all the 4 php functions below were uncommented. 我还尝试将回声部分放在大括号中,但是当以下所有4个php函数未注释时,这仍然返回服务器错误。

<table align="center" frame='box'>
<tr>
<td align="center" colspan="20">
<a href="assignlocktouser.php"><figure><img src="Images/basicunlock.png" alt="Assign Lock to Basic User" height="64" width="64"/>
<imgcaption></p>Assign Lock to Basic User</imgcaption><figure></a>
<?php if (isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="addDevice.php"><figure><img src="Images/addDevice.png" alt="Add Device" height="64" width="64"/>
<imgcaption></p>Add Device</imgcaption><figure></a>
<?php if (isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="editSequence.php"><figure><img src="Images/editsequence.png" alt="Edit Sequence" height="64" width="64"/>
<imgcaption></p>Edit Sequence</imgcaption><figure></a>
<?php //(if isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="addLock.php"><figure><img src="Images/lock.png" alt="Add New Lock" height="64" width="64"/>
<imgcaption></p>Add New Lock</imgcaption><figure></a>
<?php //(if isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="switchLockOwner.php"><figure><img src="Images/ownerunlock.png" alt="Switch Lock Owner" height="64" width="64"/>
<imgcaption></p>Switch Lock Owner</imgcaption><figure></a>      
</tr>
</table>

Any clues as to why this is happening? 关于为什么发生这种情况的任何线索?

you have your if statement the wrong way round 您有错误的if语句

<?php (if isMobile()) echo "</tr><tr>"; ?>

should be 应该

<?php if (isMobile()) echo "</tr><tr>"; ?>

I always find providing brackets best for reading if statements 我总是发现提供最适合阅读if语句的方括号

so, you could do it like this 所以,你可以这样

<?php if (isMobile()) {echo "</tr><tr>";} ?>

Or, if you wanted to short code it, with a real one liner if statement 或者,如果您想对其进行短编码,则使用一个真正的衬里if语句

<?=((isMobile()) ? "</tr><tr>" : NULL)?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM