批改状态:未批改
老师批语:
<?php
header("content-type:text/html;charset=utf-8;");
echo '<h3>变量类型的转换</h3>';
//标量:单值变量。栈:先进后出。 队列:先进先出。
//符合类型:多值变量,数组array 对象object;
$age=26;//整型 integer
$salary='8988.56';//浮点型 float
$name='胡峰强';//字符串 string
$isMarried=true;//布尔型 boolean
echo $name.'的年龄是:'.$age.',工资是:'.$salary.',是否已婚;'.$isMarried;
echo '<hr>';
//数组
//创建数组
$books=['php','mysql','css','javascript'];
echo'<pre>';
print_r($books);
echo '<hr>';
//对象
$student=new stdClass();//object
$student->name='胡峰强';
$student->course='php';
$student->grade=95;
var_dump($student);
echo $student->name ,'<br>';
var_dump($student->name);
//print_r()后面可以跟第二个参数true,如果跟了true那么这变量就不会在页面上打印,但是可以给另一个变量赋值。var_export()用法想同
$studentGrade=print_r($student->grade,true);
echo '<div style="width: 100px; height:100px; text-align:center; line-height: 100px; background:lightcoral;">',$studentGrade,'</div>';
echo'<hr>';
//资源resource
$file=fopen('test.txt','r') or die('文件打开失败');
echo fread($file,filesize('test.txt'));
//fclose($file);
echo '<hr>';
//空值null
$sprice=null;
echo 'sprice is',$sprice,'<br>';
//检测是是不是null用is_null()
echo '用三目运算配合is_null()检测是不是null<br>';
echo is_null($sprice) ? '是null' : '不是null';
echo"<hr>";
echo '用if...else配合is_null()检测是不是null<br>';
if(is_null($sprice)){
echo '是null';
}else{
echo '不是null';
}
echo "<hr>";
//变量类型检测gettype()
echo'用gettype(变量)来检测变量类型<br>';
echo gettype($file);
echo "<hr>";
//设置类型 settype()
echo'用settype(变量,类型)括号里面两个变量,一个要设置的变量,一个是要设置的类型<br>';
$num=123.22;
settype($num,integer);
echo $num,'<br>';
echo gettype($num);
//settype($num,string);
//echo gettype($num);点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号