1. php中include和require的区别
都是包括并运行指定文件
include()【运行时解析】在实行文件每次都要进行读取和评估,include()出错时可以继续运行。
require()【运行前解析】文件只处理一次,文件内容替换require语句部分,require()部分出错主程序会停止。
增加_once()之后表示已经加载的文件将不会被再次加载。
2. curl与file_get_content()区别
curl:libcurl库允许你与各种服务器使用各种类型协议进行连接和通讯,curl可以使用URL的语法模仿浏览器来传输数据,支持多种协议,如ftp, ftps, http, https, gopher, telnet…
curl使用:参考http://php.net/manual/zh/curl.examples-basic.php
- 初始化,创建一个新curl资源
curl_init()
- 设置url和相应的选项【关键步骤】
cutl_setopt()
- 抓取url并把它传递给浏览器
curl_exec()
- 关闭curl资源,并释放系统资源
curl_close
<?php
//使用PHP的cURL模块获取example.com的主页,写入example_homepage.txt中
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
file_get_contents() 将整个文件读入到一个字符串中,和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
参考:http://php.net/manual/zh/function.file-get-contents.php
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
file()将整个文件读入一个数组中
参考http://php.net/manual/zh/function.file.php
array file ( string $filename [, int $flags = 0 [, resource $context ]] )
<?php
// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。
$lines = file('http://www.example.com/');
// 在数组中循环,显示 HTML 的源文件并加上行号。
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。
$html = implode('', file('http://www.example.com/'));
// 从 PHP 5 开始可以使用可选标记参数
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
3. MySQL项目优化 — memcached/redis
php中memcache扩展和memcached扩展的区别 参考:http://www.cnblogs.com/wangtao_20/p/4057591.html
memcached是一个高性能分布式的内存对象缓存系统, 通常被用于降低数据库加载压力以提高动态web应用的响应速度。
基于的库不同(我觉得这是本质区别,库的不同决定了后续差异)。memcache扩展是原生的(基于pecl扩展库)。memcached扩展是基于libmemcached库开发的,所以支持cas操作,cas操作就是多个线程并发修改同一个key的值的时候能够进行处理。
mc支持面向对象和面向过程两套接口操作服务端。而mcd扩展只支持面向对象方式操作。这也是库(libmemcached与libmemcache)的不同影响的。
memcached支持Binary Protocol,而memcache不支持。还是库的不同导致。这意味着memcached会有更高的性能。
mcd扩展提供session处理模块
4.PHP性能优化:
1.XDebug
2.使用PEAR工具类Benchmark逐行获取代码或函数的执行时间。
Benchmark工具类包:
Benchmark_Timer类原理与通过microtime函数获取微秒时间再比较前后两个时间值的差相同。
Benchmark_Iterate类用于调试函数的平均执行时间。
Benchmark_Profiler类用于统计代码和函数的执行时间以及函数的调用次数。
如何获取一行或一段代码的执行时间:
1.通常使用microtime函数获取代码前后的微秒时间数比较两个值的时间差。
<?php
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>
2.使用Benchmark_Timer类
Benchmark_Timer类只需要在调试文件中增加Benchmark_Timer类初始化声明和marker标注,文件尾打印各个标注处的执行时间。
require_once 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$timer->start();
$timer->setMarker("marker01");
usleep(1);
$timer->setMarker("marker02");
usleep(2);
$timer->setMarker("marker03");
usleep(3);
$timer->stop();
$timer->display();
3.使用Benchmark_Iterate类
Benchmark_Iterate类用于调试函数执行的平均时间,与Benchmark_Timer类不同在于可以多次调用同一个函数获取其执行时间的平均值
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
function test($i){
echo $i;
}
$bench->run(100,"test",10);
var_dump($bench->get());
//通过调用test函数100次获取平均执行时间,结果如下
array1 => string '0.000486' (length=8) 2 => string '0.000466' (length=8).............................(中间省略)99 => string '0.000479'
(length=8)100 => string '0.000467' (length=8)'mean' => string'0.000476' (length=8)'iterations' => int 100
结果说明
每个数字表示每次调用的时间
mean表示函数执行的平均时间,如上调用100次test函数的平均时间为0.000476
iterations表示函数调用的次数
4.使用Benchmark_Profiler类【Xdebuger比这个好用】
Benchmark_Profiler类用于统计函数的执行次数和执行时间等
require_once 'Benchmark/Profiler.php';
$profiler = new Benchmark_Profiler(TRUE);
function myFunction() {
global $profiler;
$profiler->enterSection('myFunction'); //do something
$profiler->leaveSection('myFunction');
return;
}//do something
myFunction();//do more