常量的创建与访问
常量由define
和const
创建
不同处类用const,define报错
define允许使用表达式,const只能用字面量
PHP
<?php
// 常量
//define()
echo 'define创建 <br>';
define('herName', 'LP');
echo herName . '<br>' ;
echo '<hr>';
//const
echo 'const创建 <br>';
const herAd = 'GuangZhou';
echo herAd . '<br>';
echo '<hr>';
// 不同处
echo '不同处类用const,define报错 <br>';
class Test
{
//类用const,define报错
const Test = 'classTest';
}
echo Test::Test, '<br>';
echo '<hr>';
//define允许使用表达式,const只能用字面量
echo 'define允许使用表达式,const只能用字面量 <br>';
$domain = 'Test2';
define('mian_Test', $domain);
echo 'TTTT' . mian_Test . '<br>';
效果图
双引号对变量和特殊字符的处理方式
单引号不能识别变量并输出,双引号可以\
可解析符号
PHP
<?php
//单引号双引号
$name = 'amdin';
// 单引号
echo '单引号 <br>';
echo '$name\t <br>';
echo '<hr>';
// 双引号
echo '双引号 <br>';
echo "$name\t <br>";
echo '<hr>';
// \原样输出
echo '\原样输出 <br>';
echo "\$name\t <br>";
echo '<hr>';
// 应用自身
echo '应用自身 <br>';
echo ' \'T\'<br>';
echo "\"T\"<br>";
echo "\\T\\<br>";
echo '<hr>';
//解析链接一起
echo '解析链接一起 <br>';
echo "T${name}iioi<br>";
索引数组与关联数组的常用操作
<?php
// 索引数组
$usersl[0] = 10;
$usersl[1] = 'admin';
$usersl[2] = 'test';
echo '索引数组加索引: <br>';
$str = print_r($usersl, true);
echo "<pre>" . $str . "</pre>";
echo '<hr>';
//不加索引
echo '索引数组不加索引: <br>';
$users2[] = 10;
$users2[] = 'admin';
$users2[] = 'test';
$users2[] = 'test11';
$str = print_r($users2, true);
echo "<pre>$str</pre>";
echo '<hr>';
//添加元素
echo '索引数组字面量添加元素: <br>';
$users3 = [20, 'shuju', 'yuanu'];
$str = print_r($users3, true);
echo "<pre>$str</pre>";
echo '<hr>';
//查看数组更多信息
echo '索引数组查看数组更多信息: <br>';
var_dump($usersl, $users2, $users3);
echo '<hr>';
//取一个
echo '索引数组取值: <br>';
echo $users2[2], '<br>';
echo '<hr>';
//遍历
echo '索引数组循环遍历: <br>';
foreach ($users3 as $value) {
echo $value, '<br>';
}
echo '<hr>';
//数组元素与变量转换
echo '索引数组元素与变量转换: <br>';
list($age, $name, $email) = $users3;
echo "age=>$age,name=$name,email=>$email <br>";
echo '<hr>';
//将索引转为字符串
echo '索引数组索引转为字符串: <br>';
$str = implode('===', $users3);
echo gettype($str), ':', $str, '<br>';
$sql = "INSERT `user` set `comment` = ${str}";
echo '<hr>';
//将字符串转成索引数组
echo '索引数组字符串转成索引数组: <br>';
$str = 'html,css,js,php';
$arr = explode(',', $str);
echo "<pre>" . print_r($arr, true) . "</pre>";
echo '<hr>';
// 关联数组
// 语义化键名
echo '关联数组语义化键名: <br>';
$products['id'] = 998;
$products['name'] = '电脑';
$products['price'] = 666;
echo "<pre>" . print_r($products, true) . "</pre>";
echo '<hr>';
//字面量创建
echo '关联数组字面量创建: <br>';
$products1 = ['id' => 998, 'name' => '相机', 'peice' => 666];
echo "<pre>" . print_r($products1, true) . "</pre>";
echo '<hr>';
//数组遍历
echo '关联数组遍历: <br>';
foreach ($products1 as $value) {
echo $value, '<br>';
}
// key取键名
echo 'key';
foreach ($products1 as $key => $value) {
echo "[$key]=>$value <br>";
}
echo '<hr>';
// 数组转变量
echo '关联数组数组转变量: <br>';
extract($products1);
echo "id=>$id,name=>$name,peice=>$peice";
echo '<hr>';
// 变量转数组
echo '关联数组变量转数组: <br>';
$lpp1 = 'yiduanwenzi';
$lpp2 = 'yiduanwenzi';
$lpp3 = 'yiduanwenzi';
$test = compact('lpp1', 'lpp2', 'lpp3');
echo "<pre>" . print_r($test, true) . "</pre>";
echo '<hr>';
// 关联数组转索引数组
echo '关联数组转索引数组: <br>';
$index = array_values($products1);
//之前
echo '之前: <br>';
echo "<pre>" . print_r($products1, true) . "</pre>";
//转换
echo '转换: <br>';
echo "<pre>" . print_r($index, true) . "</pre>";
echo '<hr>';
// 多维数组
$users = [
['id' => 1, 'name' => 'name1', 'grade' => 4],
['id' => 2, 'name' => 'name2', 'grade' => 5],
['id' => 3, 'name' => 'name3', 'grade' => 6],
];
// echo "<pre>".print_r($users,true)."</pre>";
// 访问
echo '多维数组访问: <br>';
echo $users[1]['name'], '<br>';
echo '<hr>';
//遍历
echo '多维数组遍历<br>';
foreach ($users as $user) {
echo "id=>${user['id']}, name=>${user['name']}, price=>${user['grade']} <br>";
}
效果图
分支控制的所有形式的基本实现
PHP
<?php
//分支
// 单分支
echo "单分支";
$count = 100;
$price = 500;
$total = 0;
$discount = 1;
if ($count >= 100 && $count < 200) {
$discount = 0.6;
}
$total = $count * $price * $discount;
echo "数量: $count, 总金额: $total, 折扣: $discount <hr>";
//双分支
echo "双分支";
$count = 199;
// $count = 12;
if ($count >= 188 && $count < 200) {
$discount = 0.6;
} else {
$discount = 1;
}
$total = $count * $price * $discount;
echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
//多分支
echo "多分支";
$count = 50;
$count = 105;
$count = 251;
$count = 357;
if ($count >= 100 && $count < 200) {
$discount = 0.7;
} elseif ($count >= 250 && $count < 300) {
$discount = 0.6;
} elseif ($count >= 300) {
$discount = 0.5;
} else {
$discount = 1;
}
$total = $count * $price * $discount;
echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
//switch 简化
echo "switch 简化";
$count = 18;
$count = 168;
$count = 248;
$count = 368;
switch (true) {
case $count >= 100 && $count < 200:
$discount = 0.7;
break;
case $count >= 200 && $count < 300:
$discount = 0.6;
break;
case $count >= 300:
$discount = 0.5;
break;
default:
$discount = 1;
}
$total = $count * $price * $discount;
echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
//switch单值判断
echo "switch 单值判断";
$count = 231;
$discount = 0.4;
$discount = 0.3;
switch ($discount) {
case 0.6:
$total = $count * $price * 0.6;
break;
case 0.5:
$total = $count * $price * 0.5;
break;
case 0.4:
$total = $count * $price * 0.4;
break;
case 0.3:
$total = $count * $price * 0.3;
break;
default:
exit('不支持');
}
echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";