Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

Palavra-chave require

Avalia o ficheiro PHP obrigatório no âmbito da inclusão e fornece o respetivo resultado.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

require $filename

Avalia o ficheiro PHP obrigatório no âmbito da inclusão e fornece o respetivo resultado. Se o PHP não o puder abrir, gera um aviso e lança Error desde PHP 8. Intercetar o erro permite continuar e um erro não intercetado termina a execução normal.

$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

Exemplos

<?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();}

Resultado

4 Error

Fontes