程式語言對比手冊(橫向版)[-PHP-]
php-執行在服務端的跨平臺免費面向物件的指令碼語言

一、環境的搭建
1.WAMPServer整合環境下載和安裝
Windows Apache MySQL PHP
,官網: http://www.wampserver.com/ (首頁挺狂放)

官網下載.png
2.安裝
安裝軟體可以隨便在哪裡下,官網挺慢的。安裝next,也沒什麼好說的

安裝.png
3.執行
訪問localhost,出現介面,表示服務端已經通了。(雖然目前什麼都沒做)

執行.png
4.小面板
也就是便捷操作的面板

小面板.png
比如進入MySQL的控制檯(WAMPServer會自動幫我們裝一個MySQL,預設無密碼)

mysql控制檯.png
5.網站訪問
現在將一個以前的靜態站點放在專案下的poem資料夾下

站點的程式碼位置.png
然後訪問: http://localhost/poem/ ,不出所料,正常訪問。
注意:
到現在為止都是Apache的功勞,和PHP還沒太大的關係。

站點.png
6.多站點的支援
這裡在: J:\PHP\webset
資料夾下放一個站點的檔案,只需要簡單的修改以下配置,再重啟即可

image
---->[D:\M\WAMP\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf]------------------------ <VirtualHost *:80> ServerName toly1994328.com #站點名 DocumentRoot J:/PHP/webset #站點原始碼資料夾 <Directory"J:/PHP/webset"> Options Indexes FollowSymLinks #站點許可權相關 AllowOverride all Require all granted </Directory> </VirtualHost> ---->[C:\Windows\System32\drivers\etc\hosts]------------------------ 127.0.0.1toly1994328.com ---->[D:\M\WAMP\wamp64\bin\apache\apache2.4.23\conf\httpd.conf]------------------------ # Virtual hosts Include conf/extra/httpd-vhosts.conf #這句話如果封住要解封(我這預設就開的)

新增站點.png
7.修改埠號
詳見圖片上的url中的埠號
---->[D:\M\WAMP\wamp64\bin\apache\apache2.4.23\conf\httpd.conf]------------------------ Listen 0.0.0.0:8081 Listen [::0]:8081 ServerName localhost:8081 |-- 多站點的話也要修改 httpd-vhosts 裡的埠號 ---->[D:\M\WAMP\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf]------------------------ <VirtualHost *:8081> ServerName localhost ... <VirtualHost *:8081> ServerName toly1994328.com #站點名 ... </VirtualHost>

image

image
二、php語法初識
1.什麼都不說,來個 Hello World
先 : echo 輸出

helloworld.png
感覺挺爽的嘛,SpringBoot的HelloWorld比這麻煩多了
---->[Hello.php]--------------------------- <?php echo "Hello World!";
2.變數 $ 名稱 = 變數

image
---->[vartest.php]--------------------------- <?php $name="張風捷特烈";//生成變數 echo $name;//輸出變數 echo "<br />";//輸出換行 $name="toly";//改變變數 echo $name;//輸出變數
3.幾種資料組織形式
感覺比JavaScript還要奔放,比Python要收斂一丟丟。就是$符號太多...晃眼
<?php $t = true;//1.布林型 $fa = false;//布林型 $i = 5; //2.整型 $a = 0xff; // 支援十六進位制數 -- 255 $b = 074; // 支援八進位制數 -- 60 $f = 3.14159;//3.浮點型 $f1 = 3.14159e4;//浮點型 31415.9 $f2 = 3.14159e-3;//浮點型 0.00314159 $str = "String Type";//4.字串 $str = 'String Type';//字串 $color = array("red", "green", "blue");//5.陣列 echo $color[0];//獲取元素 $colorMap = array("red"=>"紅色", "green"=>"綠色", "blue"=>"藍色");//6.類似對映或字典 echo $colorMap["red"];//紅色 $shape = new Shape("四維空間");//7.物件 echo $shape->getName(); //呼叫方法 $n=null;//8.null class Shape{ var $name; function __construct($name = "green"){ $this->name = $name; } /** * @return string */ public function getName() { return $this->name; } /** * @param string $name */ public function setName($name){ $this->name = $name; } }
4.看一下請求和響應: Hello.php
|--- 客戶端請求 (此時不太關心) GET http://toly1994328.com/Hello.php HTTP/1.1 Host: toly1994328.com Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9 |--- 服務端響應 HTTP/1.1 200 OK Date: Wed, 13 Mar 2019 14:06:15 GMT Server: Apache/2.4.23 (Win64) PHP/5.6.25 X-Powered-By: PHP/5.6.25 Content-Length: 13 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8<----text/html 表示可以讓瀏覽器解析請求體中的html程式碼 Hello World! <---- 可見echo可以將資料放入請求體裡
5.小測試
既然echo可以輸出,放個靜態頁面輸出會怎麼樣,就來個粒子吧
這說明echo可以將整個html頁面輸出讓瀏覽器解析,這樣的話php的地位應該是控制服務端的輸出流。

