php 小问题集合

来源:互联网 发布:理查德施特劳斯 知乎 编辑:程序博客网 时间:2024/06/03 02:25

1.file exist error

之前在做监控模块的编写,语言用的是php,环境是linux

我把工程目录直接放在了root的home目录下面,因此设置的工程目录是‘~/hengyun_monitor/xxxxx’,在用file_exists时出现了找不到文件的情况

把目录改为'/root/hengyun_monitor/xxx'即可找到。

<?php$filename="~/hengyun_monitor/rra/mem-swap-avail.rrd";if(file_exists($filename)){        echo("file exists");}else{        echo("file do not exists");}?>
return:

file do not exists

<?php$filename="/root/hengyun_monitor/rra/mem-swap-avail.rrd";if(file_exists($filename)){        echo("file exists");}else{        echo("file do not exists");}?>
return:

file exists

小错误导致我在找rrdtool的各种问题,因此mark一下

2. Fatal error: Cannot use object of type stdClass as array

导致这个问题的原因是在php数组中如果有object元素,必须使用‘->’调用,不能直接使用数组方式调用。

object(stdClass)[14]  public 'hello' => string 'computer' (length=8)
比如上面的例子,如果$temp['hello']这样写就是错误的,必须$temp->hello这样调用。主要的原因应该是对象的调用和数组调用的区别。