‘壹’ 字符串的16、php中介绍
string是一系列字符。在 php 中,字符和字节一样,也就是说,一共有 256 种不同字符的可能性。这也暗示 php 对 Unicode 没有本地支持。请参阅函数utf8_encode()和utf8_decode()以了解有关 Unicode 支持
注:一个字符串变得非常巨大也没有问题,php 没有给字符串的大小强加实现范围,所以完全没有理由担心长字符串。
语法
字符串可以用三种字面上的方法定义。
单引号
双引号
定界符 指定一个简单字符串的最简单的方法是用单引号(字符 ')括起来。
要表示一个单引号,需要用反斜线(/)转义,和很多其它语言一样。如果在单引号之前或字符串结尾需要出现一个反斜线,需要用两个反斜线表示。注意如果试图转义任何其它字符,反斜线本身也会被显示出来!所以通常不需要转义反斜线本身。
注:在 php 3 中,此情况下将发出一个 E_NOTICE 级的警告。
注:和其他两种语法不同,单引号字符串中出现的变量和转义序列不会被变量的值替代。
<?phpecho 'this is a simple string';echo 'You can also have embedded newlines instrings this way as it isokay to do';// Outputs: Arnold once said: I'll be backecho 'Arnold once said: I/'ll be back';// Outputs: You deleted C:/*.*?echo 'You deleted C://*.*?';// Outputs: You deleted C:/*.*?echo 'You deleted C:/*.*?';// Outputs: This will not expand: /n a newlineecho 'This will not expand: /n a newline';// Outputs: Variables do not $expand $eitherecho 'Variables do not $expand $either';?>
此外,如果试图转义任何其它字符,反斜线本身也会被显示出来!
双引号字符串最重要的一点是其中的变量名会被变量值替代。细节参见字符串解析。 另一种给字符串定界的方法使用定界符语法(“<<<”)。应该在 <<< 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。
结束标识符必须从行的第一列开始。同样,标识符也必须遵循 php 中其它任何标签的命名规则:只能包含字母数字下划线,而且必须以下划线或非数字字符开始。
例子 11-3. 非法的例子
<?phpclass foo { public $bar = <<<EOTbarEOT;}?>
定界符文本表现的就和双引号字符串一样,只是没有双引号。这意味着在定界符文本中不需要转义引号,不过仍然可以用以上列出来的转义代码。变量会被展开,但当在定界符文本中表达复杂变量时和字符串一样同样也要注意。
例子 11-4. 定界符字符串例子
<?php$str = <<<EODExample of stringspanning multiple linesusing heredoc syntax.EOD;/* More complex example, with variables. */class foo{ var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); }}$foo = new foo();$name = 'MyName';echo <<<EOTMy name is $name. I am printing some $foo->foo.Now, I am printing some {$foo->bar[1]}.This should print a capital 'A': /x41EOT;?>注:定界符支持是 php 4 中加入的。 当用双引号或者定界符指定字符串时,其中的变量会被解析。
有两种语法,一种简单的和一种复杂的。简单语法最通用和方便,它提供了解析变量,数组值,或者对象属性的方法。
复杂语法是 php 4 引进的,可以用花括号括起一个表达式。
简单语法如果遇到美元符号($),解析器会尽可能多地取得后面的字符以组成一个合法的变量名。如果想明示指定名字的结束,用花括号把变量名括起来。
<?php$beer = 'Heineken';echo $beer's taste is great; // works, ' is an invalid character for varnamesecho He drank some $beers; // won't work, 's' is a valid character for varnamesecho He drank some ${beer}s; // worksecho He drank some {$beer}s; // works?>
同样也可以解析数组索引或者对象属性。对于数组索引,右方括号(])标志着索引的结束。对象属性则和简单变量适用同样的规则,尽管对于对象属性没有像变量那样的小技巧。
<?php// These examples are specific to using arrays inside of strings.// When outside of a string, always quote your array string keys// and do not use {braces} when outside of strings either.// Let's show all errorserror_reporting(E_ALL);$fruits = array('strawberry' => 'red', 'banana' => 'yellow');// Works but note that this works differently outside string-quotesecho A banana is $fruits[banana].;// Worksecho A banana is {$fruits['banana']}.;// Works but php looks for a constant named banana first// as described below.echo A banana is {$fruits[banana]}.;// Won't work, use braces. This results in a parse error.echo A banana is $fruits['banana'].;// Worksecho A banana is . $fruits['banana'] . .;// Worksecho This square is $square->width meters broad.;// Won't work. For a solution, see the complex syntax.echo This square is $square->width00 centimeters broad.;?>
对于任何更复杂的情况,应该使用复杂语法。
复杂(花括号)语法不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。
事实上,用此语法可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用 { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 { 后面时被识别(用“{/$”或者“/{$”来得到一个字面上的“{$”)。用一些例子可以更清晰:
<?php// Let's show all errorserror_reporting(E_ALL);$great = 'fantastic';// 不行,输出为:This is { fantastic}echo This is { $great};// 可以,输出为:This is fantasticecho This is {$great};echo This is ${great};// Worksecho This square is {$square->width}00 centimeters broad.;// Worksecho This works: {$arr[4][3]};// This is wrong for the same reason as $foo[bar] is wrong// outside a string. In otherwords, it will still work but// because php first looks for a constant named foo, it will// throw an error of level E_NOTICE (undefined constant).echo This is wrong: {$arr[foo][3]};// Works. When using multi-dimensional arrays, always use// braces around arrays when inside of stringsecho This works: {$arr['foo'][3]};// Works.echo This works: . $arr['foo'][3];echo You can even write {$obj->values[3]->name};echo This is the value of the var named $name: {${$name}};?>
访问和修改字符串中的字符
字符串中的字符可以通过在字符串之后用花括号指定所要字符从零开始的偏移量来访问和修改。
注:为了向下兼容,仍然可以用方括号。不过此语法自 php 4 起已过时。
例子 11-5. 一些字符串例子
<?php// Get the first character of a string$str = 'This is a test.';$first = $str{0};// Get the third character of a string$third = $str{2};// Get the last character of a string.$str = 'This is still a test.';$last = $str{strlen($str)-1};// Modify the last character of a string$str = 'Look at the sea';$str{strlen($str)-1} = 'e';?> 字符串可以用“.”(点)运算符连接。注意这里不能用“+”(加)运算符。更多信息参见字符串运算符。
有很多实用函数来改变字符串。
普通函数见字符串函数一节,高级搜索和替换见正则表达式函数(两种风格:Perl 和 POSIX 扩展)。
还有 URL 字符串函数,以及加密/解密字符串的函数(mcrypt 和 mhash)。
最后,如果还是找不到想要的函数,参见字符类型函数。 可以用 (string) 标记或者strval()函数将一个值转换为字符串。当某表达式需要字符串时,字符串的转换会在表达式范围内自动完成。例如当使用echo()或者print()函数时,或者将一个变量值与一个字符串进行比较的时候。阅读手册中有关类型和类型戏法中的部分有助于更清楚一些。参见settype()。
布尔值TRUE将被转换为字符串 1,而值FALSE将被表示为 (即空字符串)。这样就可以随意地在布尔值和字符串之间进行比较。
整数或浮点数数值在转换成字符串时,字符串由表示这些数值的数字字符组成(浮点数还包含有指数部分)。
数组将被转换成字符串 Array,因此无法通过echo()或者print()函数来输出数组的内容。请参考下文以获取更多提示。
对象将被转换成字符串 Object。如果因为调试需要,需要将对象的成员变量打印出来,请阅读下文。如果希望得到该对象所依附的类的名称,请使用函数get_class()。自 php 5 起,如果合适可以用 __toString() 方法。
资源类型总是以 Resource id #1 的格式被转换成字符串,其中 1 是 php 在运行时给资源指定的唯一标识。如果希望获取资源的类型,请使用函数get_resource_type()。
NULL将被转换成空字符串。
正如以上所示,将数组、对象或者资源打印出来,并不能提供任何关于这些值本身的有用的信息。请参阅函数print_r()和var_mp(),对于调试来说,这些是更好的打印值的方法。
可以将 php 的值转换为字符串以永久地储存它们。这种方法被称为序列化,可以用函数serialize()来完成该操作。如果在安装 php 时建立了 WDDX 支持,还可以将 php 的值序列化为 XML 结构。 当一个字符串被当作数字来求值时,根据以下规则来决定结果的类型和值。
如果包括“.”,“e”或“E”其中任何一个字符的话,字符串被当作float来求值。否则就被当作整数。
该值由字符串最前面的部分决定。如果字符串以合法的数字数据开始,就用该数字作为其值,否则其值为 0(零)。合法数字数据由可选的正负号开始,后面跟着一个或多个数字(可选地包括十进制分数),后面跟着可选的指数。指数是一个“e”或者“E”后面跟着一个或多个数字。
<?php$foo = 1 + 10.5; // $foo is float (11.5)$foo = 1 + -1.3e3; // $foo is float (-1299)$foo = 1 + bob-1.3e3; // $foo is integer (1)$foo = 1 + bob3; // $foo is integer (1)$foo = 1 + 10 Small Pigs; // $foo is integer (11)$foo = 4 + 10.2 Little Piggies; // $foo is float (14.2)$foo = 10.0 pigs + 1; // $foo is float (11)$foo = 10.0 pigs + 1.0; // $foo is float (11)?>
此转换的更多信息见 Unix 手册中关于 strtod(3) 的部分。
如果想测试本节中的任何例子,可以拷贝和粘贴这些例子并且加上下面这一行自己看看会发生什么:
<?phpecho /$foo==$foo; type is . gettype ($foo) . <br />/n;?>
不要指望在将一个字符转换成整型时能够得到该字符的编码(可能也会在 C 中这么做)。如果希望在字符编码和字符之间转换,请使用ord()和chr()函数。
‘贰’ @extract这个函数在PHP中的用处是什么呢 - 技术问答
楼主不看手册么{:2_150:} [php] \"blue\",? ?? ?? ?? ?? ?? ? \"size\"??=> \"medium\",? ?? ?? ?? ?? ?? ? \"shape\" => \"sphere\");extract($var_array, EXTR_PREFIX_SAME, \"wddx\");echo \"$color, $size, $shape, $wddx_size\\n\";?>[/php]
‘叁’ 阿里云默认centos7上怎么安装php
首先更新系统软件</str>
$ yum update
安装nginx</str></str>
1.安装nginx源
$ yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm2.安装nginx
$ yum install nginx
3.启动nginx
$ service nginx start
Redirecting to /bin/systemctl start nginx.service4.访问http://你的ip/
如果成功安装会出来nginx默认的欢迎界面
安装MySQL5.7.*
</str>
1.安装mysql源</str>
$ yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm2.安装mysql
$ yum install mysql-community-server
确认一下mysql的版本,有时可能会提示mysql5.63.安装mysql的开发包,以后会有用
$ yum install mysql-community-devel
4.启动mysql
$ service mysqld start
Redirecting to /bin/systemctl start mysqld.service5.查看mysql启动状态
$ service mysqld status
出现pid
证明启动成功
6.获取mysql默认生成的密码
$ grep 'temporary password' /var/log/mysqld.log2015-12-05T05:41:09.104758Z 1 [Note] A temporary password is generated for root@localhost: %G1Rgns!dD!v</str>
加粗的就是生成的密码
7.换成自己的密码
$ mysql -uroot -p
Enter password:输入上面的密码
成功输入后进入一下步,这里你估计会输入 好几次才进去8. 更换密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';这个密码一定要足够复杂,不然会不让你改,提示密码不合法;9.退出mysql;
mysql> quit;
10.用新密码再登录,试一下新密码
$ mysql -uroot -p
Enter password:输入你的新密码
11.确认密码正确后,退出mysql;
mysql> quit;
编译安装php7.0.0
</str>
</str>
1.下载php7源码包</str>
$ cd /root & wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror2.解压源码包</str>
$ tar -xvf php7.tar.gz
3.</str>
$ cd php-7.0.1
4.安装php依赖包</str>
</str>
$ yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel5.编译配置,这一步我们会遇到很多configure error,我们一一解决,基本都是相关软件开发包没有安装导致</str>
</str>
$ ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache
configure error:
1.configure: error: xml2-config not found. Please check your libxml2 installation.
解决:
$ yum install libxml2 libxml2-devel
2.configure: error: Cannot find OpenSSL's <evp.h>
解决:
$ yum install openssl openssl-devel
3.configure: error: Please reinstall the BZip2 distribution解决:
$ yum install bzip2 bzip2-devel
4.configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/解决:
$ yum install libcurl libcurl-devel
5.If configure fails try --with-webp-dir=<DIR> configure: error: jpeglib.h not found.
解决:
$ yum install libjpeg libjpeg-devel
6.If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yesconfigure: error: png.h not found.
解决:
$ yum install libpng libpng-devel
7.If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yeschecking for png_write_image in -lpng... yesIf configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.
解决:
$ yum install freetype freetype-devel
8.configure: error: Unable to locate gmp.h解决:
$ yum install gmp gmp-devel
9.configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:
$ yum install libmcrypt libmcrypt-devel
10.configure: error: Please reinstall readline - I cannot find readline.h解决:
$ yum install readline readline-devel
11.configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution解决:
$ yum install libxslt libxslt-devel
6.编译与安装
$ make && make install
这里要make好久,要耐心一下
7.添加 PHP 命令到环境变量
$ vim /etc/profile
在末尾加入
PATH=$PATH:/usr/local/php/bin
export PATH
要使改动立即生效执行
$ ./etc/profile
或
$ source /etc/profile
查看环境变量
$ echo $PATH
查看php版本
$ php -v
8.配置php-fpm
$ cp php.ini-proction /etc/php.ini
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf$ cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm$ chmod +x /etc/init.d/php-fpm
9.启动php-fpm
$ /etc/init.d/php-fpm start
配置nginx虚拟机,绑定域名</str>
1.
</str>
$ vim /etc/nginx/conf.d/php7.thinkcmf.com.conf这里可以把php7.thinkcmf.com.conf改成自己的域名把下面的内容复制到php7.thinkcmf.com.conf里server{
listen 80;
server_name php7.thinkcmf.com;
root /var/www/html/php7.thinkcmf.com; # 该项要修改为你准备存放相关网页的路径location / {
index index.php index.html index.htm;
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。
rewrite ^/(.*)$ /index.php/$1;
#若是子目录则使用下面这句,将subdir改成目录名称即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;}
}
#proxy the php scripts to php-fpm
location ~ \.php {
include fastcgi_params;
##pathinfo支持start
#定义变量 $path_info ,用于存放pathinfo信息set $path_info "";
#定义变量 $real_script_name,用于存放真实地址set $real_script_name $fastcgi_script_name;#如果地址与引号内的正则表达式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {#将文件地址赋值给变量 $real_script_name
set $real_script_name $1;
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;fastcgi_param SCRIPT_NAME $real_script_name;fastcgi_param PATH_INFO $path_info;
###pathinfo支持end
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}
2.重启nginx
$ service nginx reload
3.
$ vim /var/www/html/php7.thinkcmf.com/index.php</str>
把下面的代码复制到这个文件 里
<?php
phpinfo();
4.查看访问http://php7.thinkcmf.com
‘肆’ php文件用什么软件打开
php文件可以用记事本打开。
一个访问者打开主页时,服务端便执行PHP的命令并将执行结果发送至访问者的浏览器中,这类似于ASP和CoildFusion,然而PHP和他们不同之处在于PHP开放源码和跨越平台,PHP可以运行在WINDOWS NT和多种版本的UNIX上。
PHP脚本程序主要应用于Web服务端开发,命令行和编写桌面应用程序。PHP支持和所有web开发语言之间的WDDX复杂数据交换。关于相互连接,PHP 已经支持了对Java对象的即时连接,并且可以透明地将其用作PHP对象。
PHP语言的特点:
1、PHP它驱动全球超过2亿多个网站,有全球超过81.7%的公共网站在服务器端采用PHP。PHP常用的数据结构都内置了,使用起来方便简单,也一点都不复杂,表达能力相当灵活。
2、PHP在不断兼容着类似closures和命名空间等技术,同时兼顾性能和当下流行的框架。版本是7之后,一直在提供更高性能的应用。
3、PHP 语言在补丁漏洞升级过程中,核心部分植入简易,且速度快。
以上内容参考网络——PHP
‘伍’ php5.6编译生成libphp5.so的问题求助
1 libphp5.so是php5提供的,
2 你还需要编译php5才能生成这个文件 你在PHP的configure的时候,加上: --with-apxs2=/usr/local/apache/bin/apxs 这样就会自动编译一个libphp5.so出来了。
‘陆’ 时时教大家搭建PHP环境 怎么用PHP源码安装
[Raykaeso@LAMP httpd-2.4.17]# cd /LAMP
[Raykaeso@LAMP LAMP]# yum -y install libxml2-devel #不装这个编译不了PHP5.6
[Raykaeso@LAMP LAMP]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel png jpeg gd #开启GD库需要的依赖
[Raykaeso@LAMP LAMP]# yum -y install curl curl-devel #开启CURL库需要的依赖
[Raykaeso@LAMP LAMP]# tar -zvxf php-5.6.16.tar.gz
[Raykaeso@LAMP LAMP]# cd php-5.6.16
[Raykaeso@LAMP php-5.6.16]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --enable-sysvsem --enable-sockets --enable-pcntl --enable-mbstring --enable-mysqlnd --enable-pdo --with-pdo-mysql --enable-opcache --enable-shmop --enable-zip --enable-ftp --enable-gd-native-ttf --enable-wddx --enable-soap --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --with-mysqli
[Raykaeso@LAMP php-5.6.16]# make && make install
拷贝一份正式的php-fpm.conf和php.ini配置文件
[Raykaeso@LAMP php-5.6.16]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[Raykaeso@LAMP php-5.6.16]# cp ./php.ini-proction /usr/local/php/etc/php.ini
‘柒’ php中extract()中是关联数组,直接$键名就可以输出键值,但索引数组怎么写,$0写法是错的
共同学习一下,
1、你用的extract()方法中,必须使用关联数组,除了:EXTR_PREFIX_INVALID 或EXTR_PREFIX_ALL
2、代码:
<?php
$size = "large";
$var_array = array("blue","medium","sphere");
extract($var_array, EXTR_PREFIX_INVALID, "wddx");
//EXTR_PREFIX_INVALID可以换成EXTR_PREFIX_ALL
echo "$wddx_0, $size, $wddx_2, $wddx_1\n";
?>
3、(1)注意:用索引数组,前面要有前缀的,并且其中的一个参数要为EXTR_PREFIX_INVALID 或EXTR_PREFIX_ALL
(2)建议:尽量用关联数组,用索引多了你会自己搞乱掉的
4、版权所有,翻版必究。代码经试验过。记得好评哦
‘捌’ 到底什么是PHP序列化
在PHP中,序列化用于存储或传递 PHP 的值的过程中,同时不丢失其类型和结构。本文讲述PHP序列化的四种方案,感兴趣的可以了解一下
序列化是将变量转换为可保存或传输的字符串的过程;反序列化就是在适当的时候把这个字符串再转化成原来的变量使用。这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性。
1、什么是PHP序列化——serialize和unserialize函数
这两个是序列化和反序列化PHP中数据的常用函数。
$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');
//序列化数组$s = serialize($a);echo $s;//输出结果:a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";}
echo ''
;
//反序列化$o = unserialize($s);
print_r($o);
当数组值包含如双引号、单引号或冒号等字符时,它们被反序列化后,可能会出现问题。为了克服这个问题,一个巧妙的技巧是使用base64_encode和base64_decode。
$obj = array();//序列化$s = base64_encode(serialize($obj)); //反序列化$original = unserialize(base64_decode($s));
但是base64编码将增加字符串的长度。为了克服这个问题,可以和gzcompress一起使用。
//定义一个用来序列化对象的函数
function my_serialize( $obj ) { return base64_encode(gzcompress(serialize($obj))); }
//反序列化function my_unserialize($txt) { return unserialize(gzuncompress(base64_decode($txt))); }
2、什么是PHP序列化——json_encode 和 json_decode
使用JSON格式序列化和反序列化是一个不错的选择:
使用json_encode和json_decode格式输出要serialize和unserialize格式快得多。
JSON格式是可读的。
JSON格式比serialize返回数据结果小。
JSON格式是开放的、可移植的。其他语言也可以使用它。
$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');
//序列化数组$s = json_encode($a);echo $s;//输出结果:{"a":"Apple","b":"banana","c":"Coconut"}
echo '
;
//反序列化$o = json_decode($s);
在上面的例子中,json_encode输出长度比上个例子中serialize输出长度显然要短。[page]
3、什么是PHP序列化——var_export 和 eval
var_export 函数把变量作为一个字符串输出;eval把字符串当成PHP代码来执行,反序列化得到最初变量的内容。
$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');
//序列化数组$s = var_export($a , true);echo $s;//输出结果: array ( 'a' => 'Apple', 'b' => 'banana', 'c' => 'Coconut', )
echo '
';
//反序列化eval('$my_var=' . $s . ';');
print_r($my_var);
4、什么是PHP序列化——wddx_serialize_value 和 wddx deserialize
wddx_serialize_value函数可以序列化数组变量,并以XML字符串形式输出。
$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');
//序列化数组$s = wddx_serialize_value($a);echo $s;
//输出结果(查看输出字符串的源码): ApplebananaCoconut
echo '
';
//反序列化$o = wddx_deserialize($s);
print_r($o);//输出结果:Array ( [a] => Apple [b] => banana 1 => Coconut )
可以看出,XML标签字符较多,导致这种格式的序列化还是占了很多空间。
结论
上述所有的函数在序列化数组变量时都能正常执行,但运用到对象就不同了。例如json_encode序列化对象就会失败。反序列化对象时,unserialize和eval将有不同的效果。
本篇《什么是PHP序列化?这个知识点才是你应该了解到的用》到这里就已经结束了,小编一直认为,某一个编程软件受欢迎是有一定原因的,首先吸引人的一定是其功能,环球网校的小编祝您PHP学习之路顺利,如果你还想知道更多php知识,也可以点击本站的其他文章进行学习。