天天看點

php基礎知識

<?php

// $now = date("Y-m-d H:i:s" ,time());//時間戳傳華為日期

// $time = strtotime("2017-08-08 23:00:01");//日期轉化為時間戳

// echo $time;

// $d=mktime(9, 12, 31, 6, 10, 2015);//mktime将指定日期轉化為時間戳(1970.1.1距離日期的時間戳)

// echo "建立日期是 " . date("Y-m-d h:i:sa", $d);

// echo count(strlen("http://php.net"));//傳回結果為1,count函數用來統計數組個數,strlen用來傳回字元串長度,

// $my_array = array("Dog","Cat","Horse");

// list($a, $b, $c) = $my_array;//list()是用來把數組中的值指派給變量,但數組必須是索引數組,索引從0開始

// echo "I have several animals, a $a, a $b and a $c.";

// Header('location:index.php');//php中跳轉的句法

// $pattern = '/<script.*>\.+<\/script>/';//用正規表達式去除就是腳本的方法

// Preg_replace($pattern,'',$str);

//去掉數組中空值的方法,

// $array1 = array(' ',1,'',2,3);

// print_r(array_filter($array1, "del"));//array_filter通過回調函數過濾數組,如果回調函數傳回true,則包含在數組中,否則删除

// function del($var)

// {

// return(trim($var));

// }

//正則去除數組中空值的方法

// $arr=array("",1,2,3,"");

// $ptn="/\S+/i";//i是正則修飾符,為不區分大小寫

// print_r(preg_grep($ptn,$arr));

//擷取目前時間戳,列印前一天時間的方法

// time()

// echo date("Y-m-d H:i:s",Strtotime("-1 day"));

//php編碼轉化函數

// Iconv(‘utf-8’,’gb2312’,$str);

//字元串轉化為數組

// $str = "1,3,5,7,9,10,20";

// $arr = explode(",",$str);

// // var_dump($arr);

// //數組轉化為字元串

// $st = implode(',',$arr);

// echo $st;

//寫出一個函數,參數為年份和月份,輸出結果為指定月的天數

// Function day_count($year,$month){

// Echo date("t",strtotime($year."-".$month."-1"));

// day_count(2018,3);

//serialize() /unserialize()函數的作用

// $arr = array("測試1","測試2","測試3");//數組

// $sarr = serialize($arr);//産生一個可存儲的值(用于存儲)

// var_dump($sarr);

// $unsarr=unserialize($newarr);//從已存儲的表示中建立 PHP 的值

//一個檔案的路徑為/wwwroot/include/page.class.php,寫出獲得該檔案擴充名的方法

// $arr = pathinfo("/wwwroot/include/page.class.php");//pathinfo以數組的形式傳回檔案路徑資訊

// $str = substr($arr['basename'],strrpos($arr['basename'], '.'));//strrpos傳回字元最後一次出現的位置

// $num = strrpos($arr['basename'], '.');

// // echo $num;

// var_dump($arr);

//請簡單寫一個類,執行個體化這個類,并寫出調用該類的屬性和方法的語句

// class Car {

// //定義公共屬性

// public $name = '汽車';

// //定義受保護的屬性

// protected $corlor = '白色';

// //定義私有屬性

// private $price = '100000';

// public function getPrice() {

// return $this->price; //内部通路私有屬性

// }

// $car = new Car();

// echo $car->name; //調用對象的屬性,屬性方法之前千萬不能加$符号

// $a = $car->getPrice();//受保護或私有屬性隻能内部調用,然後可以通過調用該方法在外部獲得

// // echo $car->color; //錯誤 受保護的屬性不允許外部調用

// // echo $car->price; //錯誤 私有屬性不允許外部調用

//本地資料庫info裡已建有表info,資料庫的連接配接使用者為root,密碼為123456,請連接配接查詢id大于70的資料

// $conn = mysqli_connect("localhost","root","123456","info") or die('連接配接失敗');

// $sql = "select * from info where id > 65";

// $result = mysqli_query($conn,$sql);

// foreach ($result as $k => $v) {

// echo $v['id']." ".$v['name'].$v['ad']." ",$v['tel']." ",$v['ip']." <br/>";

//簡單實作分頁

// $con = mysqli_connect("localhost","root","123456","info");

// mysqli_query($con,"set names utf8");

// $pageSize = 5;

// $result = mysqli_query($con,'select * from info');

// $totalNum = mysqli_num_rows($result);

// $countPage = intval($totalNum/$pageSize);

// $nowPage = isset($_GET['page']) ? intval($_GET['page']) : 1;

// $prev = ($nowPage - 1 <=0) ? 1 : $nowPage-1;

// $next = ($nowPage + 1 >= $countPage) ? $countPage : $nowPage + 1;

// $offset = ($nowPage - 1) * $pageSize;

// $sql = "select * from info limit $offset,$pageSize";

// $ret = mysqli_query($con,$sql);

// while($arr = mysqli_fetch_array($ret)){

// echo $arr['id']." ".$arr['name']." <br/>";

// echo "<a href = \"".$_SERVER['PHP_SELF']."?page=1\">首頁</a>";

// echo "<a href = \"".$_SERVER['PHP_SELF']."?page=".$prev."\">上一頁</a>";

// echo "<a href = \"".$_SERVER['PHP_SELF']."?page=".$next."\">下一頁</a>";

// echo "<a href = \"".$_SERVER['PHP_SELF']."?page=".$countPage."\">尾頁</a>";

//冒泡排序法

// function mysort($arr){

// for($i = 0;$i<count($arr);$i++){

// for($j = 0;$j<count($arr)-1-$i;$j++){

