Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword require

Evaluate a required PHP file in the current inclusion scope and use its returned value.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

require $filename

Evaluate a required PHP file in the current inclusion scope and use its returned value. If PHP cannot open the file it emits a warning and raises Error in PHP 8. Catching that error can allow recovery, while an uncaught error terminates normal execution.

$file=tempnam(sys_get_temp_dir(),"req"); file_put_contents($file,'<?php return ["units"=>4];');
echo (require $file)["units"]; unlink($file); set_error_handler(fn()=>true); try {require $file;}catch(Error $e){echo " ",get_class($e);}finally{restore_error_handler();}
4 Error

Examples

<?php
$file=tempnam(sys_get_temp_dir(),"req"); file_put_contents($file,'<?php return ["units"=>4];');
echo (require $file)["units"]; unlink($file); set_error_handler(fn()=>true); try {require $file;}catch(Error $e){echo " ",get_class($e);}finally{restore_error_handler();}

Result

4 Error

Sources