1、使用绝对路径载入文件
defined(‘ROOT’,pathinfo(__FILE__,PATHINFO_DIRNAME));
require(ROOT.’/test.php’);
2、写入文件前,检查目录写权限
一般代码:
$content=”please input text”;
$path=’e:/text/text.txt’;
file_put_contents($path,$content);
存在问题:写或保存文件前,确保目录是可写的,假如不可写输出
错误信息,在linux系统中,需要处理权限,目录权限不当会导致
很多问题,文件也有可能无法读取。
1、父目录不存在
2、目录存在,但文件不可写
3、文件被写锁住
改良代码:
$content=’please input text’;
$dir=’e:/text’;
$path=$dir.’/text.txt’;
if(is_writable($dir)){
file_put_contents($path,$contents);
}else{
die(‘文件夹有权限设置或者文件不可写’);
}
3、不要依赖submit按钮值来检查表单的提交行为
一般代码:
if($_POST[‘submit’]==’Save’){
//other thing
}
上诉情况大多数情况下正确,除了应用多语言,save可能代表其他含义,不好区分它们所以不要依赖它们
改良代码:
if($_SERVER[‘REQUEST_METHOD’]==’POST’&&isset($_POST[‘submit’])){
//other thing
}
4、生成唯一的id 使用函数uniqid()2013/9/1
5、array_fill($start_index,$num,$mix)数组填充
6、mt_rand()与rand() 区别:mt_rand()比rand()产生随机数的平均速度快4倍
8、比较常见的输出:echo,var_dump(),var_export()
9、所有魔术方法:__construct() __destruct() __clone() __set() __get() __isset() __unset() __call() __invoke()
10、reset系列相关函数;reset()将数组的内部指针移动到第一个位置 current() key() next()
11、OOP关键字:class final instanceof private protected public static abstruct interface extends implements :: ->
12、查询出当天的记录: select * from notes where to_days(notes_date)=to_days(now()); to_days()为Mysql时间函数用于计算出天数
13、查询出昨天的记录: select * from notes where (TO_DAYS(NOW())-TO_DAYS(notes_date)) BETWEEN 1 and 2;
14、查询出近7天的记录:select * from notes where DATE_SUB(CURDATE(),INTERVAL 7 DAY)
17、查询出上月的记录:select * from notes where PERIOD_DIFF(DATE_FORMAT(CURDATE(),’%Y%m’),DATE_FORMAT(notes_date,’%Y%m’))=1; period_diff(P1,P2)返回P1到P2之间的月份间隔数,其中P1和P2的格式是YYYYMM或YYMM
18、在MVC中所有的相对路径参照路径都是入口文件 index.php
19、if(array_key_exists($key,get_object_vars($this))) 在一个类中中判断传入的参数键是否在类中
20、 __DIR__:动态得到当前文件所在目录
dirname()得到上级目录 dirname(__DIR__) 动态得到当前文件所在目录的上级目录
__FILE__ 动态得到包含文件名的全路径
basename(__FILE__) 动态得到文件名 包括后面的地址栏参数 如 test.php?a=method&id=2
21、读取txt文件中的信息并以换行为分隔存入到数组中: $txt=file_get_contents(text.txt); $string_arr=explode(“
”,$txt) //此处的分隔符必须要是双引号将
包含起来,不能使用单引号
22、使用fread读取文件内容
function get_file_content($file_path){
$fp=fopen($file_path, ‘r’);
$data=”;
while (!feof($fp)){ //feof($handle) 其中feof表示测试文件指针是否已经到了文件结束的位置
$data.=fread($fp, 1024);
}
fclose($fp);
return $data;
}
23、ini_set()函数的使用
24、array_unique(array) 移除数组中重复的项
25、strlen()与mb_strlen()的区别:
都是用于获取字符串的长度,其中str_len只针对单字节编码字符,也就是说它计算的是字符串的总字节数。如果是多字节编码如gbk和utf8,使用
strlen得到的不是字符的个数而是该字符的总字节数。可以使用mb_strlen获取其字符个数,使用mb_strlen要注意两点,一是要开启mbstring扩展
二是指定字符集
$str=”传智播客php学院”;
echo strlen($str); //21 当前页面是utf-8字符集,如果是gbk则结果为15
echo mb_strlen($str); //21 注意 未指定字符集,使用内部字符编码(单字节)
echo mb_strlen($str,’utf8′); //9
自定义函数取得带有中文字符字符串的长度
function strlen_utf8($str){
$arr=preg_split(‘//u’,$str,-1,PREG_SPLIT_NO_EMPTY);
$len=count($arr);
return $len;
}
26、截取带有中文的字符串使用 mb_substr()
$str=”你好我叫shushu”;
echo mb_substr($str,2,3,’utf8′);
function substr_utf8($str,$start,$len){
return implode(“”,array_slice(preg_split(‘//u’,$str,-1,PREG_SPLIT_NO_EMPTY),$start,$len));
}
27、post与get传输的最大容量分别是: post根据php.ini文件的配置 post_max_size(默认为8M),get则是2k
28、打印前一天的时间
strtotime()函数用于将英文文本的日期时间转换为unix时间戳
得到前一天的时间:date(“Y-m-d H:i:s”,strtotime(“-1 day”));
得到上一个月的时间:date(“Y-m-d H:i:s”,strtotime(“-1 Month”))
得到上一年的时间:date(“Y-m-d H:i:s”,strtotime(“-1 Year”)) 或者 date(“Y-m-d H:i:s”,strtotime(“last year”))
得到上一周的时间:date(“Y-m-d H:i:s”,strtotime(“-1 Week”));
计算2009-4-1与200-3-2两个日期之差:(strtotime(‘2009-4-1’)-strtotime(‘2009-3-2’))/(3600*24) 得到两个日期的日期之差
29、list()函数,把一个数组中的值赋给list中的变量 如 list($a,$b)=array(‘xiaozhang’,’xiaoli’);
30、implode()将数组转成字符串 emplode()将字符串转为数组
31、preg_split()函数:使用正则表达式对字符串分割
语法:preg_split(正则表达式,分割的字符串,limit,$flag) 其中limit为-1表示没有限制,$flag为PREG_SPLIT_NO_EMPTY表示只返回非空部分
preg_split(‘//u’,$str,-1,PREG_SPLIT_NO_EMPTY);
32、array_slice() 从数组中取出一段来 语法:array_slice(array,offset,[length])
33、trim() 消去字符串左右两边的空格 语法:trim($string,【$charlist】) 如果$charlist缺省则消去$string中左右两边的空格,如果存在$charlist则消去
$string中两边的$charlist 如 trim(‘ashushusdsa’,’a’) 得到的结果是字符串 :shushusds 消掉了左右两边的a
其他两个函数 ltrim()与rtrim()分别表示消掉左边和消掉右边的指定字符串
34、何为可变变量:获取一个变量的值作为这个可变变量的变量名
35、以Apache模块的方式安装PHP,在文件http.conf中首先要用语句:LoadModule php5_module ‘c:/php/php5apache2.dll’; 动态加载PHP模块
然后再用语句 AddType application/x-httpd-php .php 使得Apache把所有扩展名为php的文件都作为PHP脚本处理。
36、取得一个全路径的文件的扩展名的五种方法
//取得一个全路径的扩展名
//方法1
function getExtension1($file_path){
//对文件地址做分割得到数组
$path_arr=preg_split(‘/\./’, $file_path,-1,PREG_SPLIT_NO_EMPTY);
return $path_arr[(count($path_arr)-1)];
}
//方法2
function getExtension2($file_path){
return pathinfo($file_path,PATHINFO_EXTENSION);
}
//方法3
function getExtension3($file_path){
$arr=explode(‘/’, $file_path);
$file_arr=$arr[(count($arr)-1)];
$extension=explode(‘.’, $file_arr);
return $extension[(count($extension)-1)];
}
//方法4
function getExtension4($file_path){
$extension=strrchr($file_path,’.’);
return ltrim($extension,’.’);
}
//方法5
function getExtension5($file_path){
return substr($file_path, strrpos($file_path, ‘.’)+1);
}
echo getExtension4(‘f:/test/a.txt’);
37、intval() 函数 将’101′ 转换为101
38、将open_dir转换为OpenDir
function get($string){
$arr=explode(‘_’,$string);
$txt=array_map(‘toUpper’,$arr); //使用array_map()函数使得数组$arr中的每一个元素都调用了函数toUpper
return implode(”,$txt);
}
function toUpper($word){
$word{0}=strtoupper($word{0});
return $word;
}
39、array_map(callback,array) 该函数的作用是使得数组array中的每一个元素都回调callback函数
40、统计字符在字符串中出现的次数:substr_count(字符串,字符)
//自定义函数:统计字符在字符串中出现的次数
function getNumber($string,$key){
$arr=explode($key, $string);
return count($arr)-1;
}
echo getNumber(‘gsggsgsgs’,’g’);
41、ord($string)函数 得到字符串中的ASCII码 如:0-9为48-57 只能得到第一个字符的ASCII值
42、删掉一个网站的所有的Cookie $_SERVER[‘HTTP_COOKIE’]得到包含所有cookie的字符串 每个cookie之间用”;”分割 如:name1=xiaozhang; name2=xiaoli; name4=xiaoluo
//删除一个站点的所有cookie
$cookie_str=$_SERVER[‘HTTP_COOKIE’];
$cookies_arr=explode(‘;’, $cookie_str);
foreach ($cookies_arr as $cookie){
$cookie_arr=explode(‘=’, $cookie); //得到cookie名与cookie值的数组
$cookie_name=trim($cookie_arr[0]); //必须要使用trim()去掉两边的空格
//echo $cookie_name;
setcookie($cookie_name,”,time()-3600);
}
43、chr()
44、ini_get()取得PHP配置信息 ini_set() 配置PHP
45、extract()
46、str_repeat(字符串,次数) 该函数是重复的产生字符串 如:$str=str_repeat(‘=’,10) 将生成10个“=”的字符串
47、mysql_real_escape_string() 转义SQL中的特殊字符
48、fsockopen()
49、feof()
50、header()
51、header_set()
if(!header_set()){
header(‘
Location:http://www.baidu.com’);
exit;
}
52、gmdate()
53、glob()
54、serialize() 对数据序列化
55、unserialize() 对数据反序列化
56、json_decode() 对json格式的数据进行编码
57、json_encode() 对变量进行json编码
58、unlink()删掉文件
59、finfo_open() finfo_file()
60、ucfrist() 将字符串的首字母大写,主要应用于MVC地址栏中平台与模块的单词拼写
61、addslashes()
62、get_magic_quotes_gpc()
63、array_walk_recursive()
64、IE8下不支持html5标签的解决方法:使用javascript:document.createElement(‘header’); 这句话即告诉IE8说header为一个标签
65、array_unique(array) 消除数组中重复的项,返回为一个数组
66、js事件监听:
IE:
attachEvent(type,callback) type:事件名 如 onclick,callback:事件处理程序
基于w3c模型的浏览器(firefox 等):
addEventListener(type,callback,flag) type:事件名 如click 注意,这里没有on前缀 callback:事件处理程序 flag: 使用捕捉模型还是冒泡模型 默认:冒泡
解决浏览器兼容性问题:
/**
* obj: 为操作的dom对象
* type: 事件名 不加 ‘on’
* callback: 事件处理程序
function addEvent(obj,type,callback){
if(window.addEventListener){
obj.addEventListener(type,callback);
}else{
obj.attachEvent(‘on’+type,callback);
}
}
67、js中的event对象 window.event 只支持IE浏览器 火狐等W3C浏览器不支持 这个需要在匿名函数中传递event对象 function(event){}
如:
obj.onclick=function(event){
var evt;
if(window.event){
//解决IE浏览器问题
evt=window.event;
}else{
//解决w3c浏览器问题
evt=event;
}
}
68、js中的事件冒泡机制与阻止冒泡:
div1
div2
div3
阻止冒泡发生
function stopBubble(evt){
if(window.event){
//ie浏览器
window.event.cancelBubble=true;
}else{
evt.stopPropagation();
}
}
69、js 字符串的search方法:var str=’try you best’; str.search(‘tr’) 说明:该方法是查找字符串中匹配的字符
如果找到则返回索引位置,如果没有找到则返回-1 实例:js版搜索功能
70、多关键字的搜索功能实现原理:将关键字字符串使用空格分隔,得到数组,再for遍历逐个匹配即可 如 str=’中国 中华 台湾’ 使用空格分隔成数组再使用 for遍历
搜索实例:
$link=mysql_connect(‘localhost’,’root’,’123456′);
mysql_select_db(‘myshop’);
mysql_query(‘set names utf8’);
$str=’诺基亚 三星’;
$arr=explode(‘ ‘, $str);
$sql=”select goods_name from ecs_goods where”;
for ($i=0;$i
$sql .=” goods_name like ‘%$key%’ or”;
}
$sql=substr($sql, 0,-2);
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)){
var_dump($row);
}
71、背景透明度兼容性:IE:filter:alpha(opacity:30) 火狐、谷歌:opacity:0.3
72、元素的:offsetLeft、offsetRight、offsetWidth、offsetHeight 的使用
73、JS中根据类名筛选出指定的元素 通用方法
function getByClass(oParent,sClass){
var aEle=
oParent.getElementsByTagName(‘*’); //从父节点中得到所有的子节点,其中通配符“*”表示匹配所有的元素
var aResult=[];
for(var i=0;iif(aEle[i].className==sClass){
aResult.push(aEle[i]);
}
}
return aResult;
}
74、JS中判断变量数据类型使用 typeof 例如:typeof str.match(book) 得到的是一个object类型(数组对象模型)
75、php中的析构函数,销毁对象的顺序是先定义的销毁,原因是对象是名是存在于栈中,栈是先进后出的,名存放在栈中,地址存放在堆中
变量存放在栈中,因为栈有着 小、快的特点,便于变量的反复的读取和写入。堆相对于栈而言,有着大、慢的特点
所谓的引用传递是指:两个栈中的对象指向堆中的同一个内存
按值传递:普通变量
按地址传递:对象变量
76、JS正则表达式的分组匹配
var pattern=/^([a-z]+)\s([0-9]{4})$/i;
var str=’google 2012′;
// alert(pattern.exec(str));
alert(pattern.exec(str)[0]); //得到的是全部字符
alert(pattern.exec(str)[1]); //得到的是第一个分组匹配成功的字符
alert(pattern.exec(str)[2]); //得到的是第二个分组匹配成功的字符
77、JS中对象的方法就是属性
function Person(p_name){
this.name=p_name;
}
var p1=new Person(‘xoazhang’);
p1.say=function(){
alert(‘我叫’+this.name);
}
78、JS中检测数据类型:使用 typeof和 instanceof constructor
两者的区别:typeof运算符在检查基本数据类型的时候非常好用,但检测引用类型的时候,它就不是那么好用
通常,我们并不想知道它是不是对象,而是想知道它到底是什么类型的对象。因为数组也是object,null也是object
所以此时,你需要使用 instanceof 运算符来查看
//检查数据类型,使用 typeof 和 instanceof
var str=new String(‘test’);
alert(typeof str);
var arr=new Array();
alert(typeof null);
alert(str instanceof String);
alert(arr instanceof Array); //是否是数组
var obj={};
alert(obj instanceof Object); //是否是对象
var reg=/g/;
alert(reg instanceof RegExp); //是否是正则表达式
function Person(){ }
var person=new Person();
alert(person.constructor); //得到person对象的构造器
79、解决JS只能取得行内样式的值,而不能取得外联文件样式的值的方法
if(typeof window.getComputedStyle!=’undefined’){
//支持w3c浏览器
return window.getComputedStyle(this,null)[attr]; //其中this表示当前元素对象
}else if(typeof this.currentStyle!=’undefined’){
//支持IE
return this.currentStyle[attr];
}
80、状态码的问题
81、使用正则表达式将页面中的js脚本清除掉
var str=’2323
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 ZLME@ZLME.COM 举报,一经查实,立刻删除。
留言与评论(共有 0 条评论) |