Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword include_once

Include the file only if it has not already been included in this request, even by ordinary include or require.

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

Syntax

include_once $filename

Include the file only if it has not already been included in this request, even by ordinary include or require. The first execution can return a value from the file. A skipped repeat returns true and does not repeat side effects. A missing file issues a warning and returns false. For a local file the resolved path determines identity, so /dir/./file and /dir/file count as the same inclusion.

$file=tempnam(sys_get_temp_dir(),"once"); file_put_contents($file,'<?php ++$count; return 8;'); $count=0;
$a=include $file; $b=include_once dirname($file)."/./".basename($file); unlink($file); echo json_encode([$a,$b,$count]);
[8,true,1]

Examples

<?php
$file=tempnam(sys_get_temp_dir(),"once"); file_put_contents($file,'<?php ++$count; return 8;'); $count=0;
$a=include $file; $b=include_once dirname($file)."/./".basename($file); unlink($file); echo json_encode([$a,$b,$count]);

Result

[8,true,1]

Sources