批改状态:合格
老师批语:
$id='123';
echo (int)$id;
echo gettype((int)$id);
echo (string)$id;
echo gettype((string)$id);
$id='123.456';echo (float)$id;
echo gettype((float)$id);
$id='123.456';
echo ((array)$id)[0];
echo gettype((array)$id);
$id='123.456';
echo ((object)$id)->scalar;
echo gettype((object)$id);
echo 1+'2','<br>';echo (int)'a2','<br>';echo 1+'2a','<br>';echo (int)'2a','<br>';

echo 'php'. 123 .'<br>';
echo 'php'.(string)123;
if(!$a) echo'没有定义';var_dump($a);var_export((bool)null);if(!isset($a)) echo'没有定义';

settype()设置变量类型 gettype()获取变量类型
$price=6666;settype($price,'string');echo $price;echo gettype($price);

function sum($a,$b){if(is_numeric($a) && is_numeric($b))echo $a.'+'.$b.'='.($a+$b).'<br>';else echo'参数类型错误';}sum(1,2);sum('1',2);sum('1','2');sum('php','abc');

function sum1(int $a,int $b){return $a.'+'.$b.'='.($a+$b);}echo sum1('1',2);

变量声明规则
-1.变量必须要以’$’为前缀:$name,
-2.变量严格区分大小写,函数不区分大小写
function hello($name){return $name;}echo HELLO('php').'<br>';echo hello('html').'<br>';$name='php';$Name='html';echo $name.'<br>';echo $Name;

命名规则
-1、驼峰式命名
-2、蛇形 xx_xxx()函数
-3、帕斯卡UserModel 类,大驼峰
-4、常量 全大写
$$url='站点';echo $url.'=>'.$$url.'<br>';echo $url.'=>'.$site.'<br>';

$price2=$price;printf('price=%d,price2=%d <br>',$price,$price2);$price=456;printf('price=%d,price2=%d <br>',$price,$price2);

$price2=& $price;printf('price=%d,price2=%d <br>',$price,$price2);$price=789;printf('price=%d,price2=%d <br>',$price,$price2);

$email='admin@qq.com';printf('name=%s,email=%s',$name,$email);echo '<br>';printf('name=%s,email=%s',$GLOBALS['name'],$GLOBALS['email']);

// var_dump($_SERVER);echo 'ip:'.$_SERVER['REMOTE_ADDR'].'<br>';echo '浏览器:'.$_SERVER['HTTP_USER_AGENT'].'<br>';echo '当前php文件绝对路径='.$_SERVER['SCRIPT_FILENAME'].'<br>';echo '当前php文件相对路径='.$_SERVER['SCRIPT_NAME'].'<br>';echo '当前php文件名称='.$_SERVER['PHP_SELF'].'<br>';echo '查询条件='.$_SERVER['QUERY_STRING'].'<br>';echo 'PATH_INFO='.$_SERVER['PATH_INFO'].'<br>';echo 'URI:'.$_SERVER['REQUEST_URI'].'<br>';echo $_GET['id'];

define('NATION','CHINA');const GENDER='男';printf('国籍:%s,性别:%s',NATION,GENDER);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号