image
<?php echo "<!doctype html> <html> <head> <meta charset=\"utf-8\"> <title>液態粒子字型</title> <style> body,html { margin:0; width:100%; overflow:hidden; } canvas { width:100%; } .control { position:absolute; } .control input { border:0; margin:0; padding:15px; outline:none; text-align:center; } .control button { border:0; margin:0; padding:15px; outline:none; background:#333; color:#fff; } .control button:focus,.control button:hover { background:#222 } </style> </head> <body> <div class = \"control\" style = \"position:absolute\"> <input type = \"text\" value = \"張風捷特烈\" id = \"t\"> <button onclick = \"changeText(t.value)\"> change </button> </div> <script> var can = document.createElement(\"canvas\"); document.body.appendChild(can); var ctx = can.getContext('2d'); function resize(){ can.width = window.innerWidth; can.height = window.innerHeight; } const max_radius = 3; const min_radius = 1; const drag = 50; window.onresize = function(){ resize(); }; function cfill(){ ctx.fillStyle = \"#000\"; ctx.fillRect(0,0,can.width,can.height); ctx.fill(); } var mouse = { x:-1000, y:-1000 }; can.onmousemove = function(e){ mouse.x = e.clientX; mouse.y = e.clientY; }; can.ontouchmove = function(e){ mouse.x = e.touches[0].clientX; mouse.y = e.touches[0].clientY; }; resize(); cfill(); function distance(x,y,x1,y1){ return Math.sqrt( ( x1-x ) * ( x1-x ) + ( y1-y ) * ( y1-y ) ); } class Particle{ constructor(pos,target,vel,color,radius){ this.pos = pos; this.target = target; this.vel = vel; this.color = color; this.radius = radius; var arr = [-1,1]; this.direction = arr[~~(Math.random()*2)]*Math.random()/10; } set(type,value){ this[type] = value; } update(){ this.radius += this.direction; this.vel.x = (this.pos.x - this.target.x)/drag; this.vel.y = (this.pos.y - this.target.y)/drag; if(distance(this.pos.x,this.pos.y,mouse.x,mouse.y) < 50){ this.vel.x += this.vel.x - (this.pos.x - mouse.x)/15; this.vel.y += this.vel.y - (this.pos.y - mouse.y)/15; } if(this.radius >= max_radius){ this.direction *= -1; } if(this.radius <= 1){ this.direction *= -1; } this.pos.x -= this.vel.x; this.pos.y -= this.vel.y; } draw(){ ctx.beginPath(); ctx.fillStyle = this.color; ctx.arc(this.pos.x,this.pos.y,this.radius,0,Math.PI*2); ctx.fill(); } } var particles = []; var colors = [\"#bf1337\",\"#f3f1f3\",\"#084c8d\",\"#f2d108\",\"#efd282\"]; var bool = true; var current = 0,i; function changeText(text){ var current = 0,temp,radius,color; cfill(); ctx.fillStyle = \"#fff\"; ctx.font = \"120px Times\"; ctx.fillText(text,can.width*0.5-ctx.measureText(text).width*0.5,can.height*0.5+60); var data = ctx.getImageData(0,0,can.width,can.height).data; cfill(); for(i = 0;i < data.length;i += 8){ temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)}; if(data[i] !== 0 && ~~(Math.random()*5) == 1/*(temp.x % (max_radius+1) === 0 && temp.y % (max_radius+1) === 0)*/){ if(data[i+4] !== 255 || data[i-4] !== 255 || data[i+can.width*4] !== 255 || data[i-can.width*4] !== 255){ if(current < particles.length){ particles[current].set(\"target\",temp); }else{ radius = max_radius-Math.random()*min_radius; temp = {x:Math.random()*can.width,y:Math.random()*can.height}; if(bool){ temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)}; } color = colors[~~(Math.random()*colors.length)]; var p = new Particle( temp, {x:(i/4)%can.width,y:~~((i/4)/can.width)},{x:0,y:0}, color, radius); particles.push(p); } ++current; } } } bool = false; particles.splice(current,particles.length-current); } function draw(){ cfill(); for(i = 0;i < particles.length;++i){ particles[i].update(); particles[i].draw(); } } changeText(\"張風捷特烈\"); setInterval(draw,1);</script> </body> </html>"
6、Idea 整合PHP環境
工欲善其事必先利其器,總不能在文字編輯器裡寫程式碼吧,IDE 本人首選Idea
6.1.先裝外掛