// if($arr[$j]>$arr[$j+1]){

// $temp = $arr[$j+1];

// $arr[$j+1] = $arr[$j];

// $arr[$j] = $temp;

// }

// }

// }

// return $arr;

// $arr = [23,54,1,2,12,5,32,22,13,6,77];

// print_r(mysort($arr));

// //乘法表

// for($i = 1;$i<=9;$i++){

// for($j = 1;$j<=$i;$j++){

// echo $j. "*" .$i ."=" .$j*$i." ";

// }

// echo "<br/>";

// //反轉字元串

// $s = '1234567890';

// $arr = ['hello',"你好",33,'good'];

// $o = '';

// $i = 0;

// while(isset($arr[$i]) && $arr[$i] != null) {

// $o = $arr[$i++].$o;

// var_dump($o);//可翻轉數組

// // echo strrev($s);//反轉字元串的函數

//使首字母大寫

//方法一

// function Fun($str){

// if(isset($str) && !empty($str)){

// $newStr='';

// if(strpos($str,'-')>0){

// $strArray=explode('-',$str);

// $len=count($strArray);

// for ($i=0;$i<$len;$i++){

// $newStr.=ucfirst($strArray[$i]);//ucfirst數字母大寫,類似ucwords()

// }

// }

// return $newStr; }

// }

// var_dump(Fun("fang-zhi-gang")); //FangZhiGang

// 傳入年份,月份,傳回特定月份的開始和結束的時間戳

// public function getShiJianChuo($nian=0,$yue=0){

// if(empty($nian) || empty($yue)){

// $now = time();

// $nian = date("Y",$now);

// $yue = date("m",$now);

// }

// $time['begin'] = mktime(0,0,0,$yue,1,$nian);

// $time['end'] = mktime(23,59,59,($yue+1),0,$nian);

// return $time;

//傳入時間範圍,用時間戳寫資料查詢的條件

// $input = array_column(I('input'), 'value', 'name');

// //确定時間段範圍

// if($input['search_time']){

// $st = strtotime(explode(' - ', $input['search_time'])[0]);

// $et = strtotime(explode(' - ', $input['search_time'])[1]) +86399;

// $where['unix_timestamp(pay.addtime)'] = array('BETWEEN',array($st,$et));

//支付訂單号研究,

//floor,

// $out_trade_no = date('Ymdhis', time()).substr(floor(microtime()*1000),0,1).rand(0,9);

// $out_trade_no = date('Ymdhis', time());

// echo $out_trade_no;

//删除字元串中的%

// function decodeUnicode(str) {

// str = str.replace(/\\/g, "%");

// return unescape(str);

// }

//多條件查詢,關鍵字or

// $where1['id'] = ['in',$cinema_data];

// $where2['adder'] = session('mobile');

// $where=array($where1,$where2,'_logic'=>'or');//不論是數組還是字元穿都可以

// $list = M('cinema')->where($where)->order($orde)->select();

// echo __FILE__;//傳回一個絕對路徑

// echo $_SERVER['REMOTE_ADDR'];//擷取用戶端地IP位址

// header("location:index.php")//跳轉頁面語句

// $pattern = '/<script.*>\.+<\/script>/';

// Preg_replace($pattern,’’,$str);//$str是一段heml文本,使用正規表達式去除其中的js腳本

// Iconv('utf-8','gb2312',$str);//php進行編碼轉換

//輸入指定的年份,月份,傳回指定的天數

// day_count(2019,2);

//給定路徑,擷取擴充名

// $arr = pathinfo("wwwroot/include/page.class.php");

// var_dump(substr($arr['basename'],strrpos($arr['basename'],'.')));

//調用屬性和方法的方法

// Class myclass{

// Public $aaa;

// Public $bbb=444;

// private function _justForMyself(){

// echo "這是私有函數,隻能在類内部使用哦"."<br/>";

// Public function myfun(){

// $this->aaa="test";

// $this->_justForMyself();

// echo "this is my function"."<br/>";

// $myclass = new myclass();

// $myclass->myfun();//調用類的方法,私有屬性隻能在類的内部調用。

// echo $myclass->aaa;//調用類的屬性,放在方法調用之後,因為方法為屬性重新賦了值

//連烈資料庫,關鍵隻需要三步

// $conn = mysqli_connect("localhost","root","123456","info") or die("連接配接錯誤!");

// mysqli_query($conn,"set name 'utf-8'");

// $sql = "select * from info";

// while($row = mysqli_fetch_assoc($result)){

// echo $row['name'];

// foreach ($result as $key => $value) {

// var_dump($value['name']);

//switch語句

//基本結構

// switch (variable) {

// case 'value':

// # code...

// break;

// default:

// $favcolor = 'green';

// switch($favcolor)//括号中是判斷對象

// case "yellow"://判斷情況之一

// echo "你喜歡的顔色是黃色!";

// break;

// case "blue":

// echo "你喜歡的顔色是藍色!";

// case "red":

// echo "你喜歡的顔色是紅色!";

// default://其它情況

// echo "你喜歡的顔色不是黃、藍、紅色!";

//構造方法

// class FatherAction{

// public function __construct(){

// echo "father";

// class SonAction extends FatherAction{

// public function __construct(){

// parent::__construct();

// echo "son";

// public function index() {

// // echo "hellow";

// // public function getPrice() {

// // echo "red";

// // }

// $f = new SonAction();

// $f->index();

//php建立多級目錄的函數

/**

* 建立多級目錄

* @param $path string 要建立的目錄

* @param $mode int 建立目錄的模式,在windows下可忽略

*/

// function create_dir($path,$mode = 0777)