1.php标签的起始
<?php
?>
2.如何打印输出
- 打印输出
$email = ‘admin@php.cn’; - echo 打印,可同时打印多个变量
echo $email, “<br>“; - 把值打印为红色
echo ‘<span style="color:red">‘ . $email . ‘</span>‘;
echo ‘<hr>‘;
- print 打印多个时,中间加.隔开,一般不会用它来打印多个,
- 区别: echo打印没有返回值,print返回值为1
print $email . 100;
echo ‘<hr>‘;
echo print $email;
echo ‘<hr>‘;
echo gettype($email);
echo ‘<hr>‘;
以上打印的变量为字符串string,那么想看到它的值呢,打印完整信息用var_dump()
var_dump($email);
var_dump(123);
echo ‘<hr>‘;var_export()返回的就是原代码
var_export($email);
3.变量区分大小写,函数不区分大小写
$username = ‘admin’;
$userName = ‘peter’;
echo $username;
- 变量区分大小写
echo $username, ‘<br>‘, $userName, ‘<hr>‘; - 函数不区分大小写
echo strtoupper($userName);
echo STRTOUPPER($userName);
- 变量的动态,变量的类型可以随值而变化
$var = 100;
var_dump($var);
echo ‘<hr>‘;
$var = ‘php.cn’;
var_dump($var);
- 变量值传递,每一个是独立的
$price1 = 99;
$price2 = $price1;
$price2 = 199;
echo ‘price1= ‘ . $price1 . ‘<hr>‘; - 引用传递,同步更新了,&是拿变量的地址
$price1 = 99;
$price2 = &$price1;
$price2 = 199;
echo ‘price1= ‘ . $price1 . ‘<hr>‘;
可变变量
$var = ‘email’;变量名可以动态创建var = ‘admin@php.cn’;
echo $email;
- 变量的检测与删除
- 打开所有错误
error_reporting(E_ALL); - 变量检测
$username = ‘老师’;
$username = null; isset()如果变量存在且值不为null 才是true
if (isset($username)) echo $username . ‘<br>‘;
else echo ‘没有定义该变量’ . ‘<hr>‘;unset()删除销毁变量,没有返回值
unset($username);
echo $username;
$var = ‘’;
if (empty($var)) var_dump($var) . ‘<br>‘;
4.数据类型:基本,复合,特殊
- 1 基本,标量,单值
- 1.1 字符串
$name = ‘手机’; - 1.2 数值
$price = 2000; 1.3 布尔
$is5G = false;2 复合,多值
- 2.1 对象
$obj = new stdClass; - 2.2 对象是属性和方法的无序集合
$obj->name = ‘peter zhu’;
$obj->age = ‘35’;
echo $obj->name, ‘<hr>‘; - 2.3 数组
$arr = [‘电脑’, 5000, ‘Dell’]; - true返回值,要有一个接收者str,用<pre></pre>来输出
$str = print_r($arr, true);
echo $str;
echo ‘<pre>‘ . $str . ‘</pre>‘; 3.特殊
$num = null;
var_dump($num);3.1 $f是文件资源类型,(stream) resource类型
$f = fopen(‘demo2.php’, ‘r’);
var_dump($f);
echo gettype($f);
5.变量的类型转换
- 1.自动转换,true是1,null是0,5g只取前面那个数字,如果前面是字母g5,php,则报错
$a = null;
$b = true;
$c = false;
$d = ‘5g’;
$e = ‘php’;
$f = 15;
echo $a + 10, ‘<br>‘;
echo $b + 10, ‘<br>‘;
echo $d + 10, ‘<br>‘;
echo $e + 10, ‘<br>‘;
- . : 字符串连接操作
echo $e . $f, ‘<br>‘;
- 2.强制转换
- 前面加(int)
echo ‘<hr>‘;
echo (int) $e + 10, ‘<br>‘;
echo intval($e) + 20, ‘<br>‘;
echo strval($f) . ‘hello’;
3 临时转换,integer
echo gettype($f);
echo ‘<hr>‘;4 永久转换,string
settype($f, ‘string’);
echo gettype($f);