Article Directory
php base
type of data
Memory & public
common data types
int
float
…
String Functions
The method defined in a string of
common string functions
md5("123") // 计算123的md5值
......还有很多
constant
常量的值被定义后在脚本的任何地方都不可以改变
define("一个常量由英文字母、下划线、和数字组成,但数字不能作为首字母出现。 (常量名不需要加 $ 修饰符)")
define ('hello',"nihao", true);// 默认为false区分大小写,true为不区分大小写
echo hello; // 输出nihao;
var_dump($a) //输出数据的类型
echo "$a" // 直接输出变量
print_r ($a) // 输出变量a,同时还存在返回值
== // 值相等为true
=== // 类型和值都相等为true
Operators
Common operator ± × / **% and or is ...
Note operator precedence
if the judge
if (True){
echo "";
}else{
echo "";
}
if (a<b){
echo "a<b"
}elseif(c<a){
echo "":
}else{
}
switch structure
<?php
switch (n)
{
case label1:
如果 n=label1,此处代码将执行;
break;
case label2:
如果 n=label2,此处代码将执行;
break;
default:
如果 n 既不等于 label1 也不等于 label2,此处代码将执行;
}
?>
while the structure (infinite loop)
while (True){
#code......
}可以用来写死循环
for loop
for ($i=0;$i<=100;$i++){
#code.........
}
function
Calling functions are not case sensitive
function functionname(形式参数){
static $a = 0; // 只始化a这个变量一次,如果没有static这个申明,每调用一次这个函数就重新初始化一次a这个变量
}
function &demo($a,$b){
echo $a;
echo $b;
$c = $a + $b;
return $c;
}// 出现了& 内存共用
$hello = demo(1,2);
$hello = 123;
echo demo();// demo()的值为123,因为内存共用
Array
- Numeric array
$arr = array("a","b","c");
echo $arr[0];
echo count($arr)// 获取数组的长度
for ($a=0;$a<count($arr);$a++){
echo $arr[$a];
echo "<br/";
}
// 数值数组,通过id(0,1,2。。。。)来遍历内容;
// 可以通过for循环来遍历数组内容,注意与关联数组遍历的区别
- Associative array inside the array of content, a key corresponding to a key
$arr = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
或者
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
// 遍历关联数组
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>// 注意区分与,数值数组的区别
- Multi-dimensional array (an array is simply there is also an array)
$arr = array("Peter"=>"35","Ben"=>"37","Joe"=>array("shuaiqi","年级"))// 这就是一个二维数组,如果”Joe” 数组里面还有一个数组就是3维数组。
Timestamp
echo time(); // 输出时间戳
echo date("Y.m.d H.i.s") // 输出年月日小时分钟秒
System Variables
File Operations
Ternary operator
(Expr1)? (Expr2): (expr3)
$username = isset($_GET['user']) ? $_GET['user']:"nobody";
echo $username;
相当于
if ($username = isset($_GET['user']){
$username = $_GET["user"];
}else{
$username = "nobody";
}
GET, POST and REQUEST requests
- GET
<form action="xxxx.php" method="get">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
$name = $_GET['user'];
echo $name;
var_dump($name);
通过url提交参数
列如: http://127.0.0.1/test/index.php?user=hellophp
输出内容:hellophp string(8) "hellophp"
- POST
<form action="xxx.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
$name = $_POST["user"];
echo $name;
var_dump($name);
- REQUEST
预定义的 $_REQUEST 变量包含了 $_GET、$_POST 和 $_COOKIE 的内容。
$_REQUEST 变量可用来收集通过 GET 和 POST 方法发送的表单数据。
echo $_REQUEST['user'];
Personal self Blog:
http://pigdaqiang.top
Jane book Blog:
https://www.jianshu.com/p/d11b7aab15cd