PHP随记及解惑汇总
1.php字符串长度函数:strlen($str); 1个中文字符串算3个长度 英文和数字算1个
mb_strlen();中英文字符都算长度1 详细说明>>
-----------
2.PHP substr() 函数:
substr(string,start,length)
string 必需。规定要返回其中一部分的字符串。
start 必需。规定在字符串的何处开始。
正数 - 在字符串的指定位置开始
负数 - 在从字符串结尾的指定位置开始
0 - 在字符串中的第一个字符处开始
charlist 可选。规定要返回的字符串长度。默认是直到字符串的结尾。
正数 - 从 start 参数所在的位置返回
负数 - 从字符串末端返回
Substring($start,$len); //$start从哪开始 $len截取的长度
-------------
$page = new Index (); //Index是页面类名
$page->startUp (); //表示运行页面
-----------
3.跳转:
header("url"); //跳转redirect作用 执行完毕后,后面的代码也会继续执行,故需在该语句后加die结束
header('location:/Login.php'); exit();
----------
字符串+字符串变量输出:
<?php $var ="Hello World"; echo "测试内容是:{$var}<br />"; //只能是双引号,如果是单引号则无法解析 echo "测试内容是:".$var."<br />"; ?>
正常双引号结果:
测试内容是:Hello World
测试内容是:Hello World
单引号结果:
测试内容是:{$var} 测试内容是:Hello World
4.PHP中大括号{}的作用:
1.在字符串和字符串变量混合使用是一般用大括号:可以消除歧义,也更直观。
2.和双引号同时使用。echo "a{$var}"; 如果是单引号则无法解析
3.$array{0} 表示数组的第一个元素
5.Zend Studio下Smarty模板.tpl文件中写判断语句:
{if 条件}
html代码...
{else}
html代码...
{/if}
PHP中没有方法重载的概念,写方法的时候可以设置最多参数的方法,部分参数设置为默认值:
public function __construct($allowAnonymous = false, $loginPage, $allowCache = false, $cacheLifetime = -1)
6.PHP循环输出List:
$GroupLists = $rLists->returnObject; if (isset($GroupLists)&& count($GroupLists)) { foreach ($GroupLists as $item) { $this->groupHtml=$this->groupHtml. "<tr id='groupitem_{$item->GroupId}'". "</tr>"; } }
7.字符串格式化函数sprintf(),类似c#中的string.Format();
$text1 ="变量内容1"; $text2 ="变量内容2"; var $str = sprintf("字符串%s内容%s",$text1,$text2);
8.PHP中的引用传递:
1.方法的定义时参数需要用&符号表示
public function test1($str,&$outVal)
{
}
2.调用方法时的参数不要加上&不然会报错:
调用:
test1($str,$outVal);
如果调用加上了$就会报错:test1($str,&$outVal); //这样会报错
9. PHP与.NET求两个日期时间的秒数差:时间格式:2014/1/1 12:30:02
PHP中时间相减得到的就是秒数差:
<?php $dtStart = new \DateTime('1970-01-01'); $dtNow = time(); $toNow = $dtNow - $dtStart; echo $toNow; ?>
.NET中的写法:
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); DateTime dtNow = DateTime.Parse(DateTime.Now.ToString()); TimeSpan toNow = dtNow.Subtract(dtStart); string timeStamp = toNow.Ticks.ToString(); //这里得到的是纳秒 timeStamp = timeStamp.Substring(0, timeStamp.Length - 7); //纳秒转换为秒
10.PHP中$this与self的区别:
self指类本身,$this指实体本身。$this不能引用静态成员和常量。
11.MD5加密函数: md5($str);
12.如何不让php自动把×换成×号?
出错的情况:<a href="temp.php?t=1×=2">click</a>
正确的情况:<a href="temp.php?t=1&times=2">click</a>
原因:PHP会将×解析为x
解决办法:&改为&
13.请求URL获取返回数据:file_get_contents($url); 详细>>
14.PHP与C#正则写法的差异:
C#写法:@"0*[0-9][0-9]*$"
PHP写法:/0*[0-9][0-9]*$/ 在首尾需要加上/,在最后加上i则表示不区分大小写:/0*[0-9][0-9]*$/i
15.字符串补全:
str_pad($returnValue, 8,'0',STR_PAD_LEFT); //字符串在左边补全,一共8位 如:$returnValue=2 则返回结果为:00000002 更多>>
C#的写法:str.ToString().PadLeft(8, '0');
16.解析指定url: 可获取相对地址、hosts、协议等 详细>> 更多>>
print_r(parse_url('http://wdot.cc/Attack/90.html')); 运行结果: Array ( [scheme] => http [host] => wdot.cc [path] => /Attack/90.html //array['path'] 获取相对地址 )
17.PHP时间比较:
时间戳比较:
$sjc= strtotime('+'.$mintes.' minute',strtotime($validCodeInfo->ValidTime));
$sjc > time();
时间格式比较:
date("Y-m-d H:i:s",strtotime('+'.$mintes.' minute',strtotime($validCodeInfo->ValidTime)))<date("Y-m-d H:i:s",time())
18.PHP的内存限制
PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
http://blog.csdn.net/beyondlpf/article/details/7794028
19.在原有基础上增加时间:
http://blog.sina.com.cn/s/blog_ad3e2f7d01017nkv.html
20.结束页面输出,类似ASP.NET中的Response.End()
exit();或die();
21.Url编码:urlencode();
22.取2个数中最大数,或从数组中获取最大的值:max(val1,val2); max(arry);
23.PHP与HTML混编:
html代码...
<?php
php代码部分
?>
html代码...
简单示例:
<?php $expression = 0; if ($expression) { ?> <strong>This is true.</strong> <?php } else { ?> <strong>This is false.</strong> <?php } ?>
用Smarty模板技术,tpl文件写法:(不知道不用smarty这样写行不行,感觉应该不行)
{if $a=1} //注意:if后面不能有括号(),if($a=1)是错误写法
html代码...
{elseif $a=2} //elseif连起来的 不能分开, else if不对
html代码... {else} html代码... {/if}
24.获取url的host部分:
$_SERVER['HTTP_HOST']
25.PHP中表示int的最大值:
26.PHP中POST表单:
27.PHP对字符串进行Html编码:htmlspecialchars() 相对于.net中的HtmlEncode()
常见错误:
1.错误信息:Namespace declaration statement has to be the very first statement in the script
一般是文件Bom头引起的,解决办法:找到文件用notepad++打开 - 格式 - 已Utf-8格式无Bom格式编码
2.错误信息:PCRE does not support
preg_match("/^[\u4e00-\u9fa5]{1,4}$/",$str);
应该改为
preg_match("/^[\x{4e00}-\x{9fa5}]{1,4}$/u",$str); //如果十六进制全部字母形式则为preg_match("/^[\xFFF-\xFFF/u",$str); 详细>>
3.报错:Warning: preg_match(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u
解决办法:
将 /[^0-9A-Za-z\u4e00-\u9fff]+/ 改为 /[^0-9A-Za-z\x{4e00}-\x{9fff}]+/u
原因:
如果当前文件是unicode编码则用 /[^0-9A-Za-z\u4e00-\u9fff] 去匹配部分指定汉字或字符
如果当前文件是utf-8编码则用 /[^0-9A-Za-z\x{4e00}-\x{9fff}]+/u (我从.net文件改版成php的,php编码是utf-8的)

浙公网安备 33010602011771号