一、mysql教程驱动mysqlnd 一直以来,php教程都是通过mysql客户端连接mysql,而现在mysql官方已经推出php版的mysql客户端,而这个mysqlnd有效降低内存的使用以及提高性能。具体可以看: http://dev.mysql.com/downloads/connector/php-mysqlnd/ http://forge.mysql.com/wiki/PHP_MYSQLND 从图中可以看出,使用mysqlnd少了从mysql驱动中复制数据到php扩展这一步。mysqlnd使用copy-on-write,也就是写时复制,读引用。 mysqlnd已经内置在php5.3的源码中,编译的时候使用--with-mysql=mysqlnd、--with-mysqli=mysqlnd 和 --with-pdo-mysql=mysqlnd 安装mysqlnd驱动。 mysqlnd的优点 编译php更方便了,不需要libmysql,已经内置在源码中
- 编译php更方便了,不需要libmysql,已经内置在源码中
- 使用php许可,避免版权问题
- 使用php的内存管理,支持php内存限制(memory_limit)
- 所有数据在内存只有一份,之前的libmysql有两份,参考上图
- 提供性能统计功能,帮助分析瓶颈
- mysqli支持长连接(persistent connections)
- 性能绝对比libmysql要快
- 在驱动层增加缓存机制
- md5()大概提高了10%-15%的性能
- Better stack implementation in the engine,没明白
- 常量保存在ROM里(Constants moved to read-only memory),这里没明白意思
- 改进异常处理,操作码(opcode)更简洁
- 解决了include(require)_once重复打开的问题,之前once我都是用静态变量实现的,终于解决这个问题了
- 用gcc4编译的二进制文件将更小
- 整体性能提高了5%-15%
四、名字空间(namespace)
这是个很好的功能,没加入之前都是用前缀来解决命名污染的,方法有点山寨,呵呵。五、延迟静态绑定(Late Static Binding) 我估计php的静态是在预编译时就固定好的,所以在继承的时候,父类里的self指的是父类,而不是子类。而php5.3加入了新的语法static,可以在运行时候捕捉当前类。 比较典型的例子就是单件模式了:
class ParentClass { <br>static private $_instance; <br>private function __construct() { <br>} <br>static public function getInstance() { <br>if (!isset(self::$_instance)) { <br>self::$_instance = new self(); <br>} <br>return self::$_instance; <br>} <br>} |
class ParentClass { <br>static private $_instance; <br>private function __construct() { <br>} <br>static public function getInstance() { <br>if (!isset(self::$_instance)) { <br>$class = static::getClass();// 使用static延迟绑定 <br>self::$_instance = new $class(); <br>} <br>return self::$_instance; <br>} <br>static public function getClass() { <br>return __CLASS__; <br>} <br>} |
$date = strtotime("08-01-07 00:00:00");//php 认为格式 是年-月-日 <br>var_dump(date("Y-m-d", $date)); // string(10) "2008-01-07" <br>$date = date_create_from_format("m-d-y", "08-01-07");//告诉php格式是 月-日-年 <br>var_dump($date->format('Y-m-d')); // string(10) "2007-08-01" |
$lambda = function () { echo "Hello World!n"; }; |
function replace_spaces ($text) { <br>$replacement = function ($matches) { <br>return str_replace ($matches[1], ' ', ' ').' '; <br>}; <br>return preg_replace_callback ('/( +) /', $replacement, $text); <br>} |
function replace_spaces ($text) { <br>return preg_replace_callback ('/( +) /', <br>function ($matches) { <br>return str_replace ($matches[1], ' ', ' ').' '; <br>}, $text); <br>} |
function (normal parameters) use ($var1, $var2, &$refvar) {} |
$foo = ONE; |
$bar = |
php的GC机制采用引用计数机制,引用计数是很简单高效的GC机制,不过缺点也很明显,不能完全回收所有已无效的变量,例如变量相互引用了,就无法回收了,5.3里加入的GC函数,其实就是起到增强GC机制的作用。
gc_enable(); // 激活GC,增强GC机制,回收循环引用的无效变量 <br>var_dump(gc_collect_cycles()); // 强制回收已无效的变量 <br>gc_disable(); // 禁用GC |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
