使用函数在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>

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)

You can put the use statement above the if. The use statements does not load anything. The require statement is the one that loads the file, and you can keep that in the if.