php
来源:互联网 发布:英国硕士绩点算法 编辑:程序博客网 时间:2024/06/10 07:58
//PHP配置
//修改php.ini配置文件,开启mysqli扩展
extensing=php_mysqli.dll //去掉php.ini前边的分号
extension_dir="D:\phpStudy\php\php-5.5.38\ext"//设置PHP的ext的路径 绝对路径 扩展文件地址
//php.ino中与session相关的配置
session.auot_start=0 //不自自动开启
session.auto_start=1 //自动开启
//设置session的回收时间
session.gc_maxlifetime=1440
//session的保存路径
session.save_path = '/tmp'
//开启gd2的扩展
extension=php_gd2.dll
//创建图片文档类型
//header('Content-Type:image/jpeg');
//创建一张空白图片
//imagecreatetruecolor($width,$height);
//画一个验证码
$img=imagecreatetruecolor(75,25);
//设置颜色
imagecolorallocate($img,$red,$green,$blue);
//画矩形 设置背景色 //$x,$y 左上角标 $x2m,$y2右下角坐标
imagefilledrectangle($img,$x,$y,$x2m,$y2,$coloer;);
//画字符串 //$font 1-5 最小1最大5 $x坐标值 $y坐标值 $string 要画的字
imagestring($img,$font,$x,$y,$string,$color);
//画字线段 //$font 1-5 最小1最大5 $x坐标值 $y坐标值 $string 要画的字
imagestring($img,$x1,$y1,$x2,$y2,$color);
//画点
imagesetpixel($img,$x,$y,$color);
//将图片发送到客户端浏览器
imagejpeg($img);//注意发送图的类型必须与文档类型一致
imagegif($img);//注意发送图的类型必须与文档类型一致
imagepng($img);//注意发送图的类型必须与文档类型一致
//销毁内存中的图片对像
imagedestroy($img)
//目录函数
//创建文件夹 注意:不能创建多级目录
//mkdir('文件夹名');//在当前目录
mkdir('images');//在当前目录
mkdir('images/home');//在指定路径下新建一个文件夹
mkdir('d:/home'); //绝对路径
//删除当前目录下的指定文件夹
//rmdir('images') 注意:只能删除空文件夹
rmdir('images') //移除文件夹 删除文件夹
//获得当前目录
$str=getcwd();//返回字符串 当前目录
//打开目录
$handler=opendir('路径/文件夹');
$str=readdir($handler);
//遍历目录(读取文件夹里的内容)
while($str=readdir($handler)){ //遍历完返回空
echo "{$str}<br/>";
}
//关闭目录
closedir($handler);
//遍历目录
$fileList=scandir('路径/文件夹');
//遍历目录 返回:一维索引数组
$fileList=scandir('d:/phpStudy');//同上面while一样的功能
//显示所有名称
foreach($fileList as $v){
echo $v;
}
//文件函数
//1.创建一个空白文件 touch('路径/文件名');
touch('hello.txt');//当前目录下创建空白文件
touch('php/hello.txt');//相对路径
touch('d:php/hello.txt');//绝对路径
//2.给文件重命名 rename('原名','新名');
//rename($oldname,$newname);
rename('hello.txt','hello.php');//hello.txt改成hello.php
rename('d:/hello.txt','d:/hello.php');//只能改文件名,不能改路径
//3.复制文件copy('原文件','目地文件');
//copy($sourec,$dest);
copy('d:/test.php','e:/test/php');
//4.删除文件
unlink('d:/text.php')//unlink('路径/文件名')
//5.获得文件上次的访问时间 fileatime($filename) 返回:时间戳
$a=fileatime('d:/text.php');//返回:时间戳
//$date=date('日期格式',$a);
$date=date('Y-m-d H:i:s',$a); //时间戳转成时间
//6.获得文件最后一次的修改时间
$a=filectime($filename);//返回时间戳
//$date=date('日期格式',$a);
$date=date('Y-m-d H:i:s',$a); //时间戳转成时间
//7.获得文件大小 filesize('路径/文件名');
filesize('d:/text.php');
//8.获得文件类型
$ftype=filetype('d:/text.php');//返回 file,dir
//9.判断某路径对应的是否为文件
$result=is_file('d:/text.php');//返回 true,false
//10.判断某路径对应的是否为文件夹
$result=is_dir('d:/text.php');//返回 true,false
//11.将相对路径,转为绝对路径
$result=realpath('d:/text.php');
//12.返回整个路径的文件名
$result=basename('d:/text.php');
//13.返回整个路径对应的目录部分
$result=dirname('d:/text.php');
//14.判断文件是否存在
$result=file_exists('d:/text.php');
//文本文件的读写
//1.写入文本文件
file_push_contents('路径','内容');
//2.获得文本文件中的内容
file_get_contents('路径');
//3.将字符串中所有的\n转换为<br/>
$result=n12br('字符串内容');
//正则表达式
//$str='1361301428';
//pattern="/正则表达式/";
$result=preg_match($pattern,$str);
//正则替换字符串里的数字
//$result=preg_replace(正则表达式,'替换成当前字符',需要替换的字符串);
$str='社会主义好9你好456大家好78956';
$pattern="/\d+/";
$result=preg_replace($pattern,'hello',$str);
//通配符
\n 匹配回车符(Windows)
\r 匹配换行符(Linux)
\t 匹配水平制表符(tab键)
\v 匹配垂直制表符(连继换4行)
\f 匹配翻页符
\w 匹配任意字符以及下划线(汉字特殊符号除外)
\W 匹配任意一个非字符(如:特殊符号汉字)
\d 匹配任意0-9数字
\D 匹配任意非数字,(如:除了0-9以外的任意其它字符)
\s 匹配一个空格
\S 匹配一个非空格
. 占位符,代表当前位有一个任意字符
^ 严格开头
$ 严格结尾
//限定符
? 0次或1次
+ 1次或多次
* 0次或多次
{n} 重复n次
{n,} 最少n次,最多无数次
{n,m} 最少n次,最多出现m次
\d{10} 代理当前位置必须要有10个连继的数字
//其它符号
() 提高优先级
[] 综括号内内容多选一//如:[abc] [a-z0-9]
| 或者
//url重写
//Rewrite(Apache的功能) 技术 url重写
//修改httpd:conf配置文件 去掉前面的# 开启url重写功能
//#LoadModule rewrite_module modules/mod_rewrite.so
//找到如下选项 进行修改
<Directory />
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride All //如果是none改为All
Order allow,deny
Allow from all
Require all granted
</Directory>
//找到如下选项 修改开发目的的选项 进行修改
<Directory "D:\phpStudy\WWW">
AllowOverride All //如果是none改为All
Options None
Require all granted
</Directory>
//在项目的根目录下新建一个文件.htaccess(url重写规则文件);
//1)该文件的文件名固定
//2)文件的存放位置固定(必须存放在项目的根目录)
//3)在.htaccess中,编写规则
//---图片水印
//打开指定的图片
//$img=imagecreatefromjpeg('图片路径');
$img=imagecreatefromjpeg('img/a.jpg');
//画字符串
$color=imagecolorallocate($img,255,255,255);
imagestring($img,5,200,100,'www.php.me',$color);
//画字符串02
//imagettftext($img,大小,角度,$x,$y,$color,字体文件,要显示的字符串)
//imagettftext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
imagettftext($img,30,-45,50,100,$color,'font/zw.otf','你好');
//存盘
imagejpeg($img,'保存路径');
//图片缩放
//打开原图
$srcImg=imagecreatefromjpeg('图片地址');//原图
$newImg=imagecreatetruecolor(宽,高);//缩放后的图
$imgsize=getimagesize('img/a.jpg');//获得图片宽高返回数组
print_r($imgsize);
//imagecopyresized(新图,原图,新图_x,新图_y,原图_x,原图_y,新图_宽,新图_高,原图_宽,原图_高) //缩放
imagecopyresized($dst_img,$str_img,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h) //缩放
imagejpeg($newImg,'img/hello.jpg');//存盘
//出现编码的地方
1. 文件存储格式的编码
2. <meta http-equiv='content-type' content='text/html'; charset=utf-8>
3. header("content-type:text/html;charset=utf-8");
4. MySQL本身编码
5. 建库时指定库的编码
//连接数据库流程 mysqli函数库
<?php
header('Content-Type:text/html;charset=utf-8');
//1:连接数据库
$conn=mysqli_connect('127.0.0.1','root','root','ifeng') or die('数据库连接失败');
//2:向mysql发送sql语句
$sql='delete from t_news where nid=1';
$result=mysqli_query($conn,$sql);
//判断是否执行成功
if($result===true){
echo '删除成功';
}else{
echo '删除失败';
};
//4:关闭数据库
// $searr=mysqli_free_result($rs);//如果mysql语句有结果集 释放解果集 返回索引+关联数组
// $searr=mysqli_fetch_assoc($rs) //返回的是关联数组
// $searr=mysqli_fetch_row($rs) //返回的是索引数组
mysqli_close($conn);
phpinfo();//查看PHP信息
//包含文件
include '路径/xxx.php'; //包含的文件有错误,产生一个警告,程序继续执行;
require '路径/xxx.php'; //包含的文件有错误,产生一个至命错误,程序终止执行;
include_once '路径/xxx.php'; //如果包含多次同一文件,页面只包含一次,只有一次生效;
require_once '路径/xxx.php'; //如果包含多次同一文件,页面只包含一次,只有一次生效;
//session
//开启会话 session_start();
session_start();
$id=session_id();
//存取数据
$b=$_SESSION['userName'];
echo $b;
//向cookiek中存储临时性数据 //浏览器关闭cookiek失效
setcookie('address','北京',)
//$变量=$_COOKIE[名]; 获得cookie的值的方法
//setcookie('cookie名','值',到期时间);//
//向cookie中存储数据 //时间到期cookiek失效
setcookie('address','北京',time()+3600*48);//两天后到期
//cookie存数组
$cityList=array('北京','上海','天津','深圳');
setcookie('addr',$cityList);
$arrcook=$_COOKIE['addr'];
//函数
//数组转成字符串,序列化数组
$str=serialize($cityList);
//反序列化 //将字符串还原为数组
$arr=unserialize($str);
//分割字符串
$str='中国-美国-英国-德国';
$arr=explode('-',$str);//返回数组
//返回一个英文字母加数据的字符串,保证调用一万次产生的值不同
uniqid()//产生不同字符串
//面向对像
//类的封装
//属性
//可见性
$属性名称=初值
public //公共类特点:类内部可见,对像可见 可继承
protected //受保护类 特点:类内部可见,对像不可见 可被继承
private //私有类 特点:类内部可见,对像不可见 不能被继承
//方法
//可见性
function 方法名称(参数1,...){
方法体;
return 返回值;
};
//构造函数
1. function __construct(参数1,...){
....
}
2. function 类名5(参数1,...){
....
}
//析构函数
function __destruct(){
....
}
//如:声明一个类
class people{
public $age;
public $height='1.75';
public $weight='55';
//构造函数 实例化类后自动调用
function __construct(参数1,...){
}
function eat($what=''){
if($what='大鱼大肉'){
$this->$weight='90';
}else if($what='素菜'){
$this->$weight='50';
}else{
$this->$weight='45';
}
}
}
//类的自动加载 例如:
function __autoload($className){
//echo 'nihao';
$classFileName=$className.".class.php";
require 'class/'.$classFileName;
};
$ob=new News();
var_dump($ob);
//设置自动加载目录:
set_include_path('路径');//可以设置多个
//先去class目录下找,没找到再去class/public路径下找 例如:
set_include_path('class;class/public');
//linux系统的分割符是冒号":" 不是分号";" 如下
set_include_path('class:class/public');
PATH_SEPARATOR //系统常量 不分系统 如下
set_include_path('class PATH_SEPARATOR class/public');
//继承语法格式 //子类继承父类 只有 protected public可以被继承 private不可以被继承
// 例如: f 为父类 son 为子类
class f{
public $a=1;
protected $b=2;
private $c=3;
private $d=4;
public function fun1(){
echo 'fun1';
}
public function fun2(){
echo $this->a;
}
protected function fun3(){
echo $this->b;
}
function fun4(){
echo $this->d;
}
}
class son extends f{
}
$exobj=new son();
$exobj->fun1;
//抽像类 一个类中有没有方法体的方法,这个方法就叫抽像方法abstract class 类名{}格式如下:
abstract class 类名{
属性:
方法:
抽像方法:
}
//抽像方法 protected 可加可不加
abstract protected function 方法名称(参数1,...){
}
//接口文件写法 例如:
interface News{
/* desc:添加文章
* params: array $arr, example:array('title'=>'cotent')
* return:主键id值
* author:xufangfang-99@163.com
*/
function adNews($arr);
/* 说明:删除文章
* 参数: int $id, example:'15'
* 返回值:主键id值
* 联系方法:xufangfang-99@163.com
*/
function deleteNewsById($id);
}
//继承接口:
class 类名 implements 接口文件名{
}
class News extends Model implements 接口文件名{
}
//魔术方法 名字固定,功能固定
function __construct() //构造函数
function __destruct() //析构函数
//__call(方法名,参数(数组))
__call($funName,$args) //调用一个未定义的方法时__自动被启动
__get($proName)//访问一个未定义的属性时,自动调用
//__set(属性名,属性值)
__set($proName,$proValue)//给一个不存在属性赋值时,自动调用。
__clone()//当对像被克隆时,自动调用
函数:克隆变量
clone(变量)
$a=123;
$b=clone($a);
private function __clone(){//防止克隆
}
__sleep()
__wakeup()
//静态类
class 类名{
可见性为 static 属性名=默认值;
可见性为 static functin 方法名(参数1,..){
....//方法体
$a=123;
//静态类不可以这样访问 $this->静态属性名;
//静态方法可通过下面方法调用
类名::静态属性名;//访问静态属性前面要加$
self::静态属性名;
类名::静态方法名;
self::静态方法名;
return 值;
}
}
//类常量
const 名称=值;
如
圆类
const pi=3.14;
//单例模式 //数据库操作类,需要。
class Test{
private function __construct(){}//防止外部调用NEW实例化,构造函数私有化
protected static $ob='';//初始化单例为空
static function getInstance(){//只能调用一次方法
if(self::$ob==''){
$ob=new Test();
self::$ob=$ob;
return $ob;
}else{
return self::$ob;
}
}
private function __clone(){}
}
Text::getInstance();
//PDO 开启
extension=php_pdo.dll
extension=php_pdo_mysql.dll
//使用PDO
//$ob= new PDO("mysql:host=ip地址;dbname=数据库名称", '用户名', '密码');
$ob=new PDO("mysql:host=localhost;dbname=news", 'root', 'root');
var_dump($ob);
//方法
query(sql语句) //select 返回 object
exec(sql语句) //insert update delet 返回影响的条数
fetch()//获取一条记录
//$arr=$psob->setFetchMode(PDO::FETCH_ASSOC);
fetchAll()//获取所有记录 实例如下
//$arr=$psob->fetch();//fetch 每次取一条,然后指针下移
//$arr=$psob->fetchAll();//fetch 每次取一条,然后指针下移
//指定返回数的类型
setFetchMode();
//PDO常量
PDO::FETCH_ASSOC
PDO::FETCH_NUM
PDO::FETCH_BOTH
PDO::FETCH_OBJECT
//sql语句预处理
$pdoOB->prepare("insert manager(username,password) value('fanny0001','123456')")
$psOb->execute(array('值1','值2','值',...));
//占位符
//?
//:
//sql语句中哪些部分可以使用预处理。
select 字段列表 from 表名 where 字段 运算符 ? and type=?
limit start,length 只能有一个使用占位符
having 字段名 运算符?
insert into 表名 set 字段=?,字段=?,...where 字段 运算符?
update 表名 set num=num+1 where 字段 运算符 ?
delete from 表名 where 字段 运算符 ?
//smarty模板
//自定义函数实现字符串截取,支持中文
$str //被截取的字符串
$charNum //截取长度
$charset //中文字符的编码
function mysubstr($str,$charNum,$charset='utf-8'){
if(mb_strlen($str,$charset)<=$charNum){
return $str;
}else{
return mb_substr($str,0,$charNum,$charset).'...';
}
}
//注册函数 把主程序中的函数,注册到模板上使用
//1.在主程中声明函数
//2.如何注册:
$smarty->registerPlugin('function','模板上函数名称','被注册的函数名');
//缓存
//1.修改配置文件
$smarty->caching=true;//开启缓存
$smarty->cache_lifetime=300;//指定缓存时间 单位:300秒
//2.生成缓存
$smarty->isCached('path','cacheid');//判断缓存是否存在及合法
$smarty->display('path','cacheid');//生成缓存,拿缓存,显示
<{nocache}> <{$t}> <{/nocache}>//局部不缓存
//静态化
//1.获取详细页运行的结果
$content=$smarty->fetch('path');//返回文本字符串
//TP thinkPHP框架
//指定应用目录
define('APP_PATH','./Application/');
//加载入口文件
require './ThinkPHP/ThinkPHP.php';
//url地规则
// index.php?m=模块名&c=控制器&a=方法名&... //普通模式
// index.php/模块名/控制器名/方法名/id/值... //PathInfo模式
// index.php?s=/模块名/控制器名/方法名/id/值... //兼容模式
// .haccess文件中正则表达式决定 rewrite模式
// 如: /news_detail_id_1.html 浏览器解析 index.php/home/news/detail/id/1
// $this->assign('s',$str); 变量传给模板
// $this->error("提示信息","目标地址");失败提示
// $this->seccess("提示信息","目标地址");成功提示
// $this->display();显示模板
// U("模块/控制器/方法",array('名'=>值,...));
// redirect(); //实现跳转,没有提示
// __PUBLIC__ 模板标签用来生成Public目录对应的url地址
// 后台访问控制
// 在LoginController/check中创建会话变量
// 在NewsController/add,oper中判断
//
// V模板的编写
// 普通变量标签 {$名称}
// 数据变量标答 {$数组名.下标} {$数组名['下标']}
// 数组遍历标签 <foreach name='数组名' item='v' key='k'> {$v.下标} </foreach>
// 条件标签 <if condition="$a "> </if> 标签符不要定界签{}
// <if condition="$a "> <else/> </if>
// <if condition="$a "> <elseif condition="条件"> <else/> </if>
// 说明条件中不能使用< > 大于号 >,小于号 < 因为和定界符冲突
// eq 等于
// gt 大于
// lt 小于
// egt 大于等于
// elt 小于等于
// neq 不等于
// heq 恒等于
// nheq 不恒等于
// 变量调节器
// {$被调节变量|函数=参数1,函数=参数2,###...}//
// {$Think.const.常量名称}常量的使用
// {$Think.get.名称}
// {$Think.cookie.下标}
// {$Think.request.下标}//即能接收get传值也能接收post传值
// {$Think.session.下标}
// 公共模板加载
// <include file='path'>
// path写法:
// ./Application/Home/View/Public/header.html
// 控制器名:方法名
// Public:header
// 模板上路径标签:
// 例如路径: http://www.php.me/homework/TP/index.php/Home/News/lister
// __PUBLIC__ 静态资源的路径 js css image
// ure地址,a连接
// __ROOT__ //当前的根路径 如: http://www.php.me/homework/TP
// __APP__ //当前Think入口文件index.php 如: http://www.php.me/homework/TP/index.php
// __MODULE__ //当前模块Home 如: http://www.php.me/homework/TP/index.php/Home
// __URL__ //当前控制器News 如: http://www.php.me/homework/TP /index.php/Home/News
// __ACTION__ //当前的方法lister 如: http://www.php.me/homework/TP/index.php/Home/News/lister
// __SELF__ //全部包括传值 如: http://www.php.me/homework/TP/index.php/Home/News/lister
//
// Model.class.php ThinkPHP模型类 常用以下方法
// add()
// save()
// delete()
// select()
// find()
// 条件
// $数据模型类对像->where('条件')
// $数据模型类对像->join() 多表连查
// $数据模型类对像->alias() 给主表取别名
// $数据模型类对像->order() 排序
// $数据模型类对像->group() 分组
// $数据模型类对像->having() 筛选
// $数据模型类对像->limit() 指定拿多少条
// $数据模型类对像->table() 指定表名
// $数据模型类对像->field() 指定字段列表
// 子类封装
// Model类的实例化
// 1.放在Common/Model/XxxModel.class.php
// 2.必须继承Model
// 3.使用命名空间
// 4.方法没有要求
// 5.属性 protected $tableName='';
// 例子:封装一个数据模型类,操作newstype
// Model类的实例化
new \Think\Model('news'); //简写 M('news')// 调用Think的Model类
new \Common\Model\XxxModel();//简写 D('type') 找typeModel//调用自定义类 //如果子类不存在,直接实例化Model(); type当表名使用
//PHP加密
//使用 Zend Guard 收费产品。 一年大约几千元,有试用期5天,5天后过期,先加密,再在服务器端解密.
//修改php.ini配置文件,开启mysqli扩展
extensing=php_mysqli.dll //去掉php.ini前边的分号
extension_dir="D:\phpStudy\php\php-5.5.38\ext"//设置PHP的ext的路径 绝对路径 扩展文件地址
//php.ino中与session相关的配置
session.auot_start=0 //不自自动开启
session.auto_start=1 //自动开启
//设置session的回收时间
session.gc_maxlifetime=1440
//session的保存路径
session.save_path = '/tmp'
//开启gd2的扩展
extension=php_gd2.dll
//创建图片文档类型
//header('Content-Type:image/jpeg');
//创建一张空白图片
//imagecreatetruecolor($width,$height);
//画一个验证码
$img=imagecreatetruecolor(75,25);
//设置颜色
imagecolorallocate($img,$red,$green,$blue);
//画矩形 设置背景色 //$x,$y 左上角标 $x2m,$y2右下角坐标
imagefilledrectangle($img,$x,$y,$x2m,$y2,$coloer;);
//画字符串 //$font 1-5 最小1最大5 $x坐标值 $y坐标值 $string 要画的字
imagestring($img,$font,$x,$y,$string,$color);
//画字线段 //$font 1-5 最小1最大5 $x坐标值 $y坐标值 $string 要画的字
imagestring($img,$x1,$y1,$x2,$y2,$color);
//画点
imagesetpixel($img,$x,$y,$color);
//将图片发送到客户端浏览器
imagejpeg($img);//注意发送图的类型必须与文档类型一致
imagegif($img);//注意发送图的类型必须与文档类型一致
imagepng($img);//注意发送图的类型必须与文档类型一致
//销毁内存中的图片对像
imagedestroy($img)
//目录函数
//创建文件夹 注意:不能创建多级目录
//mkdir('文件夹名');//在当前目录
mkdir('images');//在当前目录
mkdir('images/home');//在指定路径下新建一个文件夹
mkdir('d:/home'); //绝对路径
//删除当前目录下的指定文件夹
//rmdir('images') 注意:只能删除空文件夹
rmdir('images') //移除文件夹 删除文件夹
//获得当前目录
$str=getcwd();//返回字符串 当前目录
//打开目录
$handler=opendir('路径/文件夹');
$str=readdir($handler);
//遍历目录(读取文件夹里的内容)
while($str=readdir($handler)){ //遍历完返回空
echo "{$str}<br/>";
}
//关闭目录
closedir($handler);
//遍历目录
$fileList=scandir('路径/文件夹');
//遍历目录 返回:一维索引数组
$fileList=scandir('d:/phpStudy');//同上面while一样的功能
//显示所有名称
foreach($fileList as $v){
echo $v;
}
//文件函数
//1.创建一个空白文件 touch('路径/文件名');
touch('hello.txt');//当前目录下创建空白文件
touch('php/hello.txt');//相对路径
touch('d:php/hello.txt');//绝对路径
//2.给文件重命名 rename('原名','新名');
//rename($oldname,$newname);
rename('hello.txt','hello.php');//hello.txt改成hello.php
rename('d:/hello.txt','d:/hello.php');//只能改文件名,不能改路径
//3.复制文件copy('原文件','目地文件');
//copy($sourec,$dest);
copy('d:/test.php','e:/test/php');
//4.删除文件
unlink('d:/text.php')//unlink('路径/文件名')
//5.获得文件上次的访问时间 fileatime($filename) 返回:时间戳
$a=fileatime('d:/text.php');//返回:时间戳
//$date=date('日期格式',$a);
$date=date('Y-m-d H:i:s',$a); //时间戳转成时间
//6.获得文件最后一次的修改时间
$a=filectime($filename);//返回时间戳
//$date=date('日期格式',$a);
$date=date('Y-m-d H:i:s',$a); //时间戳转成时间
//7.获得文件大小 filesize('路径/文件名');
filesize('d:/text.php');
//8.获得文件类型
$ftype=filetype('d:/text.php');//返回 file,dir
//9.判断某路径对应的是否为文件
$result=is_file('d:/text.php');//返回 true,false
//10.判断某路径对应的是否为文件夹
$result=is_dir('d:/text.php');//返回 true,false
//11.将相对路径,转为绝对路径
$result=realpath('d:/text.php');
//12.返回整个路径的文件名
$result=basename('d:/text.php');
//13.返回整个路径对应的目录部分
$result=dirname('d:/text.php');
//14.判断文件是否存在
$result=file_exists('d:/text.php');
//文本文件的读写
//1.写入文本文件
file_push_contents('路径','内容');
//2.获得文本文件中的内容
file_get_contents('路径');
//3.将字符串中所有的\n转换为<br/>
$result=n12br('字符串内容');
//正则表达式
//$str='1361301428';
//pattern="/正则表达式/";
$result=preg_match($pattern,$str);
//正则替换字符串里的数字
//$result=preg_replace(正则表达式,'替换成当前字符',需要替换的字符串);
$str='社会主义好9你好456大家好78956';
$pattern="/\d+/";
$result=preg_replace($pattern,'hello',$str);
//通配符
\n 匹配回车符(Windows)
\r 匹配换行符(Linux)
\t 匹配水平制表符(tab键)
\v 匹配垂直制表符(连继换4行)
\f 匹配翻页符
\w 匹配任意字符以及下划线(汉字特殊符号除外)
\W 匹配任意一个非字符(如:特殊符号汉字)
\d 匹配任意0-9数字
\D 匹配任意非数字,(如:除了0-9以外的任意其它字符)
\s 匹配一个空格
\S 匹配一个非空格
. 占位符,代表当前位有一个任意字符
^ 严格开头
$ 严格结尾
//限定符
? 0次或1次
+ 1次或多次
* 0次或多次
{n} 重复n次
{n,} 最少n次,最多无数次
{n,m} 最少n次,最多出现m次
\d{10} 代理当前位置必须要有10个连继的数字
//其它符号
() 提高优先级
[] 综括号内内容多选一//如:[abc] [a-z0-9]
| 或者
//url重写
//Rewrite(Apache的功能) 技术 url重写
//修改httpd:conf配置文件 去掉前面的# 开启url重写功能
//#LoadModule rewrite_module modules/mod_rewrite.so
//找到如下选项 进行修改
<Directory />
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride All //如果是none改为All
Order allow,deny
Allow from all
Require all granted
</Directory>
//找到如下选项 修改开发目的的选项 进行修改
<Directory "D:\phpStudy\WWW">
AllowOverride All //如果是none改为All
Options None
Require all granted
</Directory>
//在项目的根目录下新建一个文件.htaccess(url重写规则文件);
//1)该文件的文件名固定
//2)文件的存放位置固定(必须存放在项目的根目录)
//3)在.htaccess中,编写规则
//---图片水印
//打开指定的图片
//$img=imagecreatefromjpeg('图片路径');
$img=imagecreatefromjpeg('img/a.jpg');
//画字符串
$color=imagecolorallocate($img,255,255,255);
imagestring($img,5,200,100,'www.php.me',$color);
//画字符串02
//imagettftext($img,大小,角度,$x,$y,$color,字体文件,要显示的字符串)
//imagettftext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
imagettftext($img,30,-45,50,100,$color,'font/zw.otf','你好');
//存盘
imagejpeg($img,'保存路径');
//图片缩放
//打开原图
$srcImg=imagecreatefromjpeg('图片地址');//原图
$newImg=imagecreatetruecolor(宽,高);//缩放后的图
$imgsize=getimagesize('img/a.jpg');//获得图片宽高返回数组
print_r($imgsize);
//imagecopyresized(新图,原图,新图_x,新图_y,原图_x,原图_y,新图_宽,新图_高,原图_宽,原图_高) //缩放
imagecopyresized($dst_img,$str_img,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h) //缩放
imagejpeg($newImg,'img/hello.jpg');//存盘
//出现编码的地方
1. 文件存储格式的编码
2. <meta http-equiv='content-type' content='text/html'; charset=utf-8>
3. header("content-type:text/html;charset=utf-8");
4. MySQL本身编码
5. 建库时指定库的编码
//连接数据库流程 mysqli函数库
<?php
header('Content-Type:text/html;charset=utf-8');
//1:连接数据库
$conn=mysqli_connect('127.0.0.1','root','root','ifeng') or die('数据库连接失败');
//2:向mysql发送sql语句
$sql='delete from t_news where nid=1';
$result=mysqli_query($conn,$sql);
//判断是否执行成功
if($result===true){
echo '删除成功';
}else{
echo '删除失败';
};
//4:关闭数据库
// $searr=mysqli_free_result($rs);//如果mysql语句有结果集 释放解果集 返回索引+关联数组
// $searr=mysqli_fetch_assoc($rs) //返回的是关联数组
// $searr=mysqli_fetch_row($rs) //返回的是索引数组
mysqli_close($conn);
phpinfo();//查看PHP信息
//包含文件
include '路径/xxx.php'; //包含的文件有错误,产生一个警告,程序继续执行;
require '路径/xxx.php'; //包含的文件有错误,产生一个至命错误,程序终止执行;
include_once '路径/xxx.php'; //如果包含多次同一文件,页面只包含一次,只有一次生效;
require_once '路径/xxx.php'; //如果包含多次同一文件,页面只包含一次,只有一次生效;
//session
//开启会话 session_start();
session_start();
$id=session_id();
//存取数据
$b=$_SESSION['userName'];
echo $b;
//向cookiek中存储临时性数据 //浏览器关闭cookiek失效
setcookie('address','北京',)
//$变量=$_COOKIE[名]; 获得cookie的值的方法
//setcookie('cookie名','值',到期时间);//
//向cookie中存储数据 //时间到期cookiek失效
setcookie('address','北京',time()+3600*48);//两天后到期
//cookie存数组
$cityList=array('北京','上海','天津','深圳');
setcookie('addr',$cityList);
$arrcook=$_COOKIE['addr'];
//函数
//数组转成字符串,序列化数组
$str=serialize($cityList);
//反序列化 //将字符串还原为数组
$arr=unserialize($str);
//分割字符串
$str='中国-美国-英国-德国';
$arr=explode('-',$str);//返回数组
//返回一个英文字母加数据的字符串,保证调用一万次产生的值不同
uniqid()//产生不同字符串
//面向对像
//类的封装
//属性
//可见性
$属性名称=初值
public //公共类特点:类内部可见,对像可见 可继承
protected //受保护类 特点:类内部可见,对像不可见 可被继承
private //私有类 特点:类内部可见,对像不可见 不能被继承
//方法
//可见性
function 方法名称(参数1,...){
方法体;
return 返回值;
};
//构造函数
1. function __construct(参数1,...){
....
}
2. function 类名5(参数1,...){
....
}
//析构函数
function __destruct(){
....
}
//如:声明一个类
class people{
public $age;
public $height='1.75';
public $weight='55';
//构造函数 实例化类后自动调用
function __construct(参数1,...){
}
function eat($what=''){
if($what='大鱼大肉'){
$this->$weight='90';
}else if($what='素菜'){
$this->$weight='50';
}else{
$this->$weight='45';
}
}
}
//类的自动加载 例如:
function __autoload($className){
//echo 'nihao';
$classFileName=$className.".class.php";
require 'class/'.$classFileName;
};
$ob=new News();
var_dump($ob);
//设置自动加载目录:
set_include_path('路径');//可以设置多个
//先去class目录下找,没找到再去class/public路径下找 例如:
set_include_path('class;class/public');
//linux系统的分割符是冒号":" 不是分号";" 如下
set_include_path('class:class/public');
PATH_SEPARATOR //系统常量 不分系统 如下
set_include_path('class PATH_SEPARATOR class/public');
//继承语法格式 //子类继承父类 只有 protected public可以被继承 private不可以被继承
// 例如: f 为父类 son 为子类
class f{
public $a=1;
protected $b=2;
private $c=3;
private $d=4;
public function fun1(){
echo 'fun1';
}
public function fun2(){
echo $this->a;
}
protected function fun3(){
echo $this->b;
}
function fun4(){
echo $this->d;
}
}
class son extends f{
}
$exobj=new son();
$exobj->fun1;
//抽像类 一个类中有没有方法体的方法,这个方法就叫抽像方法abstract class 类名{}格式如下:
abstract class 类名{
属性:
方法:
抽像方法:
}
//抽像方法 protected 可加可不加
abstract protected function 方法名称(参数1,...){
}
//接口文件写法 例如:
interface News{
/* desc:添加文章
* params: array $arr, example:array('title'=>'cotent')
* return:主键id值
* author:xufangfang-99@163.com
*/
function adNews($arr);
/* 说明:删除文章
* 参数: int $id, example:'15'
* 返回值:主键id值
* 联系方法:xufangfang-99@163.com
*/
function deleteNewsById($id);
}
//继承接口:
class 类名 implements 接口文件名{
}
class News extends Model implements 接口文件名{
}
//魔术方法 名字固定,功能固定
function __construct() //构造函数
function __destruct() //析构函数
//__call(方法名,参数(数组))
__call($funName,$args) //调用一个未定义的方法时__自动被启动
__get($proName)//访问一个未定义的属性时,自动调用
//__set(属性名,属性值)
__set($proName,$proValue)//给一个不存在属性赋值时,自动调用。
__clone()//当对像被克隆时,自动调用
函数:克隆变量
clone(变量)
$a=123;
$b=clone($a);
private function __clone(){//防止克隆
}
__sleep()
__wakeup()
//静态类
class 类名{
可见性为 static 属性名=默认值;
可见性为 static functin 方法名(参数1,..){
....//方法体
$a=123;
//静态类不可以这样访问 $this->静态属性名;
//静态方法可通过下面方法调用
类名::静态属性名;//访问静态属性前面要加$
self::静态属性名;
类名::静态方法名;
self::静态方法名;
return 值;
}
}
//类常量
const 名称=值;
如
圆类
const pi=3.14;
//单例模式 //数据库操作类,需要。
class Test{
private function __construct(){}//防止外部调用NEW实例化,构造函数私有化
protected static $ob='';//初始化单例为空
static function getInstance(){//只能调用一次方法
if(self::$ob==''){
$ob=new Test();
self::$ob=$ob;
return $ob;
}else{
return self::$ob;
}
}
private function __clone(){}
}
Text::getInstance();
//PDO 开启
extension=php_pdo.dll
extension=php_pdo_mysql.dll
//使用PDO
//$ob= new PDO("mysql:host=ip地址;dbname=数据库名称", '用户名', '密码');
$ob=new PDO("mysql:host=localhost;dbname=news", 'root', 'root');
var_dump($ob);
//方法
query(sql语句) //select 返回 object
exec(sql语句) //insert update delet 返回影响的条数
fetch()//获取一条记录
//$arr=$psob->setFetchMode(PDO::FETCH_ASSOC);
fetchAll()//获取所有记录 实例如下
//$arr=$psob->fetch();//fetch 每次取一条,然后指针下移
//$arr=$psob->fetchAll();//fetch 每次取一条,然后指针下移
//指定返回数的类型
setFetchMode();
//PDO常量
PDO::FETCH_ASSOC
PDO::FETCH_NUM
PDO::FETCH_BOTH
PDO::FETCH_OBJECT
//sql语句预处理
$pdoOB->prepare("insert manager(username,password) value('fanny0001','123456')")
$psOb->execute(array('值1','值2','值',...));
//占位符
//?
//:
//sql语句中哪些部分可以使用预处理。
select 字段列表 from 表名 where 字段 运算符 ? and type=?
limit start,length 只能有一个使用占位符
having 字段名 运算符?
insert into 表名 set 字段=?,字段=?,...where 字段 运算符?
update 表名 set num=num+1 where 字段 运算符 ?
delete from 表名 where 字段 运算符 ?
//smarty模板
//自定义函数实现字符串截取,支持中文
$str //被截取的字符串
$charNum //截取长度
$charset //中文字符的编码
function mysubstr($str,$charNum,$charset='utf-8'){
if(mb_strlen($str,$charset)<=$charNum){
return $str;
}else{
return mb_substr($str,0,$charNum,$charset).'...';
}
}
//注册函数 把主程序中的函数,注册到模板上使用
//1.在主程中声明函数
//2.如何注册:
$smarty->registerPlugin('function','模板上函数名称','被注册的函数名');
//缓存
//1.修改配置文件
$smarty->caching=true;//开启缓存
$smarty->cache_lifetime=300;//指定缓存时间 单位:300秒
//2.生成缓存
$smarty->isCached('path','cacheid');//判断缓存是否存在及合法
$smarty->display('path','cacheid');//生成缓存,拿缓存,显示
<{nocache}> <{$t}> <{/nocache}>//局部不缓存
//静态化
//1.获取详细页运行的结果
$content=$smarty->fetch('path');//返回文本字符串
//TP thinkPHP框架
//指定应用目录
define('APP_PATH','./Application/');
//加载入口文件
require './ThinkPHP/ThinkPHP.php';
//url地规则
// index.php?m=模块名&c=控制器&a=方法名&... //普通模式
// index.php/模块名/控制器名/方法名/id/值... //PathInfo模式
// index.php?s=/模块名/控制器名/方法名/id/值... //兼容模式
// .haccess文件中正则表达式决定 rewrite模式
// 如: /news_detail_id_1.html 浏览器解析 index.php/home/news/detail/id/1
// $this->assign('s',$str); 变量传给模板
// $this->error("提示信息","目标地址");失败提示
// $this->seccess("提示信息","目标地址");成功提示
// $this->display();显示模板
// U("模块/控制器/方法",array('名'=>值,...));
// redirect(); //实现跳转,没有提示
// __PUBLIC__ 模板标签用来生成Public目录对应的url地址
// 后台访问控制
// 在LoginController/check中创建会话变量
// 在NewsController/add,oper中判断
//
// V模板的编写
// 普通变量标签 {$名称}
// 数据变量标答 {$数组名.下标} {$数组名['下标']}
// 数组遍历标签 <foreach name='数组名' item='v' key='k'> {$v.下标} </foreach>
// 条件标签 <if condition="$a "> </if> 标签符不要定界签{}
// <if condition="$a "> <else/> </if>
// <if condition="$a "> <elseif condition="条件"> <else/> </if>
// 说明条件中不能使用< > 大于号 >,小于号 < 因为和定界符冲突
// eq 等于
// gt 大于
// lt 小于
// egt 大于等于
// elt 小于等于
// neq 不等于
// heq 恒等于
// nheq 不恒等于
// 变量调节器
// {$被调节变量|函数=参数1,函数=参数2,###...}//
// {$Think.const.常量名称}常量的使用
// {$Think.get.名称}
// {$Think.cookie.下标}
// {$Think.request.下标}//即能接收get传值也能接收post传值
// {$Think.session.下标}
// 公共模板加载
// <include file='path'>
// path写法:
// ./Application/Home/View/Public/header.html
// 控制器名:方法名
// Public:header
// 模板上路径标签:
// 例如路径: http://www.php.me/homework/TP/index.php/Home/News/lister
// __PUBLIC__ 静态资源的路径 js css image
// ure地址,a连接
// __ROOT__ //当前的根路径 如: http://www.php.me/homework/TP
// __APP__ //当前Think入口文件index.php 如: http://www.php.me/homework/TP/index.php
// __MODULE__ //当前模块Home 如: http://www.php.me/homework/TP/index.php/Home
// __URL__ //当前控制器News 如: http://www.php.me/homework/TP /index.php/Home/News
// __ACTION__ //当前的方法lister 如: http://www.php.me/homework/TP/index.php/Home/News/lister
// __SELF__ //全部包括传值 如: http://www.php.me/homework/TP/index.php/Home/News/lister
//
// Model.class.php ThinkPHP模型类 常用以下方法
// add()
// save()
// delete()
// select()
// find()
// 条件
// $数据模型类对像->where('条件')
// $数据模型类对像->join() 多表连查
// $数据模型类对像->alias() 给主表取别名
// $数据模型类对像->order() 排序
// $数据模型类对像->group() 分组
// $数据模型类对像->having() 筛选
// $数据模型类对像->limit() 指定拿多少条
// $数据模型类对像->table() 指定表名
// $数据模型类对像->field() 指定字段列表
// 子类封装
// Model类的实例化
// 1.放在Common/Model/XxxModel.class.php
// 2.必须继承Model
// 3.使用命名空间
// 4.方法没有要求
// 5.属性 protected $tableName='';
// 例子:封装一个数据模型类,操作newstype
// Model类的实例化
new \Think\Model('news'); //简写 M('news')// 调用Think的Model类
new \Common\Model\XxxModel();//简写 D('type') 找typeModel//调用自定义类 //如果子类不存在,直接实例化Model(); type当表名使用
//PHP加密
//使用 Zend Guard 收费产品。 一年大约几千元,有试用期5天,5天后过期,先加密,再在服务器端解密.