php 1
Thanks Tutorial!
powered by gpt5-mini in VSCode.
注释比较魔怔
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
font-family : "fira code"
}
.t1{
color : #114514
}
</style>
</head>
<body>
<div class="t1">我是 php 的大象</div>
<?php echo "丁飞说"; ?>
<!-- php could cooperate as this with html -->
<!-- now in html env. -->
<?php if(0) {?>
<p> how? </p>
<?php }?>
<?php
$int = 67;
$bool = false;
$string = "string";
$double = 67.67;
/* default
0 false "" 0.0 */
// whatDoesThisMean? it means rules in naming;
$num = array(7,3,3,5,7,7,3,3);
?><br><?php
$a = "ding";
$b="fei";
$c=$a.$b;
echo $c;echo "\n";
$c=2;$d="3";
if($c < $d and $a != $b)echo "eq!";
$f=442;
/* superglobal variables
_GET["name"] // handle data in url
_POST["number1"] // handle data in form
_REQUEST // not commonly used
htmlspecialchars() // prevent xss attack
_SERVER['PHP_SELF'] // the file name of currently executing script
_SERVER['SERVER_NAME'] // the name of the server host
_SERVER['REQUEST_METHOD'] // the request method used to access the page,check if it is POST or GET,prevent form resubmission.
header() // redirect to another page
*/
// switch uses == logi to compare;
// !! use "break;" to prevent ub;
switch ($f) {
case 1:
echo "f is 0\n";
echo "try\n";
break;
case "0":
echo "how\n";
// break;
default:
echo "nothing";
break;
}
// if this doesnt enter any case
// it willnot cause any problems whenever "default" exist or not;
// new things in php match!
// !! it uses === logi
$g=1;// $g=3;
$result = match($g){
1 => "g is 1",
2,4,6,8 => "g is even",
default => "ok well no",
};
/*
if "match" doesnt matched anything
it will throw an error to the web
!! and shut down the whole program below
when "default" doesnt exist;
*/
// slightly different!
// both above dont supp. < = > these
?><br><?php
echo "no!!";
$fruits = ["pineapple","apple","banana"]; //一维数组
$fruitsTwo[] = ["pineapple","apple","banana"]; // 二维数组
echo $fruits[1];
array_push($fruits,"grape"); // 添加元素到结尾
$fruits[] = "grape"; // 也可以这样添加元素到结尾
$fruits[1] = "grape"; // 修改元素
// unset($fruits[1]); // 删除元素
echo "<br>";
count($fruits); // 获取数组长度
for($i=0;$i<count($fruits);$i++){
echo $fruits[$i]; echo " ";
}
array_splice($fruits, 0,1); // 删除元素并重新索引,0表示从第一个元素开始,1表示删除一个元素
echo "<br>";
echo $fruits[1]; // 输出 grape
echo "<br>";
echo "<br>";
for($i=0;$i<count($fruits);$i++){
echo $fruits[$i]; echo " ";
}
/* -- associative array -- */
$dingFei = [
"father" => "dingFei",
"son" => "DXJ",
"wife" => "for sure not apple",
];// associative array
echo "<br>";
echo $dingFei["son"];// like map in cpp
$dingFei["son"] = "not DXJ"; // modify
$dingFei["mother"] = "pineapple"; // add new key-value pair
$dingFei["wife"] = null; // set value to null
echo "<br>";
print_r($fruits); // print the whole array in a human-readable format
echo "<br>";
print_r($dingFei);
count($dingFei); // get the number of elements in the array
sort($dingFei); // turn the associative array into a indexed array and sort it by value
// !! this will lose the key-value relationship
// instead:
// asort($dingFei); sort associative array by value
// ksort($dingFei); sort associative array by key
array_splice($fruits,1,0,"watermelon"); // insert "watermelon" at index 1
array_splice($fruits,2,0,["peach","pear"]); // insert multiple elements at index 2
echo "<br>";
print_r($fruits);
// multidimensional array
$foodNormal = [
["pineapple","apple","banana"],
["carrot","broccoli","spinach"],
["chicken","beef","pork"],
];
$foodAssociative = [
"fruits" => ["pineapple","apple","banana"],
"vegetables" => ["carrot","broccoli","spinach"],
"meats" => ["chicken","beef","pork"],
];
$foodAssociativeAssociative = [
"fruits" => ["bitter" => "lemon", "sweet" => "banana",],
"vegetables" => ["leafy" => "spinach", "root" => "carrot",],
];
echo $foodAssociativeAssociative["fruits"]["sweet"]; // banana
// string functions
$stringExample = "Hello, World!";
strlen($stringExample); // 获取字符串长度,.size() 不行
$stringExample[7]; // 访问字符串中的字符,类似数组访问 -> 'W'
strpos($stringExample, "World"); // 查找子字符(串)的位置,第一个字符位置 -> 7
$replaceString = str_replace("World", "PHP", $stringExample); // 替换子字符(串) -> "Hello, PHP!",
//!!不改变原字符串,需要接受返回值,函数大多都是这样
echo "<br>";
echo $stringExample;
echo "<br>";
echo $replaceString;
$loweredString = strtolower($stringExample); // 转为小写 -> "hello, world!"
$upperedString = strtoupper($stringExample); // 转为大写 -> "HELLO, WORLD!"
$stringExample2 = "114514";
$intFromString = intval($stringExample2); // 字符串转整数 -> 114514
// "Hello, World!"
// 负数语义:倒数第x个字符
$subString = substr($stringExample, 7, 5); // 获取子字符串 -> "World",从索引7开始,长度为5
$subString2 = substr($stringExample, -6, 5); // 从从倒数第6个字符开始,向后5个字符 -> "World",
$subString3 = substr($stringExample, 7, -3); // 从索引7开始,获取到(不包括)倒数第3个字符 -> "Wor"
echo $subString2;
echo "<br>";
echo $subString3;
$errorString = substr($stringExample, 20, 5); //!! 索引超出字符串长度,返回空字符串 -> ""
echo "<br>";
echo $errorString; // 输出空字符串
echo "<br>";
echo "nothing above";
$dividedString = explode(", ", $stringExample); // 将字符串按指定分隔符分割成数组 -> ["Hello", "World!"]
echo "<br>";
print_r($dividedString);
$stringExample2="big banana";
$dividedString2 = explode(" ", $stringExample2); // -> ["big", "", "", "", "", "banana"],多个连续的分隔符会产生空字符串元素
echo "<br>";
print_r($dividedString2);
// mathmatical functions
// 同样不改变原变量,需要接受返回值
$number = 3.14159;
$roundedNumber = round($number, 2); // 四舍五入到小数点后2位 -> 3.14
$flooredNumber = floor($number); // 向下取整 -> 3
$ceiledNumber = ceil($number); // 向上取整 -> 4
$randomNumber = rand(1, 100); // 生成1到100之间的随机整数
$power = pow(2, 3); // 计算2的3次方 -> 8
$squareRoot = sqrt(16); // 计算16的平方根 -> 4
$number2 = -3.14159;
$abs = abs($number2); // 计算绝对值 -> 3.14159
// array functions
$animals = ["cat", "dog", "bird"];
$count = count($animals); // 获取数组长度 -> 3
$isArray = is_array($animals); // 检查变量是否是数组 -> true
array_push($animals, "fish"); // 在数组末尾添加元素 -> ["cat", "dog", "bird", "fish"]
$removedElement = array_pop($animals); // 删除并返回数组末尾的元素 -> "fish" -> ["cat", "dog", "bird"]
echo "<br>";
print_r($animals);
$reversedAnimals = array_reverse($animals); // 反转数组顺序,不改变原数组 -> ["bird", "dog", "cat"]
//$animals = array_reverse($animals); // 如果想改变原数组,可以重新赋值
echo "<br>";
print_r($reversedAnimals);
$animalsWithWater = array_merge($animals, ["fish", "frog"]); // 合并数组,加最后面 -> ["cat", "dog", "bird", "fish", "frog"]
// time and date functions
//UTC 时间和本地时间的转换
date_default_timezone_set("Asia/Shanghai"); // 设置默认时区为北京时间(UTC+8)
$currentTimestamp = time(); // 获取当前时间戳(自1970年1月1日以来的秒数)
$formattedDate = date("Y-m-d H:i:s", $currentTimestamp); // 格式化时间戳为可读日期时间字符串
$formattedDateNow = date("Y-m-d H:i:s"); // 如果不传入时间戳,默认使用当前时间
// Y 4位年 m 2位月 d 2位日 H 24小时制小时 i 分钟 s 秒,其它字符保持不变
echo "<br>";
echo $formattedDateNow;
$time1 = "2024-01-01 12:00:00";
$stringTimestamp = strtotime($time1); // 将日期时间字符串转换为时间戳
echo "<br>";
echo $stringTimestamp;
?>
</body>
</html>

浙公网安备 33010602011771号