回家几天中自己积累的PHP常用知识

 
1, 语句脱离:
<?php echo "hello world"?>和
<?php
Echo “hello world”;
?>是等价的。
 
2, print_r的使用:
$a=array(1,2,”hello”,array(“one”,”two”));
则 print_r($a);后输出:
Array ( [0] => 1 [1] => 2 [2] => hello [3] => Array ( [0] => one [1] => two ) )
能够很清楚的看明白数组 a的结构。
 
3, var_dump 列出变量资讯
<?php
$phone="113";
var_dump(ereg("113",$phone));
?>
 
4
<?php
echo <<< nol
hello world!
nol;
?>
Nol 只是一个样式,可以换为其它字母或中文,但NOL;必须顶格写。  
 
5
用PHP处理多个同名复选框
如果一个表单中有多个同名复选框,在提交到php时却只有一个值,而并不像asp那样是一串用逗号分割的值。解决的方法是利用数组。将复选框的name后面加上[],例如:<input type="checkbox" name="pp" value="1"> 改为:<input type="checkbox" name="pp[]" value="1">。这样php将得到一个叫pp的阵列。在提交的表单中先用Count(pp)来判断数组的个数即选中的个数,然后对数组进行分别处理就行了。
同样的道理也适应于处理下拉框的多选问题。
 
6, 点击浏览器的后退按钮后,所有字段的信息都被清空了?
这是由于你在你的表单提交页面中使用了 session_start 函数。该函数会强制当前页面不被缓存。解决办法为,在你的 Session_start 函数后加入 header("Cache-control: private"); 注意在本行之前你的PHP程序不能有任何输出。

补充:还有基于session的解决方法,在session_start前加上
session_cache_limiter('nocache');// 清空表单
session_cache_limiter('private'); //不清空表单,只在session生效期间
session_cache_limiter('public'); //不清空表单,如同没使用session一般

可以在session_start();前加session_cache_limiter("private,max-age=10800");
 
7 ,E-mail的正确判断
eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+.)+[a-zA-Z]$",$email);
 
8 ,file_get_contents($filename):将文件的内容全部读到出
注意,$filename必须为真实路径,可用realpath()返回路径。
例:
<?php
$file=”me.php”;
$file=realpath($file);
$con=file_get_contents($file);
?>
 
File_put_contents($con,$file);
同样$file要为真实路径。
例:
<?
$con=”hello”;
$file=”me.php”;
$file=realpath($file);
$re=file_put_contents($con,$file);
?>
 
9,array_map: 将函数作用有数组的每一个单元上
例:
<?php
function cube ( $n )
{
    return(
$n * $n * $n );
}

$a = array( 1 , 2 , 3 , 4 , 5 );
$b = array_map ( "cube" , $a );
print_r ( $b );
?>
    用法2:重新建一个数组
    例:
<?php
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");

$d = array_map(null, $a, $b, $c);
print_r($d);
?>
 
10 ,strtr($str,$from,$to)转换字符串中的某些单元
例:
<?php
$a=hello world!!!;
Echo strtr($a,”hello”,”hi”);// 转换为:hillo world!!!
?>
 
<?php
$a=hello world!!!;
$b=array(”hello”=>”hi”);
Echo strtr($a,$b); // 转换为:hi world!!! (与上例不同)
?>
 
11,strpos($string,$str);    // 在$string中寻$str字符若有则返回其所在位置
<?php
$str=”hello world!”;
Echo strpos($str,”w”);
?>
    12 ,implode(“str”,$array);    将$array数组用str字符串连接起来
    例:
    <?php
    $a=array(“hello”,”world”,”!!!”);
    Echo implode(“ ”,$a);
?>
13,split(partten,string) 以正规表达式将字符串切开
例:
<?php
date = "04/30/1973";      // Delimiters may be slash, dot, or hyphen    list ($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br>/n";
?>
14 ,fopen($filepath,”a”);以只写的方式打开文件,置文件的指针于文件尾,若文件不存在,试图创建之。(在me.txt中追加内容)
例:
<?php
$file=”me.txt”;
$handle=fopen($file,”a”);
$content=”hello world!!!”;
If(fwrite($handle,$content))
{
    Echo “Write to me.txt OK!”;
}
Else echo “Write to me.txt BAD!”;
?>
15 ,range(int low,int high)建立一个整数数组
例:
<?php
$arr=range(1,3);
Print_r($arr);// 输出:array([0]=>1,[1]=>2,[2]=>3)
?>
16, htmlspecialchars(str) 将str字符串中的html标记置换
例:
<?php
$a=”<h1>hello world</h1>”;
Echo htmlspecialchars($a);   // 输出:<h1>hello world</h1>
?>
17 ,用Apache后,主页出现乱码

方法一:
AddDefaultCharset ISO-8859-1 改为 AddDefaultCharset off

方法二:
AddDefaultCharset GB2312
 
18, $_SERVER['HTTP_REFERER'] 。得到的数据是传递到本页面的上一页面的 UTL 地址
用途: 1 、防止盗连,比如我是个下载软件的网站,在下载页面我先用referer来判断上一页面是不是自己网站,如果不是,说明有人盗连了你的下载地址。
2 、电子商务网站的安全,我在提交信用卡等重要信息的页面用referer来判断上一页是不是自己的网站,如果不是,可能是黑客用自己写的一个表单,来提交,为了能跳过你上一页里的javascript的验证等目的。
19,nl2br转换新行为html中的<br>
从数据库中得到数据后,用ehco nl2br($row[‘content’]);
 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值