Parse error: syntax error, unexpected 'use' (T_USE) in C:\wamp\www\calculater\wp-content\themes\calculater\page.php on line [duplicate]

孤街醉人 提交于 2019-12-02 21:29:33

问题


ob_start();  
require_once '\dompdf\autoload.inc.php';

use Dompdf\Dompdf;

 //use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new DOMPDF();
$html = "
print_r($_POST);
";

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("page.pdf", $pdf);

?>  
<a href="./page.pdf" download="page.pdf">Download the pdf</a>
   <?php
exit;
?>

I try to do downloadable PDF script but getting parse error.


回答1:


You have problem with use of use:)

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.

Try this code:

use Dompdf\Dompdf;

ob_start();  
require_once '\dompdf\autoload.inc.php';

// instantiate and use the dompdf class
$dompdf = new DOMPDF();
$html = "
print_r($_POST);
";

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("page.pdf", $pdf);

?>  
<a href="./page.pdf" download="page.pdf">Download the pdf</a>
   <?php
exit;
?>


来源:https://stackoverflow.com/questions/36130367/parse-error-syntax-error-unexpected-use-t-use-in-c-wamp-www-calculater-wp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!