[英]'if 'condition is true but 'else' is executed
I have an problem with this code :我对这段代码有问题:
<?php $nb = 10; ?>
<script>
if(window.innerHeight+1 == screen.height)
{
document.write("<?php $nb = 10; ?>");
alert("1");
alert(window.innerHeight +1+ " " +screen.height+ " 1");
}else{
document.write("<?php $nb = 8; ?>");
alert("2");
alert(window.innerHeight +1+ " " +screen.height + " 2");
}
<script>
<?php
$blink = "";
$i = 0;
for($i=0;$i<$nb;$i++)
{
if($_SESSION['Collection'][$i]->getUrgence() == "FORTE")
{
$blink = " class=\"Blink\"";
}
elseif($_SESSION['Collection'][$i]->getUrgence() == "MOYENNE")
{
$blink = "";
}
echo '<tr>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getNumDossier().'</td>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getNomTicket().'</td>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getService().'</td>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getImpact().'</td>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getUrgence().'</td>';
echo '<td'.$blink.'>'.$_SESSION['Collection'][$i]->getDateOuverture().'</td>';
echo '</tr>';
}
?>
In fact I want to detect if the browser is in full screen, if it is I put my variable to 10 otherwise I put it to 8. After I use this variable in my php code to display a number of values.事实上,我想检测浏览器是否全屏,如果是,我把我的变量设置为 10,否则我把它设置为 8。在我的 php 代码中使用这个变量来显示一些值之后。
The 'if' is true, alert("1") is been printed but $nb =8, it was really a strange thing no ? 'if' 为真,alert("1") 被打印,但是 $nb =8,这真的是一件很奇怪的事情,不是吗?
Thank You for reading !感谢您的阅读!
I think you have two competing ideas.我认为你有两个相互竞争的想法。 As @Titus has stated in his comment all of the PHP code is compiled first.
正如@Titus 在他的评论中所说,首先编译所有 PHP 代码。 So $nb is first set to 10 and then set to 8.
所以 $nb 首先设置为 10,然后设置为 8。
Once the HTML output is generated $nb is 8 anywhere in the HTML it is used.生成 HTML 输出后,$nb 在 HTML 中使用它的任何地方都是 8。 You need to have an if/else in PHP or use a separate variable.
您需要在 PHP 中使用 if/else 或使用单独的变量。
<?php $nb = 10; ?>
<script>
if(window.innerHeight+1 == screen.height)
{
document.write("8");
alert("1");
alert(window.innerHeight +1+ " " +screen.height+ " 1");
}else{
document.write("8");
alert("2");
alert(window.innerHeight +1+ " " +screen.height + " 2");
}
You should go into your developer tools and inspect the HTML to see what values are generated on the client side.您应该进入您的开发人员工具并检查 HTML 以查看在客户端生成了哪些值。 PHP = server side code, JS = client side code (since you aren't using node).
PHP = 服务器端代码,JS = 客户端代码(因为您没有使用节点)。
Your intentions for the snippet aren't clear so there isn't a workable example I can give you.您对该代码段的意图尚不清楚,因此我无法为您提供可行的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.