我在PHP的内置服务器上输出错误时遇到了一个怪异的问题,它的确看起来像是个错误,但我不确定100%。
示例脚本:
<?php
error_reporting(-1);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('ignore_repeated_errors', true);
trigger_error('my notice', E_USER_NOTICE);
trigger_error('my warning', E_USER_WARNING);
如果在控制台中执行此代码(
php example.php
),则会得到以下预期输出:PHP Notice: my notice in C:\php\example.php on line 7
PHP Stack trace:
PHP 1. {main}() C:\php\example.php:0
PHP 2. trigger_error('my notice', 1024) C:\php\example.php:7
PHP Warning: my warning in C:\php\example.php on line 8
PHP Stack trace:
PHP 1. {main}() C:\php\example.php:0
PHP 2. trigger_error('my warning', 512) C:\php\example.php:8
但是,如果将此脚本作为网页(
php -S localhost:9000 example.php
)运行,并打开URL,则会在控制台中获得以下输出:PHP 7.1.10 Development Server started at Tue Oct 10 19:46:05 2017
Listening on http://localhost:9000
Document root is C:\php
Press Ctrl-C to quit.
[Tue Oct 10 19:46:06 2017] PHP Notice: my notice in C:\php\example.php on line 7
[Tue Oct 10 19:46:06 2017] PHP Stack trace:
[Tue Oct 10 19:46:06 2017] PHP 1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:06 2017] PHP 2. trigger_error('my notice', 1024) C:\php\example.php:7
[Tue Oct 10 19:46:06 2017] PHP Warning: my warning in C:\php\example.php on line 8
[Tue Oct 10 19:46:06 2017] PHP Stack trace:
[Tue Oct 10 19:46:06 2017] PHP 1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:06 2017] PHP 2. trigger_error('my warning', 512) C:\php\example.php:8
[Tue Oct 10 19:46:07 2017] PHP Notice: my notice in C:\php\example.php on line 7
[Tue Oct 10 19:46:07 2017] PHP Stack trace:
[Tue Oct 10 19:46:07 2017] PHP 1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:07 2017] PHP 2. trigger_error('my notice', 1024) C:\php\example.php:7
[Tue Oct 10 19:46:07 2017] PHP Warning: my warning in C:\php\example.php on line 8
[Tue Oct 10 19:46:07 2017] PHP Stack trace:
[Tue Oct 10 19:46:07 2017] PHP 1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:07 2017] PHP 2. trigger_error('my warning', 512) C:\php\example.php:8
最奇怪的是,如果在示例脚本的底部添加
trigger_error('my error', E_USER_ERROR);
,则 double 输出是固定的。是什么原因造成的? INI配置是错误还是有些怪异?
最佳答案
实际上,答案就在问题上。您使用脚本启动了内部Web服务器,它将脚本用作路由器。这意味着它将为收到的每个请求调用该脚本。当然,浏览器需要favicon.ico
,其余的您已经知道了。
在没有路由器的情况下启动PHP的内部Web服务器,使用-t
命令行选项告诉它在哪里可以找到服务器文件(网站的根目录):
php -S localhost:9000 C:\php
关于php - PHP本地服务器-两次错误-错误还是没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671988/