【问题标题】:If Statement in php leading to parse errorphp中的if语句导致解析错误
【发布时间】:2017-07-20 15:54:42
【问题描述】:

我必须开发一个符合一定逻辑的代码

这里

count( $replies ) - 计算页面上的总回复数

$replynumber - 是 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 等的每个回复,基于回复的总数帖子中的用户

我正在尝试根据回复显示基于 adsense 的自定义广告脚本和其他广告脚本

    <?php 

    if( count( $replies ) <= 3 )
                    {   
                     <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ad-->
<ins class="google"
     style="display:inline-block;width:200px;height:90px"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
                }

else if(((count( $replies ) > 3) and (count( $replies ) <= 6)) and ($replynumber == '1','3','5'))
                {           
                 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ad2-->
<ins class="google"
     style="display:inline-block;width:200px;height:90px"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
                }

else if(((count( $replies ) > 6) and (count( $replies ) <= 12)) and ($replynumber == '1','4','7','10'))
                {           
                 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ad3-->
<ins class="google"
     style="display:inline-block;width:200px;height:90px"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>               }               
               }
else if( count( $replies ) > 12 )
                {           
                 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ad4-->
<ins class="google"
     style="display:inline-block;width:200px;height:90px"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
                }                           
?>  

但这会导致解析错误 哪位大神可以帮忙看看

【问题讨论】:

  • which 行解析错误,究竟是什么?你没说。
  • 您也在将纯 JS 注入 PHP;啊,所以就是这样。
  • 即使在脚本 1 中也是如此
  • 那么如何实现所需
  • 您应该分配/回显&lt;script&gt; 作为指令;还是那是伪代码?我这里的问题不清楚

标签: php if-statement logic


【解决方案1】:

是因为php可以解析de script标签。您需要像这样关闭 de php ?> 在脚本之前,他们再次打开它或使用 echo 打印它,例如: 选项1:

if( count( $replies ) <= 3 )
{   
?>
 <script>       
 Script 1 to display
 </script>
<?php
}

选项 2:

<?php 
if( count( $replies ) <= 3 )
   {   
    echo "  <script>       
    Script 1 to display
    </script>";
 }

【讨论】:

  • 谢谢,我已经通过了第一行,但如果再次卡住,则在下一行。您能否就基于选项 -1 重写代码的解决方案提供建议。谢谢
  • 是因为 php 可以解析 de script 标签。您需要像这样关闭 de php ?> 在脚本之前,然后他们像这样 再次打开它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多