使用php -d参数可临时开启display_errors以在命令行中即时查看错误信息,而无需修改全局php.ini配置。1. 通过php -d display_errors=on script.php命令可临时显示错误;2. 可结合error_reporting=e_all来显示所有错误级别;3. 可用于调试cli脚本、测试代码路径、ci/cd流程或无权限修改php.ini的环境;4. 还可临时调整memory_limit、max_execution_time、include_path和date.timezone等配置项;5. -d参数的优势在于其临时性、无副作用且不影响其他php进程,是高效安全的调试手段。

当你想在命令行(CLI)下运行PHP脚本,同时又需要立即看到脚本可能抛出的错误,但又不希望去改动服务器上全局的
php.ini
php -d
display_errors
-d display_errors=On
要临时开启
display_errors
php
-d display_errors=On
例如,如果你有一个名为
test.php
立即学习“PHP免费学习笔记(深入)”;
<?php // test.php echo $undefinedVariable; // 故意引用一个未定义的变量 echo "Script finished.\n"; ?>
正常情况下,如果你的
php.ini
display_errors
Off
php test.php
php -d display_errors=On test.php
你就会在终端看到类似这样的输出:
PHP Notice: Undefined variable: undefinedVariable in /path/to/test.php on line 3 Script finished.
这正是我们想要的,临时性地让错误信息显现出来,方便你快速定位问题。你也可以用
-d display_errors=stderr
display_errors
display_errors
On
Off
stderr
display_errors
为什么它重要?想象一下,你写了一段PHP代码,在本地测试好好的,一上线却“白屏”了。你完全不知道发生了什么。这时候,如果
display_errors
Off
On
当然,在生产环境,我们通常会把
display_errors
Off
-d
php.ini
这其实是个很经典的场景选择题。什么时候我们应该灵活地用
-d
php.ini
-d
举几个例子:
php.ini
-d display_errors=On
-d
php.ini
php.ini
-d
php.ini
-d
总而言之,
-d
display_errors
-d
-d
display_errors
error_reporting
display_errors
display_errors
error_reporting
php -d display_errors=On -d error_reporting=E_ALL test.php
memory_limit
php.ini
php -d memory_limit=512M my_heavy_script.php
max_execution_time
php -d max_execution_time=300 my_long_script.php
include_path
include_path
php -d include_path=".:/path/to/my/library" my_script.php
date.timezone
php.ini
-d date.timezone=Asia/Shanghai
UTC
记住,
-d
以上就是PHP命令怎样用-d参数临时开启display_errors PHP命令临时显示错误的设置教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号