Blogger Information
Blog 87
fans 0
comment 0
visits 59358
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第二章:变量类型检测与转换
黄忠倚的博客
Original
579 people have browsed it

实例

<?php
echo '<h2>2.变量类型与转换</h2>';
echo '<hr color="green">';

//1.标量:单值变量,包括整形,浮点数,字符串,布尔型等。
$nianling = 30;  //integer/ int
$gongzi = 3000;  //float
$mingzi = 'Kevin Wong'; //string
$yihun = true;  //false 空

//标量的输出:echo, print,var_dump()
echo $mingzi.'的年龄是:'.$nianling.',工资:'.$gongzi.',是否已婚:'.$yihun;
echo '<hr>';
print $mingzi;
print '<br>';
var_dump($mingzi);

echo '<hr color="red>';
//2.复合类型:变量元素,多值变量,数组和对象$books = ['php', 'mysql', 'html', 'css', 'javasript'];
echo '<pre>';
var_dump($books);

$student = new stdclass();
$student->name ='王小三';
$student->courese = 'PHP';
$student->grade = 80;

echo '<hr>';
print_r($student);
var_dump($student);

$file = fopen('demo1.php', 'r') or die('打开失败');
echo fread($file, filesize('demo1.php'));
fclose($file);

$num = null;
// echo is_null($num));
var_dump(is_null($num));

/医院
 * 变量类型检测,设置
 * 1.类型查询:gettype($var)
 * 2.类型检测:is_integer(), is_float(), is_sting()
 * 	is_bool(), is_arry(), is_object(), is_resource(),
 * 	is_null(),is_numeric()
 * 
 * 3.类型转换:
 * 		1.强制转换:(int)$var,(string)$var
 * 		2.临时转换(仅转换值,类型不变):intval()floatval(),strval()
 * 		3.永久转换:settype($var,类型标识符)
 */

$price = 186.79;
echo gettype($price);  //double,就是float的别名
echo '<hr>';
echo (int)$price;
echo '<hr>';
echo gettype($price);
echo '<hr>';
settype($price, 'integer');
echo $price;
echo '<hr>';
echo gettype($price);
echo '<hr>';
var_dump(is_integer($price));
//is_numeric():判断一个变量是不是数字或数字型的字符串
$num = 100;   //数字
$sum = '100';
$name = 'Kevin_Wong';
echo '<hr>';
var_dump(is_numeric($num));
echo '<hr>';
var_dump(is_numeric($sum));

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
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