1、PHP字符串转换
数值转字符串就是按照数值原本的格式转换;字符串转换为数字,转换后的数字是从字符串开始部分的数值型字符串,数值型字符串包括正负值和用科学计数法表示的数字。
- <?php
- $number=intval("5.6abc"); //5
- $number=(float)"+5.6abc"; //5.6
- $number=floatval("-1.2e3f4g5"); //-1.2e3
- $result="12.3xy45"-6; //6.3
- $result="xy1234"/5; //0
- $result="1.2.3.4"*5; //6
- $result=1+"-1.3e3"; //-1299
- ?>
•布尔类型转字符串,false转空字符串"",true转"1"
•数组转字符串,是"Array"
•对象转字符串,是"Object"
•资源转字符串,是"Resource id#1"
2、PHP布尔型
以下类型转换为布尔类型的false,其他的转换为true
0转换为false,0.0转换为false,空白字符""和字符串"0"转换为false,没有成员的数组array()转换为false,NULL转换为false
3、PHP数字(整形和浮点型之间可以相互转化)
•整形转换为浮点型:由于浮点型的精度范围远大于整形,所以转换后的数值精度不会改变。
•浮点型转换为整形:讲自动舍弃小树部分,只保留整数部分。注:如果一个浮点型超过整数取值的有效范围,其结果将是无法确定的(整数的最大值约是2.147e9)。
- <?php
- $real_num=3.1e9;
- echo $real_num;
- echo (int)$real_num; //输出一个不确定的值,因为溢出的部分已经丢失
- ?>
4、PHP数组
布尔、数字和字符串转换为数组,将得到一个包含一个该类型数据元素的数组
•NULL转换为数组,会得到一个空数组
•对象转换为一个数组,所得到的数组键名为对象属性名,值为对应对象属性的值
数据类型之间相互转换
一种是强制转换
setType(变量,类型); //int,integer,float,double等等。
这个函数将原变量的类型改变,用var_dump();可以查看变量在赋值前使用(类型)的形式,不会改变原变量的类型
$a=(int)"123abc";
$变量=intval(变量或值);
$变量=floatval(变量或值);
$变量=stringval(变量或值);
注意:整型在内存中占4个字节 2.147e9
浮点型在内存中占8个字节
一种是自动转换(最常用方式),变量会更具运行环境自动转换,与变量和类型有关的一些常用函数:
isset();//判断一个变量是否存在,值如果是null也表示空。
empty();//判断一个变量是否为空 "",null
unset();//删除一个变量
setType();//设置一个变量类型
getType();//获取一个变量类型 var_dump();获取类型和值
变量类型测试函数
is_bool();//判断是否为布尔型
is_int() is_integer() is_long()//判断是否是整型
is_float(), is_double() is_real()//…
is_array()
is_object()
is_resource()
is_null()
is_scalar()//判断是否是标量
is_numberic()//判断是否是任何一种数字,或数字字符串
is_callable()//判断是否是有效的函数名
常量的声明与使用
1.常量是一个简单值的标识符
2.常量定义后不能再改变它的值,也不能使用unset()或其他的函数取消
3.常量可以不用理会变量范围的规则而在任何地方都可以定义和访问
4.常量使用define("常量名",值);
5.常量名在声明和使用时都不使用"$"
6.常量名称习惯都使用大写
7.常量的值只能用标量类型(int,float,bool,string)
8.常量一定要在声明时就给值
9.defined("常量");//判断常量是否存在
预定义常量和魔术常量
echo __FILE__;//输出当前文件名称目录_魔术常量
echo CASE_LOWER;//输出固定的值_预定义常量
- //这个函数将原变量的类型改变,用var_dump();可以查看变量
- $str="100.12345abc";
- setType($str,int);
- var_dump($str);
- //在赋值前使用(类型)的形式
- $str="100.12345abc";
- $a=(int)$str;
- var_dump($a);//输出int(100)
- var_dump($str);//输出值不变,"100.12345abc"
- //如果字符串不是以数字开头,则转换成0
- //不同类型运算
- $a=10;
- $b="100abc";
- $c=true;
- $d=12.34;
- $sum=$a+$c;//布尔型自动转换成1,结果为11
- $sum=$a+$b;//结果为110
- $sum=$a+$b+$c;//结果为111
- $sum=$a+$b+$c+$d;//结果为123.34,因为浮点型内存空间大,小内存转大内存。
- //判断是否是数组
- $a=array("one","two",1,3,6,8);
- if(is_array($a)){
- print_r($a);//打印数组
- }else{
- echo $a;
- }
- //定义常量,使用常量
- define("home","this is a home");
- $a=100;
- function demo()
- {
- global $a;//因为$a是全局变量所以要用全局标记调用
- echo $a;
- echo home;//常量可以不用理会范围,直接访问或者定义
- }
- demo();
- //判断常量是否存在
- if(defined("home")
- {
- echo home;
- }
- else
- {
- define("home","this is a home");
- }
波比源码 » php数据类型之间转换代码
order levofloxacin 500mg generic levofloxacin cost
order generic suhagra 50mg sildenafil 50mg pills for men order estrace 1mg online cheap
order ramipril 10mg oral irbesartan azelastine 10ml over the counter
buspar 10mg for sale ditropan 5mg price oxybutynin 5mg tablet
buy terazosin purchase azulfidine generic purchase sulfasalazine online
order olmesartan 20mg without prescription depakote cheap purchase diamox pills
order imdur 20mg online buy lanoxin 2250 mg generic order micardis 20mg pill
zithromax tablet buy prednisolone 40mg without prescription gabapentin pills
cialis 5mg pill tadalafil dosage clomipramine usa
oral chloroquine 250mg baricitinib price baricitinib 2mg over the counter
clozaril 50mg price clozaril uk dexamethasone 0,5 mg canada
omeprazole 10mg over the counter need essay written casino bonus
levitra 10mg oral lyrica 75mg for sale methylprednisolone 4 mg over the counter
buy cialis 20mg pill tadalafil 20mg usa viagra 150mg price
xenical cheap buy acyclovir generic zovirax 800mg oral
allopurinol canada purchase allopurinol without prescription brand zetia 10mg
generic viagra 100mg cost sildenafil 100mg order tadalafil online
buy cefuroxime 500mg generic methocarbamol 500mg us methocarbamol 500mg without prescription
buy desyrel 100mg generic buy aurogra 50mg without prescription order sildenafil 50mg generic
my family essay writing buy stromectol 12mg pills stromectol tablets
deltasone 5mg tablet purchase accutane generic buy amoxicillin sale
online gambling for real money generic ed drugs buy tadalafil 40mg
buy generic azithromycin 500mg buy azithromycin 250mg generic neurontin 600mg without prescription
order deltasone 10mg buy minipress 1mg sale order generic vermox 100mg
biaxin 250mg price catapres price meclizine 25mg sale
purchase tadalafil sale best online casinos that payout free blackjack online
best casino casino real money play slots for real money united states
order nifedipine oral nifedipine 10mg fexofenadine 120mg over the counter
buy doxycycline without prescription doxycycline 100mg uk order cleocin 300mg generic
temovate canada cordarone order online purchase amiodarone online
oral indomethacin 75mg order generic cenforce cenforce 100mg ca
pepcid online buy prograf 1mg ca buy mirtazapine without prescription
doxycycline 100mg brand medrol brand name medrol sale
tricor usa fenofibrate over the counter viagra 50mg brand
I really love to read such an excellent article. Helpful article. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
tadalafil uk cialis professional buy ed pills usa
buy deltasone generic order isotretinoin 10mg sale amoxicillin 250mg tablet
avodart 0.5mg cheap order cephalexin 500mg generic buy orlistat 60mg for sale
order azathioprine 50mg generic naproxen 250mg cost order naproxen pill
warfarin 5mg cheap order allopurinol online buy allopurinol without prescription
canadian rx
viagra online canadian pharmacy
reputable online canadian pharmacy
canadian pharmacy without a prescription
mexican pharmacy cialis
legit canadian online pharmacy
online medication