Why does this code produce an extra value of 1?
<?=require('./common/aside.php')?>In PHP,require和includeThese statements are used to include content from other PHP files into the current file. They allow for the reuse of code across multiple files, thereby enhancing code reusability and maintainability.
In the aforementioned code,<?=require('./common/aside.php')?>The sentence is "will"../common/aside.phpThe content in the document has been imported into the current file and is now in use.<?=Output the content of this file.<?=expression?>Grammar is equivalent to<?php echo expression; ?>grammar.
If present in the included filesechoFor other `PRINT` statements, the statement that includes this file will return a result of 1. This output value of 1 occurs because the statement returns 1.
To solve this problem, you can./common/aside.phpEnsure that no `output` statement or any other statement generates output in the document. Alternatively, useincludereplacerequireStatement, becauseincludeThe statement does not interrupt program execution; instead, it returns a warning. As a result, no output of 1 will be generated.
How do I block this value of 1?
If you are using it<?=require('./common/aside.php')?>During sentence processing, it was discovered that a '1' appeared at the end of the page; this is becauserequireThe function returns a boolean value indicating whether the file was successfully included, while<?=expression?>Grammar will output this value to the page.
To block this value of 1, you can...<?=require('./common/aside.php')?>Change to<?php require('./common/aside.php'); ?>This way, no value will be rendered on the page, so the number '1' will not appear.
If you wish to use it<?=expression?>The grammar output contains the file content; it can be exported../common/aside.phpReplace any `output` statement with a `assignment` statement, then return a variable at the end, and then<?=require('./common/aside.php')?>Output this variable. This prevents the occurrence of the value 1. For example:
在./common/aside.phpChinese:
<?php
$content = '这是侧边栏的内容';
return $content;
?>In the main file:
<?=require('./common/aside.php')?>The resulting output is:这是侧边栏的内容Instead of showing '1'.