Blogger Information
Blog 16
fans 0
comment 1
visits 14829
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量的类型与检测----2018年8月22日
王大利的博客
Original
715 people have browsed it
<meta charset="UTF-8">
<?php
//变量的类型和检测
echo '<h3>变量的类型和检测</h3>';


//变量的类型有:一个变量名对应一个变量值,这样的变量叫单值变量.: 数值(整数,浮点),字符串,布尔(true/false)


//单值变量演示
$age=30;//integer
$salary=5000.88;//浮点float
$name='zhu zhu';//string字符串
$isMarried=true; //布尔boolean    常见的单值变量
//用逗号分隔符进行连接
echo $name,'的工资是:',$salary,',年龄是:',$age,',是否结婚了:',$isMarried,'<br>';

//复合类型:多值变量:有:数组 array和对象object两类
//数组演示:
$books=['php','mysql','html','css','JavaScript'];//这是一个数组array
echo'<pre>'; //可以将数组格式化输出
print_r($books);//print_r函数是用来输出数组的,不仅可以输出数组还可以输出变量
//对象演示:
$student=new stdClass();//对象object stdClass 是一个标准类
$student->name='猪猪';//->指向符  这是对象的属性
$student->course='php';
$student->grade=90;
var_dump($student);//查看对象用var_dump
var_dump($student->name);//产看对象name属性
echo $student->name;
echo '<br>';
print_r($student->name);
//如果要对输出的字符串添加样式就得用print_r($student->name,true);
echo '<h2 style="color: lawngreen">',  print_r($student->name,true),'</h2>';

//特殊类型:资源
//资源类型演示
$file=fopen('text.txt', 'r') or die('打开失败');
echo fread($file,filesize('text.txt'));
fclose($file);
//,null 空类型演示
$price=null;
echo'$price is',$price,'<br>';

//变量检测
//is_null(),
echo is_null($price) ? '是空' : '不是空';//三元运算符? :
//gettype()
echo  '<br>',gettype($price);
//settype(变量  '设置成什么型变量")
$price=189.22;
settype($price,'int');
echo $price;
//is_null() 再次学习哪几种情况为空
$var1;
$var2=NULL;
$var3='我是中国人';
unset($var3);
echo '<br>';
@var_dump(Is_null($var1) ? '不存在': '存在');
@var_dump(Is_null($var2)  ?  '不存在': '存在');
@var_dump(Is_null($var3)  ? '不存在': '存在');
// empty()
//空字符串,空数组,null,0 '0' false 都是true
$str1='';
$str2=[];
$str3=null;
$str4=0;
$str5='0';
$str6=false;
var_dump( empty($str1)?true:false);
var_dump( empty($str2)?true:false);
var_dump( empty($str3)?true:false);
var_dump( empty($str4)?true:false);
var_dump( empty($str5)?true:false);
var_dump( empty($str6)?true:false);
//,isset()
//变量存在 并且初始化,并且初始化的值不能为null
echo'<br>';
$a=null;
var_dump(isset($a));
$b='zhuzhu';
var_dump(isset($b));
$c;
var_dump(isset($c));


//总结: is_null; empty, isset  检测变量是存在,是否为空 是否定义
//变量有单值变量 数值 字符串 布尔  多值变量:数组 对象  特殊变量:资源 空
//gettype 获取类型  settype 设置变量类型


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!