Idea安裝php外掛.png
6.2.配置php環境
然後就開心的敲程式碼了,提示什麼的都有

配置idea.png
好了,引入到此為止,下面開始正文
三、PHP中的面向物件
1.類的定義和建構函式+解構函式

構造和析構.png
---->[obj/Shape.php]---------------- <?php namespace toly1994; class Shape{ public function __construct(){ echo "Shape建構函式"; } function __destruct(){ echo "Shape解構函式"; } } ---->[obj/Client.php]---------------- <?php include './Shape.php';//引入檔案 use toly1994\Shape; $shape = new Shape();
2.類的封裝(成員變數,成員方法)
---->[obj/Shape.php]---------------- <?php namespace toly1994; class Shape{ private $name; public function __construct($name){ echo "Shape建構函式<br/>"; $this->name = $name; } public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; } public function draw(){ echo "繪製$this->name <br/>"; } function __destruct(){ echo "Shape解構函式"; } } |-- 使用 -------------------------- $shape = new Shape("Shape"); $shape->draw();//繪製Shape $shape->setName("四維空間<br/>"); echo $shape->getName();//四維空間
3.類的繼承
---->[obj/Point.php]---------------- <?php namespace toly1994; class Point extends Shape{ public $x; public $y; } |-- 使用 -------------------------- $point = new Point("二維點"); $point->draw();//繪製二維點 echo $point->getName();//二維點 $point->x=20;//二維點 echo $point->x;//20
4.類的多型
---->[obj/Circle.php]---------------- <?php namespace toly1994; class Circle extends Shape{ private $radius; public function getRadius(){ return $this->radius; } public function setRadius($radius){ $this->radius = $radius; } public function draw(){ echo "Draw in Circle<br/>"; } } ---->[obj/Point.php]---------------- <?php namespace toly1994; class Point extends Shape{ public $x; public $y; public function draw(){ echo "Draw in Point<br/>"; } } |-- 使用 -------------------------- $point=new Point(""); doDraw($point);//Draw in Point $circle=new Circle(""); doDraw($circle);//Draw in Circle function doDraw(Shape $shape){ $shape->draw(); }
5.介面及抽象類
抽象類---->[obj/Shape.php]---------------- abstract class Shape{ ... //抽象方法 abstract protected function draw(); } 介面---->[obj/Drawable.php]---------------- <?php namespace toly1994; interface Drawable{ public function draw(); } |-- 實現介面 implements 關鍵字----------- include './Drawable.php';//引入檔案 abstract class Shape implements Drawable
四、PHP中的函式:
1.PHP中函式的定義
在Circle類中,定義一個函式計算圓的面積: getArea

PHP函式定義的形式.png
---->[obj/Circle.php]---------------- <?php namespace toly1994; define("PI", 3.141592654);//定義常量 class Circle extends Shape{ private $radius; public function __construct($radius) { $this->radius = $radius; } ... /** * 獲取圓的面積 * @return float|int */ public function getArea(){ $area = PI * $this->radius * $this->radius; return $area; } } |-- 物件使用函式(方法) $circle = new Circle(10); echo $circle->getArea();//314.15926541 $circle->setRadius(20); echo $circle->getArea();//256.6370616
函式名呼叫時竟然不區分大小寫,方法也不支援過載,真是神奇的語言...
2.類的靜態方法以及引數
建立一個工具類來換行

換行.png
---->[utils/Utils.php]-------------------- <?php class Utils{ /** *換行工具方法 * @param int $num 行數 * @param bool $line true <hr>false <br> */ public static function line($num = 1, $line = true) { for ($i = 0; $i < $num; $i++) {//for迴圈控制 if ($line) {//條件控制 echo "<hr>"; } else { echo "<br>"; } } } } ---->[base/Funtest.php]------呼叫-------------- <?php include '../utils/Utils.php'; Utils::line();//預設一行 有線 Utils::line(2,false);//兩行 無線 Utils::line(5);//五行 無線
3.二維陣列的使用來建立table

建立表格.png
/** 建立表格 * @param $content 二維陣列 * @param string $css 表格樣式 * @return string */ public static function createTable($content, $css = "border='1' cellspacing='0' cellpadding='0' width='80%'") { $row = count($content); $col = count($content[0]); $table = "<table $css>"; for ($i = 0; $i < $row; $i++) {//for迴圈控制 $table .= "<tr/>"; for ($j = 0; $j < $col; $j++) { $value = $content[$i][$j]; $table .= "<td>$value</td>"; } $table .= "</tr>"; } $table .= "</table>"; return $table; } |-- 使用--------------------------- <?php include '../utils/Utils.php'; $content = [ ["姓名", "年齡", "性別", "武器", "職業"], ["捷特", 24, "男", "黑風劍", "戰士"], ["龍少", 23, "男", "控尊戒", "鑄士"], ["巫纓", 23, "女", "百里弓", "弓士"], ]; echo Utils::createTable($content); Utils::line(); $css = "border='5' cellspacing='0' cellpadding='0' width='80%' bgcolor=#81D4F5"; echo Utils::createTable($content, $css);//自定義表格樣式
4.變數作用域
4.1 區域性變數
區域性變數不能在塊的外面被呼叫
|--- 方法中[區域性變數-動態變數]在函式呼叫完成會釋放------------------ function area1(){ static $inArea = true; if ($inArea) { echo "true<br/>"; } else { echo "false<br/>"; } $inArea = !$inArea; } area1();//true area1();//true area1();//true |--- 方法中[區域性變數-靜態變數]在函式呼叫完成不會釋放,在靜態記憶體區---------------- function area1(){ static $inArea = true; if ($inArea) { echo "true<br/>"; } else { echo "false<br/>"; } $inArea = !$inArea; } area1();//true area1();//false area1();//true
4.2 全域性變數
|-- 方法體中不能使用外面定義的變數 $name = "toly"; function say(){ echo "My name is $name" ;//Undefined variable: name } say(); |-- 解決方,1,在方法體中使用global關鍵字對變數進行修飾 $name = "toly"; function say(){ global $name; echo "My name is $name"; } say(); |-- 解決方法2,使用$GLOBALS鍵值對獲取全域性變數 $name = "toly"; function say(){ echo "My name is " . $GLOBALS['name']; } say();
5.傳值與傳引用
|-- 傳入值,並不會導致原值汙染 function add($target){ $target = $target + 1; return $target; } $num = 10; $res = add($num); echo $res;//11 echo $num;//10 |-- 傳引用,方法中對入參的修改會修改原值 function add(&$target)//取地址{ $target = $target + 1; return $target; } $num = 10; $res = add($num); echo $res;//11 echo $num;//11
6.其他特點
|-- 可變函式(函式的變數化)---------------- function add($x, $y){ return $x + $y; } $fun = "add";//函式變數化 echo $fun(3, 4);//7 |-- 函式作為入參(俗稱回撥) ---------------- function add($x, $y, $fun){ return $fun($x) + $fun($y); } echo add(3, 4, function ($i) {//此處通過匿名函式 return $i * $i; });//25 |-- //此處通過create_function建立匿名函式(php7.2後過時) echo add(3, 4, create_function('$i', 'return $i * $i;'));//25 |-- 通過 call_user_func 來呼叫函式--------感覺挺詭異 call_user_func("md5", "張風捷特烈");//20ce3e8f34f3a3dd732a150a36d41512 |-- 通過 call_user_func_array 來呼叫函式--------第二參是引數的陣列 function add($x, $y){ return $x + $y; } echo call_user_func_array("add", array(3,4));//7
五、PHP中對檔案的操作
1.建立檔案(含遞迴資料夾)

建立檔案.png
$path = 'G:/Out/language/php/2/3/45/dragon.txt'; createFile($path); function createFile($path){ $dir = dirname($path);//獲取父檔案 if (!file_exists($dir)) { mkdirs($dir); createFile($path); } else { fopen($path, "w"); } } function mkdirs($dir){ return is_dir($dir) or mkdirs(dirname($dir)) and mkdir($dir, 0777); }
2.寫入字元檔案
檔案不存在,則建立檔案再寫入

寫入檔案.png
/** * 將內容寫入檔案 * @param $path 路徑 * @param $content 內容 */ function writeStr($path, $content){ if (file_exists($path)) { if ($fp = fopen($path, 'w')) { fwrite($fp, $content); fclose($fp); echo "<br>寫入成功 " . $content; } else { echo "<br>建立失敗 "; } } else { createFile($path); writeStr($path, $content); } }
3.讀取檔案

讀取檔案.png
|-- 內建函式 readfile 讀取檔案 echo readFile($path); |-- fgetc 逐字讀取 function readFileByPath($path){ $result = ""; $file = fopen($path, "r") or $result . "無法開啟檔案!"; while (!feof($file)) { $result .= fgetc($file); } fclose($file); return $result; } |-- fgets 逐行讀取 function readFileByLine($path){ $result = ""; $file = fopen($path, "r") or $result . "無法開啟檔案!"; while (!feof($file)) { $result .= fgets($file)."<br/>"; } fclose($file); return $result; } |-- 以行劃分將檔案讀入陣列 var_dump(file($path)); array(3) { [0]=> string(26) "應龍----張風捷特烈 " [1]=> string(49) "一遊小池兩歲月,洗卻凡世幾閒塵。 " [2]=> string(48) "時逢雷霆風會雨,應乘扶搖化入雲。" }
4.檔案資訊
還有很多亂七八糟的方法...用的時候再找吧,感覺和Python挺像的

資訊.png
$path = 'G:/Out/language/php/2/3/45/dragon.txt'; $stat = stat($path); echo "建立時間:" . date("Y-m-d H:i", $stat["ctime"]);//2019-03-14 04:45 echo "修改時間:" . date("Y-m-d H:i", $stat["mtime"]);//2019-03-14 04:54 echo "檔案大小:" . $stat["size"] . " 位元組"; echo "檔案模式:" . $stat["mode"]; echo "檔名:" . basename($path); echo "父資料夾:" . dirname($path); echo "是否是資料夾:" . (is_dir($path) ? "true" : "false"); echo "是否是檔案:" . (is_file($path) ? "true" : "false"); echo "是否存在:" . (file_exists($path) ? "true" : "false"); echo "檔案所在磁碟可用大小:" . disk_free_space(dirname($path)) . " 位元組"; echo "檔案所在磁碟總大小:" . disk_total_space(dirname($path)) . " 位元組"; echo "檔案型別:" . filetype($path);//file
5.檔案讀寫許可權
基本上和其他語言一樣
r只讀。在檔案的開頭開始。 r+讀/寫。在檔案的開頭開始。 w只寫。開啟並清空檔案的內容;如果檔案不存在,則建立新檔案。 w+讀/寫。開啟並清空檔案的內容;如果檔案不存在,則建立新檔案。 a追加。開啟並向檔案末尾進行寫操作,如果檔案不存在,則建立新檔案。 a+讀/追加。通過向檔案末尾寫內容,來保持檔案內容。 x只寫。建立新檔案。如果檔案已存在,則返回 FALSE 和一個錯誤。 x+讀/寫。建立新檔案。如果檔案已存在,則返回 FALSE 和一個錯誤。
六、PHP和MySQL的結合
還是玩sword表吧

資料庫表.png
1.連線MySQL

連線成功.png
<?php $host = "localhost"; $user = "root"; $pwd = "----"; $conn = mysqli_connect($host, $user, $pwd); // 檢測連線 if ($conn->connect_error) { die("連線失敗: " . $conn->connect_error); } echo "連線成功"; $conn->close();//關閉資料庫
2.查詢資料庫並封裝實體類
連上資料庫然後就是SQL的領域了

查詢資料庫並封裝實體類.png
<?php include './Sword.php'; $host = "localhost"; $user = "root"; $pwd = "----"; $conn = mysqli_connect($host, $user, $pwd); // 檢測連線 if ($conn->connect_error) { die("連線失敗: " . $conn->connect_error); } echo "連線成功"; mysqli_select_db($conn, "zoom");//選擇資料庫 $sql = "SELECT * FROM sword";//sql語句 $result = $conn->query($sql); $swords = array(); if ($result->num_rows > 0) { // 輸出資料 while ($row = $result->fetch_assoc()) { $sword = new Sword( $row["id"], $row["name"], $row["atk"], $row["hit"], $row["crit"], $row["attr_id"], $row["type_id"] ); array_push($swords, $sword); } }
3.將查詢的結果轉化為json

轉化為json.png
echo json_encode($swords);
也可以將結果輸出成表格

列表.png
function createTable($content, $css = "border='1' cellspacing='0' cellpadding='0' width='80%'"){ $row = count($content); $table = "<table $css >"; for ($i = 0; $i < $row; $i++) {//for迴圈控制 $table .= "<tr/>"; $value = $content[$i]; $table .= "<td >$value->id</td>"; $table .= "<td >$value->name</td>"; $table .= "<td >$value->atk</td>"; $table .= "<td >$value->hit</td>"; $table .= "<td >$value->crit</td>"; $table .= "<td >$value->attr_id</td>"; $table .= "<td >$value->type_id</td>"; $table .= "</tr>"; } $table .= "</table>"; return $table; }
4.建立資料庫

資料庫建立成功.png
// 建立資料庫 $sql = "CREATE DATABASE php"; echo $conn->query($sql) ? "資料庫建立成功" : "資料庫建立失敗" . $conn->error;
5.建立表

建立表.png
mysqli_select_db($conn, "php");//選擇資料庫 $sql="create table sword ( idsmallint(5) unsigned auto_increment primary key, namevarchar(32)not null, atksmallint(5) unsignednot null, hitsmallint(5) unsignednot null, critsmallint(5) unsigned default '10' null, attr_id smallint(5) unsignednot null, type_id smallint(5) unsignednot null )"; echo $conn->query($sql) ? "sword建立成功" : "sword建立失敗" . $conn->error;
另外增刪改查的操作關鍵是sql語句,本文就不引申了
七、PHP的字串及正則語法
1.PHP的字串

引號.png
|-- 雙引號 : 可解析變數解析所有轉移符-------------- <?php $name = "toly-張風捷特烈-1994328"; echo "$name"; |-- 單引號: 不可解析變數 只解析\'\\兩個轉義符------------ <?php $name = "toly-張風捷特烈-1994328"; echo '$name'; |-- 字元衝突時用轉義符 : \--------------------- \\\'\$\"\r\n\t\f |-- {}的輔助 --- {}要緊貼變數,不要加空格---------------- echo "{$name}s";//toly-張風捷特烈-1994328s echo "${name}s";//toly-張風捷特烈-1994328s |-- 字元的操作 echo $name[0];//t Utils::line(); echo $name{1};//o Utils::line(); $name[2]='L'; echo $name;//toLy-張風捷特烈-1994328
2.heredoc 和 nowdoc
|-- heredoc作用同雙引號,只是在其中雙引號不用轉義------------ $html = <<<EOF "在"""""s這裡面'''""'原樣輸出" EOF; echo $html; |-- nowdoc作用同單引號,只是在其中雙引號不用轉義------------ $html = <<<'EOD' "在"""""s這裡面'''""'原樣輸出" EOD; echo $html;
3.其他型別轉換為字串
數字: 原樣 布林: ture 1false '' null:'' 陣列: Array $num=1; $res = (string)$num;//型別轉換 strval($num);//型別轉換 settype($num,'string')//$num本身轉變 |-- php中布林值為false的情況 $flag = '';//假 $flag = "";//假 $flag = null;//假 $flag = 0;//假 $flag = "0";//假 $flag = 0.0;//假 $flag = "0.0";//真 $flag = array();//假 $flag = 'false';//真 echo $flag ? "真" : "假";
4.字串的一些方法
$name = "kiNg tolY-張風捷特烈-1994328"; echo is_string($name) ? "是字串" : "不字串";//是字串 echo empty($name) ? "為空" : "不為空";//不為空 echo "字串長度:" . strlen($name);//字串長度:33 echo "轉大寫:" . strtoupper($name);//轉大寫:KING TOLY-張風捷特烈-1994328 echo "轉小寫:" . strtolower($name);//轉小寫:king toly-張風捷特烈-1994328 echo "首字母大寫:" . ucfirst($name);//首字母大寫:KiNg tolY-張風捷特烈-1994328 echo "單詞首字母大寫:" . ucwords($name);//單詞首字母大寫:KiNg TolY-張風捷特烈-1994328 echo substr($name, 2, 8);//Ng tolY- echo substr($name, -7, 4);//1994 echo substr($name, 2);//Ng tolY-張風捷特烈-1994328 echo substr($name, 2, -4);//Ng tolY-張風捷特烈-199 echo trim("rry");//rry去除兩端的空格 echo rtrim("rry");// rry 去除右端的空格 echo ltrim("rry");//rry 去除左端的空格 echo trim($name, "k");//iNg tolY-張風捷特烈-1994328 指定字元trim $arr = ["java","kotlin","javascript","c++"]; echo join($arr, "-->");//java-->kotlin-->javascript-->c++ |-- 字串的正則操作 $split = preg_split("/-/", $name);//正則切割 print_r($split);//Array ( [0] => kiNg tolY [1] => 張風捷特烈 [2] => 1994328 ) $match = preg_match("/\d{10,16}/", $name);//匹配連續10~16個數字 print_r($match ? "匹配成功" : "匹配失敗");//匹配失敗 echo preg_replace("/-/","·",$name);//kiNg tolY·張風捷特烈·1994328
5.php與表單
簡單來個表單

表單.png
---->[reg.php]----------------------- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊頁面</title> <h1>註冊頁面</h1> <form action="doReg.php" method="post"> <label>使用者名稱:</label> <input type="text" name="username" placeholder="請輸入使用者名稱"> <label>密碼:</label> <input type="password" name="password" placeholder="請輸入密碼"> <label>確認密碼:</label> <input type="password" name="conform-password" placeholder="確認密碼"> <input type="submit" name="submit"> </form> </head> <body> </body> </html> ---->[doReg.php]----------------------- <?php $name = $_POST['username']; //獲取表單資料 echo $name;//這樣就可以連線mysql插入資料庫了
OK ,第一次接觸PHP,感覺還好吧,個人感覺和python有點像,很多東西都是函式呼叫
而不是像Java,Kotlin等用物件的api來操作,所以感覺函式多起來,挺亂的。
PHP和JavaScript怎麼說呢,感覺側重點不同,誰好誰壞的說不清,各有千秋吧。
語言都類似,基本模組都差不多,關鍵還是看能不能玩轉起來,不吹不黑,PHP還不錯。
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 附錄 |
---|---|---|
V0.1--無 | 2018-3-14 | 無 |
釋出名: 程式語言對比手冊(橫向版)[-PHP-]
捷文連結: https://juejin.im/post/5c8a19d75188257dd56e7d91
2.更多關於我
筆名 | QQ|微信|
---|---|---|---|
張風捷特烈 | 1981462002|zdl1994328|
我的github: https://github.com/toly1994328
我的簡書: https://www.jianshu.com/u/e4e52c116681
我的簡書: https://www.jianshu.com/u/e4e52c116681
個人網站: http://www.toly1994.com
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援

icon_wx_200.png