dounei9043 2014-01-01 22:08
浏览 75
已采纳

使用函数在if else语句中不起作用PHP [重复]

This question already has an answer here:

I'm trying to use the browscap-php in a if else statement in Apache & PHP 5+. I'm using the Browscap.php file like this below as a example.

Working example:

<?php 
   require 'Browscap.php';
   use phpbrowscap\Browscap;

   if ($newdata == "1") { 
       $current_browser = $bc->getBrowser();
   } else {

   }

?>

Not working example:

<?php 

   if ($newdata == "1") { 
       require 'Browscap.php';
       use phpbrowscap\Browscap;
       $current_browser = $bc->getBrowser();
   } else {
   // do not load anything dealing with browscap
   }
?>

The working example works fine but the second I place the use function in the if else statement it doesn't work. I don't want browscap loaded when I don't need to check for that information. How would I go about doing this?

</div>
  • 写回答

2条回答 默认 最新

  • dtk31564 2014-01-01 22:11
    关注

    Quoting from the manual:

    The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

    (my emphasis)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP