日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

【学习笔记】PHP基础

發布時間:2024/3/26 php 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【学习笔记】PHP基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輸出

  • echo “< h1>hello world< /h1>”;
  • echo("< h1>hello world< /h1>");
  • print_r("< h1>hello world< /h1>");
  • var_dump(100); // int(100)
  • var_dump(“hello”); // string(5) “hello”

變量定義

  • 變量以$符號開始,后面跟著變量的名稱
  • 變量名必須以字母或者下劃線字符開始
  • 變量名只能包含字母數字字符以及下劃線
  • 變量名不能包含空格
  • 變量名是區分大小寫的
<?php $str = "hello"; print $str."world"; print "{$str} world"; ?>

分支語句

<?php $isTrue = true; if (isTrue) {echo "True"; } else {echo "False"; }$num = 1; switch (num) {case 1:echo "1";break;case 2:echo "2";break;default:break; }for ($i = 0; $i < 5; $i++) {echo $i; } ?>

函數

<?php function print() {print "Hello world<br/>"; } print(); ?>

數組

<?php// 普通數組$color = array("red", "blue", "green");var_dump($color);echo $color[1];for($i=0;$i < count($color);$i++) {echo "$color[i]<br>";}// 關聯數組$arr = array("zhangsan" => "張三", "lisi" => "李四", "wangwu" => "王五");var_dump($arr); // array(3){["zhangsan"]=>string(6) "張三" ["lisi"]=>string(6) "李四" ["wangwu"]=>string(6) "王五"}foreach($arr as $key => $value) {echo "{$key}:{$value}"; // zhangsan:張三 lisi:李四 wangwu:王五}// 二維數組$arr2 = array(array("name" => "張三", "math" => 100, "english" => 95), array("name" => "李四", "math" => 92, "english" => 83), array("name" => "王五", "math" => 80, "english" => 75));for($i = 0; $i < 3; $i++) {foreach($arr2[$i] as $key => $value) {echo "{$key}:{$value} ";}echo "<br>";} ?>

數組函數

字符串函數

時間函數

數據傳輸格式

  • xml
    優點:
    • 數據種類豐富
    • 傳輸數據量大
      缺點:
    • 解析麻煩
    • 不適合輕量級數據
  • json(大多數移動端使用)
    優點:
    • 解析容易
    • 適合輕量級數據
      缺點:
    • 數據種類比較少
    • 傳輸數據量小

AJAX

AJAX:異步的JavaScript和XML
AJAX是前后端數據的搬運工,可以異步執行

  • 同步:阻塞,當前程序運行必須等前一個程序運行結束才能運行
  • 異步:非阻塞,當前程序運行與前一個程序無關

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>Test</title><script>window.onload = function() {var oBtn = document.getElementById("btn1");var oDiv = document.getElementById("div1");oBtn.onclick = function() {var xhr = XMLHttpRequest(); // 創建ajax對象xhr.open("GET", "1.txt", true); // true為異步xhr.send();xhr.onreadystatechange = function() {if (xhr.readyState == 4) {if (xhr.status == 200) {oDiv.innerHTML = xhr.responseText;} else {alert(xhr.status);}}}}}</script></head><body><button type="button" id="btn1">下載數據</button><div id="div1" style="width: 100px; height: 50px; border: 1px solid black; margin-top: 5px;"></div></body> </html>

get和post提交

  • get提交
    直接在url后插入數據 http://localhost/1.php?username=abc&password=123
    優點:簡單
    缺點:不安全;數據量有上限
  • post提交
    url不會顯示數據 http://localhost/1.php
    優點:安全;理論上沒有上限
    缺點:底層實現比get復雜
<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>Test</title></head><body><!-- <form action="1.php" method="get"> --><form action="1.php" method="post" enctype="application/x-www-form-urlencoded"><input type="text" name="username" placeholder="用戶名" /><input type="password" name="password" placeholder="密碼" /><input type="submit" value="提交"/></form></body> </html> <?phpheader(content-type:text/html;charset="utf-8");// $username = $_GET["username"];// $password = $_GET["password"];$username = $_POST["username"];$password = $_POST["password"];echo "用戶名為{$username},密碼為{$password}"; ?> var xhr = null; try {xhr = new XMLHttpRequest(); } catch(error) {xhr = new ActiveXObject("Microsoft.XMLHTTP"); }// get參數直接在url后面加參數 xhr.open("GET", "1.php?username=abc&password=123", true); xhr.send();/* post 提交 xhr.open("POST", "1.php", true); xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); xhr.send("username=abc&password=123"); */xhr.onreadystatechange = function() {if (xhr.readyState == 4) {if (xhr.status == 200) {alert(xhr.responseText);} else {alert(xhr.status);}} }

json

var arr = [100, true, "hello"]; var str = JSON.stringify(arr); // 數組轉字符串 var arr1 = JSON.parse(str); // 字符串轉數組

php中處理json

$arr = array('username' => 'abc', 'password' = > '123'); $str = json_encode($arr); // 數組轉字符串 $arr2 = json_decode($str); // 字符串轉數組

php 連接 mysql

<?php$link = mysql_connect("localhost", "root", "123456");if (!link) {echo "連接失敗";exit;}mysql_set_charset("utf8");mysql_select_db("php");$sql = "SELECT * FROM students";$res = mysql_query($sql);echo "<table border='1'>";echo "<tr><th>姓名</th><th>成績</th></tr>";while($row = mysql_fetch_assoc($res)) {echo "<tr><td>{$row['name']}</td><td>{$row['score']}</td></tr>";}echo "</table>";mysql_close($link); ?>

跨源請求

AJAX只能下載同源的數據,不能下載跨源的數據
同源:同協議、同域名、同端口號
跨源的方式:

  • 修改ajax同源協議(不建議)
  • 委托php文件進行跨源
  • JSONP跨域

JSONP跨源步驟

  • 先聲明一個有形參的函數,對這個參數做后續處理
  • 在需要下載數據時,動態創建script標簽,將標簽的src設置成要下載的數據的url
  • 當script插入到頁面時,會調用封裝好的函數,將數據傳至頁面
  • <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Test</title><style type="text/css">#div0 {text-align: center; margin: 10px auto; margin-top: 50px;}#div0 img {width: 250px;}#div1 {width: 520px; margin: 10px auto;}#input1 {width: 400px; height: 30px; display: inline-block;}#input2 {width: 100px; height: 36px; display: inline-block; background-color: royalblue; color: white; border: 0;}#ul1 {list-style: none; width: 402px; border: 1px solid black; padding: 0; margin: 0; display: none;}#ul1 li a {text-decoration: none; color: black; line-height: 20px; padding: 5px; display: block;}#ul1 li a:hover {background-color: orange; color: white;}</style><script type="text/javascript">function download(data) {var arr = data.s;var oUl = document.getElementById("ul1");oUl.innerHTML = "";oUl.style.display = "block";for (var i = 0; i < arr.length; i++) {var oLi = document.createElement("li");var oA = document.createElement("a");oA.innerHTML = arr[i];oA.href = `http://www.baidu.com/s?wd=${arr[i]}`;oLi.appendChild(oA);oUl.appendChild(oLi);}}</script><script type="text/javascript">window.onload = function() {var oInput = document.getElementById("input1");var oUl = document.getElementById("ul1");oInput.onkeyup = function() {var oValue = this.value;if(!oValue) {oUl.style.display = "none";} else {var oScript = document.createElement("script");oScript.src = `http://suggestion.baidu.com/su?wd=${this.value}&cb=download`;document.body.appendChild(oScript);}}}</script></head><body><div id="div0"><img src="https://www.baidu.com/img/flexible/logo/pc/result@2.png" ></div><div id="div1"><input type="text" id="input1" /><input type="submit" id="input2" value="百度一下" /><ul id="ul1"></ul></div></body> </html>

    cookie

    <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Test</title><script type="text/javascript">window.onload = function() {var oDiv = document.getElementById('div1');setCookie('name', 'Tom', {expires: 7});oDiv.innerHTML = getCookie('name');var oBtn = document.getElementById('remove');oBtn.onclick = function() {removeCookie('name');removeCookie('age');oDiv.innerHTML = getCookie('name');}}function setCookie(name, value, {expires, path, domain, secure}) {var cookieStr = encodeURIComponent(name) + "=" + encodeURIComponent(value);if (expires) {cookieStr += ";expires=" + getDateAfter(expires);}if (path) {cookieStr += ";path=" + path;}if (domain) {cookieStr += ";domain=" + domain;}if (secure) {cookieStr += ";secure";}document.cookie = cookieStr;}function getCookie(name) {var cookieStr = decodeURIComponent(document.cookie);var start = cookieStr.indexOf(name + '=');if (start == -1) {return null;} else {var end = cookieStr.indexOf(';', start);if (end == -1) {end = cookieStr.length;}var str = cookieStr.substring(start, end);return str.split('=')[1];}}function removeCookie(name) {document.cookie = encodeURIComponent(name) + "=;expires=" + new Date(0);}function getDateAfter(n) {var d = new Date();var day = d.getDate();d.setDate(n + day);return d}</script></head><body><button id="remove">remove</button><div id="div1"></div></body> </html>

    總結

    以上是生活随笔為你收集整理的【学习笔记】PHP基础的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。