本文主要向大家介绍了一篇文章让你解决PHP语言大多问题,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
查看PHP错误信息
1、修改php.ini配置
php.ini中display_errors / display_startup_errors 设置为On
php.ini中error_reporting 设置为E_ALL
PHP代码中设置error_reporting(E_ALL)
2、PHP中打开报错
[php] view plain copy print?
1. error_reporting(E_ALL);
2. ini_set ('display_errors', 'On');
有些情况下php.ini配置中关闭了错误显示,需要修改php.ini打开错误信息,或者错误信息被导出到了日志文件,这种情况可以直接tailf php_error.log来看错误信息。
多个版本的php或php-cli与php-fpm加载不同的配置
[plain] view plain copy print?
1. which php
2. #得到反馈 /usr/bin/php,查看PHP安装在哪
[plain] view plain copy print?
1. php -i |grep php.ini
2. #得到加载那个php.ini,如下
3. Configuration File (php.ini) Path => /usr/local/etc/php/5.6
4. Loaded Configuration File => /usr/local/etc/php/5.6/php.ini
打印单步调试
1、PHP的getTrace
fiel:发生异常的PHP程序文件名称;
line:发生异常的代码所在的行号;
function:发生异常的函数或方法;
class:发生异常的函数或方法所在的类;
type:调用发生异常的函数或方法的类型(“::”调用静态类成员,“->”调用实例化对象);
args:发生异常的函数或方法所接受的参数。
[php] view plain copy print?
1. <?php
2. $path = "www.phpdo.net";
3. try
4. {
5. file_open($path);
6. }
7. catch(Exception $e)
8. {
9. echo "异常信息".$e->getMessage()."\n";
10. echo "异常代码".$e->getCode()."\n";
11. echo "异常文件".$e->getFile()."\n";
12. echo "异常代码所在行".$e->getLine()."\n";
13. echo "传递路线:";
14. print_r($e->getTrace()); //以数组的形式返回跟踪异常的每一部的传递路线
15. echo $e->getTraceAsString();//返回格式化成字符串的getTrace函数信息
16. }
17.
18. function file_open($path)
19. {
20. if(!file_exists($path))
21. {
22. throw new exception("www.phpdo.net这个文件不存在",1);
23. }
24. if(!fopen($path,"r"))
25. {
26. throw new exception("www.phpdo.net这个文件无法打开",2);
27. }
28. }
29. ?>
30. 结果:
31.
32. 异常信息www.phpdo.net 这个文件不存在 异常代码1 异常文件E:\xampp\htdocs\php\Test\10.2.6.php 异常代码所在行22
33.
34. 传递路线:
35.
36. Array ( [0] => Array ( [file] => E:\xampp\htdocs\php\Test\10.2.6.php [line] => 5 [function] => file_open [args] => Array ( [0] => www.phpdo.net ) ) )
37.
38. #0 E:\xampp\htdocs\php\Test\10.2.6.php(5): file_open(‘www.phpdo.net’) #1 {main}
2、PHP debug_backtrace()
[php] view plain copy print?
1. <?php
2. function one($str1, $str2)
3. {
4. two("Glenn", "Quagmire");
5. }
6. function two($str1, $str2)
7. {
8. three("Cleveland", "Brown");
9. }
10. function three($str1, $str2)
11. {
12. print_r(debug_backtrace());
13. }
14.
15. one("Peter", "Griffin");
16. ?>
3、其他(var_dump,die(),exit()等)
PHP进程跟踪查看
什么是strace?
strace是一个非常简单的工具,它可以跟踪系统调用的执行。最简单的方式,它可以从头到尾跟踪binary的执行,然后以一行文本输出系统调用的名字,参数和返回值。
其实它可以做的更多:
可以对特定的系统调用或者几组系统调用进行过滤
可以通过统计特定系统调用的调用次数、耗费的时间、成功和失败的次数来配置(profile)系统调用的使用I
跟踪发送给进程的信号量
可以通过pid附着(attach)到任何运行的进程
如果你使用的是其它Unix系统,它类似于"truss"。其它更复杂的是Sun的Dtrace.
调用:
strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] …
[ -ofile ] [ -ppid ] … [ -sstrsize ] [ -uusername ] [ command [ arg … ] ]
strace -c [ -eexpr ] … [ -Ooverhead ] [ -Ssortby ] [ command [ arg … ] ]
功能:
跟踪程式执行时的系统调用和所接收的信号.通常的用法是strace执行一直到commande结束.
使用strace php test.php,或者strace -p 进程ID。strace就可以帮助你透过现象看本质,掌握程序执行的过程。
[plain] view plain copy print?
1. 如:
2. strace -p PID分析php-fpm进程
3.
4. at("/data/tdocs/webroot/go/./Widget/boutique.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
5. lstat("/data/tdocs/webroot/go/./Widget/openapi.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
6. lstat("/data/tdocs/webroot/go/./Widget/groupnew.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
7. lstat("/data/tdocs/webroot/go/./Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
8. lstat("/usr/share/pear/Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
9. lstat("/usr/share/php/Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory)
怎么使用它?
找出程序在startup的时候读取的哪个config文件?
[plain] view plain copy print?
1. $ strace php 2>&1 | grep php.ini
2. open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
3. open("/usr/local/lib/php.ini", O_RDONLY) = 4
4. lstat64("/usr/local/lib/php.ini", {st_mode=S_IFLNK|0777, st_size=27, ...}) = 0
5. readlink("/usr/local/lib/php.ini", "/usr/local/Zend/etc/php.ini", 4096) = 27
6. lstat64("/usr/local/Zend/etc/php.ini", {st_mode=S_IFREG|0664, st_size=40971, ...}) = 0
[plain] view plain copy print?
1. strace -e open php 2>&1 | grep php.ini
2. open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
3. open("/usr/local/lib/php.ini", O_RDONLY) = 4
某个进程现在在做什么?
[plain] view plain copy print?
1. strace -p 15427
2. Process 15427 attached - interrupt to quit
3. futex(0x402f4900, FUTEX_WAIT, 2, NULL
4. Process 15427 detached
是谁偷走了时间?
你可以重新编译app,打开profiling,以获取精确的信息。但是通常利用strace附着(attach)一个进程以快速地看一下当前时间花费在哪里非常有用。可以看下是否90%的CPU用在真正的工作,或者用在其它方面了。
[html] view plain copy print?
1. strace -c -p 11084
2. Process 11084 attached - interrupt to quit
3. Process 11084 detached
4. % time seconds usecs/call calls errors syscall
5. ------ ----------- ----------- --------- --------- ----------------
6. 94.59 0.001014 48 21 select
7. 2.89 0.000031 1 21 getppid
8. 2.52 0.000027 1 21 time
9. ------ ----------- ----------- --------- --------- ----------------
10. 100.00 0.001072 63 total
通信过程如何分析-tcpdump?
用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。 tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!