php基础知识

2021/9/30 17:12:16

本文主要是介绍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进行编码转换

//输入指定的年份,月份,返回指定的天数
// Function day_count($year,$month){
// Echo date("t",strtotime($year."-".$month."-1"));
// }
// 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";
// $result = mysqli_query($conn,$sql);
// 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:
// # code...
// break;
// }


// $favcolor = 'green';
// switch($favcolor)//括号中是判断对象
// {
// case "yellow"://判断情况之一
// echo "你喜欢的颜色是黄色!";
// break;
// case "blue":
// echo "你喜欢的颜色是蓝色!";
// break;
// case "red":
// echo "你喜欢的颜色是红色!";
// break;
// default://其它情况
// echo "你喜欢的颜色不是黄、蓝、红色!";
// break;
// }

//构造方法
// 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)

// {
// if (is_dir($path)) {
// # 如果目录已经存在,则不创建
// echo "该目录已经存在";
// } else {
// # 不存在,创建
// if (mkdir($path,$mode,true)) {
// echo "创建目录成功";
// } else {
// echo "创建目录失败";
// }

// }

// }
// create_dir("F:/text/phptest");
//获取扩展名
// $url = "http://www.sina.com.cn/abc/de/fg.php?id=1";
// function getExt1($url){
// $arr = parse_url($url);//返回url的组成部分
// $file = basename($arr['path']);//显示带有文件扩展名的一部分
// $ext = explode('.', $file);
// return $ext[count($ext)-1];
// }
// echo getExt1($url);



这篇关于php基础知识的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程