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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?