$a = 1
:變量bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
:常量php
echo
:用於輸出字符串 print
:用與輸出 echo 輸出的速度比print快,echo沒有返回值,print有返回值<?php $txt1="學習 PHP"; $txt2="RUNOOB.COM"; $cars=array("Volvo","BMW","Toyota"); echo $txt1; echo "<br>"; echo "在 $txt2 學習 PHP "; echo "<br>"; echo "我車的品牌是 $cars[1]"; ?>
說明:
1. 官方案例中使用了{$cars[1]}
,它與$cars[1]
等價。html
$array[0] =
這樣的方式能夠聲明 > 2. array();
用與聲明一個數組 > 3. 數組的嵌套:也就是array()的嵌套。 > 4. 數組的長度count($array)
函數獲取 > 5. map 的對象 $array['nao']=
> 6.<?php // 二維數組: $cars = array ( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100) ); ?>
同時數據能夠表示mapjava
<?php $sites = array ( "runoob"=>array ( "菜鳥教程", "http://www.runoob.com" ), "google"=>array ( "Google 搜索", "http://www.google.com" ), "taobao"=>array ( "淘寶", "http://www.taobao.com" ) ); print("<pre>"); // 格式化輸出數組 print_r($sites.); print("</pre>"); ?>
如何獲取?
能夠經過$sites[taobao][0]
來獲取map中的數據node
與
java
同樣mysql
while
while (條件) { 要執行的代碼; }
do...while
do { 要執行的代碼; } while (條件);
function functionName() { 要執行的代碼; }
說明:參數能夠設置默認值web
function fn($a = 1,$b = 2){ echo $a; echo '<br>'; echo $b; }
__LINKE__
當前行數__FILE__
__DIR__
__FUNCTION__
__CLASS__
__TRAIT__
:真正使用時在對應的重寫的方法前加上trait
如<?php class Base { public function sayHello() { echo 'Hello '; } } trait SayWorld { public function sayHello() { parent::sayHello(); echo 'World!'; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(); $o->sayHello(); ?> //結果:hello world
__METHOD__
__NAMESPACE__
:命名空間,namespace MyProject<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
redis
preg_match()
sql
test_input()
- $_GET()與$_POST
對象數據庫
string date ( string $format [, int $timestamp ] )
json
fopen()
案例:
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
fclose($file);
if (feof($file)) echo "文件結尾";
echo fgets($file). "<br>";
echo fgetc($file);
$_FILES
:表示上傳文件的對象
\(_FILES["file"]["name"] - 上傳文件的名稱
\)_FILES["file"]["type"] - 上傳文件的類型
\(_FILES["file"]["size"] - 上傳文件的大小,以字節計
\)_FILES["file"]["tmp_name"] - 存儲在服務器的文件的臨時副本的名稱
$_FILES["file"]["error"] - 由文件上傳致使的錯誤代碼
限制
setcookie(name, value, expire, path, domain);
說明:
1. name:key
2. value:值
3. expire :有效期
4. path:
5. domain : 主域
6. 獲取cookie的值: $_COOKIE[]
session_start()
$_SESSION[]
unset()
isset()
session_destroy();
mail(to,subject,message,headers,parameters)
- to :接受者
- subject :主題
- message :消息
- headers :標題
- paramters :額外的參數
<?php $to = "someone@example.com"; // 郵件接收者 $subject = "參數郵件"; // 郵件標題 $message = "Hello! 這是郵件的內容。"; // 郵件正文 $from = "someonelse@example.com"; // 郵件發送者 $headers = "From:" . $from; // 頭部信息設置 mail($to,$subject,$message,$headers); echo "郵件已發送"; ?>
安全校驗
1. spamcheck()
:email 是否合法
2. filter_var($field, FILTER_SANITIZE_EMAIL);
3. filter_var($field, FILTER_VALIDATE_EMAIL)
json_encode
:
json_decode
:
$xmlDoc = new Document(); $xmlDoc->load('note.xml'); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . " = " . $item->nodeValue . "<br>"; }
$servername = "localhost"; $username = "username"; $password = "password"; $conn = new mysqli($servername, $username, $password); //$conn = mysqli_connect($servername, $username, $password); // 檢測鏈接 if ($conn->connect_error) { die("鏈接失敗: " . $conn->connect_error); } echo "鏈接成功";
$conn->query($sql) === TRUE //mysqli_query($conn, $sql) // mysqli_multi_query($conn, $sql):多條記錄
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 建立鏈接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢測鏈接 if ($conn->connect_error) { die("鏈接失敗: " . $conn->connect_error); } // 預處理及綁定 $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"); $stmt->bind_param("sss", $firstname, $lastname, $email); // 設置參數並執行 $firstname = "John"; $lastname = "Doe"; $email = "john@example.com"; $stmt->execute(); $firstname = "Mary"; $lastname = "Moe"; $email = "mary@example.com"; $stmt->execute(); $firstname = "Julie"; $lastname = "Dooley"; $email = "julie@example.com"; $stmt->execute(); echo "新記錄插入成功"; $stmt->close(); $conn->close();
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // 設置 PDO 錯誤模式爲異常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 預處理 SQL 並綁定參數 $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"); $stmt->bindParam(':firstname', $firstname); $stmt->bindParam(':lastname', $lastname); $stmt->bindParam(':email', $email); // 插入行 $firstname = "John"; $lastname = "Doe"; $email = "john@example.com"; $stmt->execute(); // 插入其餘行 $firstname = "Mary"; $lastname = "Moe"; $email = "mary@example.com"; $stmt->execute(); // 插入其餘行 $firstname = "Julie"; $lastname = "Dooley"; $email = "julie@example.com"; $stmt->execute(); echo "新記錄插入成功"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } $dsn = null;
$result->num_rows
獲取當前的行數while($row = $result->fetch_assoc()) { echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"]; } } else { echo "0 個結果"; }
到此基礎部分已經結束,可是呢,咱們的工做纔剛剛開始,那麼學完以後,咱們須要作的事情是什麼呢?
- get,post 請求後臺返回一個頁面,獲取參數
- 頁面的文件上傳功能
- 文件下載功能的實現
- 返回一個驗證碼功能
- 返回一個json 數據
- 設置session,設置cookie, 與redis 整合。
- 使用webservice
- 預約義的變量