PHP_Day01_数据类型_定义变量&类型转换

<?php
	// 定义一些变量
	$name = 'Peter';
	$age = 28;
	$is_married = true;
	
	// 获取这些变量的类型
	echo 'The type of $name is >>'.gettype($name);
	echo '<hr>';
	echo 'The type of $age is >>'.gettype($age);
	echo '<hr>';
	echo 'The type of $is_married is >>:'.gettype($is_married);
	echo '<hr>';
	
	// 查看变量的值与类型
	echo 'Name : ';
	var_dump($name);
	echo '<hr>';
	echo 'Age : ';
	var_dump($age);
	echo '<hr>';
	echo 'Is_married : ';
	var_dump($is_married);
	echo '<hr>';
	
	// 类型转换一
	// 原变量的类型并没有改变,只是引用了新类型的值
	$str = (string)$age;
	# echo gettype($str);   不建议这样,建议下面的形式
	if(is_string($str)){
		echo 'The type of $str is string';
	} else {
		echo 'The type of $str is not string';
	}
	echo '<hr>';
	
	// 类型转化二
	settype($age, 'string');
	if(is_string($age)){
		echo 'The type of $age is string';
	} else {
		echo 'The type of $age is not string';
	}
?>

猜你喜欢

转载自blog.csdn.net/IronMan240/article/details/83690499
今日推荐