檢測變量
// 獲取變量類型
gettype()
// 是否是某種類型
is_int()
is_array()
// 變量是否存在
isset()
// 變量是否為空
empty()
打印變量
echo()// 字符串墓造、數(shù)字
print_r()// 數(shù)組、對象
var_dump()// 打印變量的類型及其值
類型轉(zhuǎn)換
+// 轉(zhuǎn)number
.// 轉(zhuǎn)字符串
if// 轉(zhuǎn)Boolean
銷毀變量
unset()
函數(shù)
// 函數(shù)就是封裝起來的一段代碼可以隨時調(diào)用
如果有默認(rèn)值參數(shù)泪幌,應(yīng)該寫在最后
<?php
$a = 7;
function fn(&$a) {
return $a = $a - 1;
}
fn($a);
echo $a;// 6
?>
<?php
$a = 7;
function fn() {
global $a;// 告訴去全局找
return $a;
}
echo fn();
?>
<?php
$a = 1;
$b = 2;
print_r($GLOBALS);// 收集頁面中全局變量的全局?jǐn)?shù)組
?>
// 動態(tài)調(diào)用函數(shù)
<?php
function good(){
echo "haha";
}
function bad(){
echo "wuwu";
}
$heart = 'good';
$heart();
?>
時間戳函數(shù)
<?php
// 格林威治事件1970年1月1日00:00:00到當(dāng)前的秒數(shù)
echo time()."<br/>";
echo microtime()."<br/>";// 微秒數(shù)和時間戳
echo microtime(true);// 合一起輸出
?>
// 格式化時間
<?php
echo date('Y/m/d H:i:s');
?>
<?php
// 上一小時的時間
$time = time() - 60 * 60;
echo date('Y/m/d H:i:s', $time);
?>
讀取文件夾
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$path = isset($_GET['dir']) ? $_GET['dir'] : '.';
$fh = opendir($path);
// echo $row = readdir($fh)."<br/>";// 返回文件名
// echo $row = readdir($fh)."<br/>";// 返回文件名
// echo $row = readdir($fh)."<br/>";// 返回文件名
// closedir();
?>
<h1>讀取文件夾</h1>
<table border="1">
<tr>
<td>名稱</td>
<td>操作</td>
</tr>
<?php while ( ($row = readdir($fh)) !== false ) {// 文件名為0時 ?>
<tr>
<td><?php echo $row; ?></td>
<td>
<a href="index.php?dir=<?php echo $path.'/'.$row; ?>">查看</a>
</td>
</tr>
<?php } ?>
<?php closedir(); ?>
</table>
</body>
</html>
數(shù)組
// 索引數(shù)組
// 關(guān)聯(lián)數(shù)組
<?php
$arr = array('name'=>'Aaayang', 'age'=>18);
echo $arr['name'];
?>
// 遍歷數(shù)組
<?php
$arr = array('a', 'b', 'c', 'd');
for($i = 0; $i < count($arr); $i ++) {
echo $arr[$i] . "<br/>";
}
?>
// foreach
<?php
$arr = array('a', 'b', 'c', 'd');
foreach ($arr as $key => $value) {
echo $key . '>>' . $value . "<br/>";
}
?>
// foreach簡寫猜欺,foreach不能單循環(huán)出鍵官硝,通過array_keys可以
<?php
$arr = array('a', 'b', 'c', 'd');
foreach ($arr as $value) {
echo $value . "<br/>";
}
?>
// array_keys
<?php
$arr = array('a', 'b', 'c', 'd');
print_r(array_keys($arr));// 返回數(shù)組中所有的鍵
?>
// 修改數(shù)組
<?php
$arr = array('a'=>3, 'b'=>4, 'c'=>5);
foreach ($arr as $key => $value) {
$arr[$key] = $value * 2;
}
print_r($arr);
?>
static
<?php
function a() {
$a = 5;
$a += 1;
return $a;
}
echo a() . "<br/>";// 6
echo a() . "<br/>";// 6
?>
<?php
function a() {
static $a = 5;
$a += 1;
return $a;
}
echo a() . "<br/>";// 6
echo a() . "<br/>";// 7
?>
// 應(yīng)用
<?php
function openfile($file) {
$fh = fopen($file, 'r');
return $fh;
}
// 打開了3次
print_r(openfile('.'));
print_r(openfile('.'));
print_r(openfile('.'));
function openfile($file) {
static $fh = null;// 記住了上次的$fh
if($fh === null) {
$fh = fopen($file, 'r');
}
return $fh;
}
// 實(shí)際打開了1次
print_r(openfile('.'));
print_r(openfile('.'));
print_r(openfile('.'));
?>
類
<?php
class Comment {
public $username;
public $content;
public function setUsername($username) {
$this->username = $username;
}
public function getUsername() {
return $this->username;
}
}
class CommentList {
const FilePath = "commentList.txt";
public function getCommentList() {
return unserialize(file_get_contents(self::FilePath));// 獲取值的姿勢
}
public function write($commentData) {
$commentList = $this->getCommentList();
}
}
?>
// 訪問私有變量的套路
<?php
class Comment {
private $username;
public $content;
public function set($name, $value) {
$this->$name = $value;
}
public function get($name) {
return $this->$name;
}
}
// 訪問私有屬性的姿勢
$comment = new Comment();
$comment->set('username','Aaayang');
echo $comment->get('username');
?>
// 魔術(shù)方法
<?php
// 魔術(shù)方法自動調(diào)用
class Comment {
private $username;
public $content;
public function __set($name, $value) {
$this->$name = $value;
}
public function __get($name) {
return $this->$name;
}
}
// 有魔術(shù)方法的情況下可以直接訪問私有
$comment = new Comment();
$comment->username = "Aaayang";
echo $comment->username;
?>
// 訪問類常量
<?php
class CommentList {
const FilePath = "commentList.txt";
public function getCommentList() {
return unserialize(file_get_contents(self::FilePath));// 獲取值的姿勢
}
public function write($commentData) {
$commentList = $this->getCommentList();
}
}
// 訪問類常量
$commentList = new CommentList();
echo $commentList::FilePath;
?>
// 靜態(tài)屬性和方法不需要實(shí)例化可以直接調(diào)用
<?php
class Tools {
public static $titleTemp = 'Aaayang';// 靜態(tài)屬性屬于類本身,不需要實(shí)例化就能調(diào)用
public static function parseTitle($title) {// 靜態(tài)方法中不能調(diào)用非靜態(tài)屬性和非靜態(tài)方法
return $title . '-_-'.self::$titleTemp;// 調(diào)用靜態(tài)屬性
}
public function test() {
self::parseTitle('haha');// 非靜態(tài)方法中調(diào)用靜態(tài)方法
}
}
// echo Tools::$titleTemp;// 靜態(tài)屬性和方法不需要實(shí)例化就能調(diào)用
echo Tools::parseTitle("VIP");
?>
<?php
// 構(gòu)造方法:對象被創(chuàng)建時自動調(diào)用的方法,一般做初始化工作時使用
// 析構(gòu)函數(shù):對象在內(nèi)存中被銷毀時自動調(diào)用末盔,不能帶參數(shù)
class Pager {
public $page;// 當(dāng)前頁
public $totalPage;
public $link;
public function __construct($totalPage, $link, $page=1){
$this->page = $page;
$this->totalPage = $totalPage;
$this->link = $link;
}
}
$pager = new Pager(10, 'http://baidu.com', 2);
print_r($pager);
?>
// 構(gòu)造方法
<?php
class BaseClass {
public $user;
public function error() {
echo "404<br/>";
}
public function __construct() {
echo "驗(yàn)證<br/>";
}
}
class SubClass extends BaseClass {
public function __construct() {
parent::__construct();// 執(zhí)行父類的構(gòu)造方法
echo "驗(yàn)證2";
}
public function test() {
$this->user;
$this->error();// 會調(diào)用本身的
}
public function error() {
echo "error";
}
}
$subClass = new SubClass();// 子類沒有構(gòu)造函數(shù)會直接調(diào)用父類的
?>