首页 >后端开发 >php教程 >PHP经验分享:常用的小技巧_PHP教程

PHP经验分享:常用的小技巧_PHP教程

WBOY
WBOY原创
2016-07-20 10:57:461121浏览

PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。本文总结了PHP日常开发中常用的8个小技巧。

1、命名

<ol class="dp-c"><li class="alt"><span><span><input><span class="string">'checkbox'</span><span> name=</span><span class="string">'checkbox[]'</span><span> value=</span><span class="vars">$dwmyrow</span><span>[banzhu] /> </span></span></span></li></ol>

2、使用

当计划当作sql指令的一部分时:如果参与控制的字段是数值型的,则

<ol class="dp-c">
<li class="alt"><span><span class="keyword">if</span><span>(! </span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$_POST</span><span>[</span><span class="string">'checkbox'</span><span>])) {  </span></span></li>
<li>
<span class="vars">$expr</span><span> = join(</span><span class="string">","</span><span>, </span><span class="vars">$_POST</span><span>[</span><span class="string">'checkbox'</span><span>]);  </span>
</li>
<li class="alt">
<span class="vars">$sql</span><span> = </span><span class="string">"select * from tbl_name where field in ($expr)"</span><span>;  </span>
</li>
<li><span>} </span></li>
</ol>

如果参与控制的字段是数值型的,则

<ol class="dp-c">
<li class="alt"><span><span class="keyword">if</span><span>(! </span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$_POST</span><span>[</span><span class="string">'checkbox'</span><span>])) {  </span></span></li>
<li>
<span class="vars">$expr</span><span> = </span><span class="string">"'"</span><span>.join(</span><span class="string">"','"</span><span>, </span><span class="vars">$_POST</span><span>['checkbox']).</span><span class="string">"."</span><span>;  </span>
</li>
<li class="alt">
<span class="vars">$sql</span><span> = </span><span class="string">"select * from tbl_name where field in ($expr)"</span><span>;  </span>
</li>
<li><span>}  </span></li>
</ol>

PHP判断Form表单是否提交

<ol class="dp-c">
<li class="alt"><span><span class="vars">$action</span><span>=</span><span class="vars">$HTTP_POST_VARS</span><span>[</span><span class="string">"Button1"</span><span>];  </span></span></li>
<li>
<span class="keyword">if</span><span>(</span><span class="vars">$action</span><span>==</span><span class="string">"提交"</span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span class="comment">//执行表单操作 </span><span> </span>
</li>
<li class="alt"><span>}  </span></li>
<li>
<span class="keyword">else</span><span> </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span class="comment">//读取默认值 </span><span> </span>
</li>
<li class="alt"><span>}  </span></li>
</ol>

PHP 获取字符串长度

<ol class="dp-c"><li class="alt"><span><span class="func">strlen</span><span>(</span><span class="vars">$myrow</span><span>[1]) </span></span></li></ol>

PHP Url转向

<ol class="dp-c"><li class="alt"><span><span>Header(</span><span class="string">"Location: "</span><span>.</span><span class="vars">$_SERVER</span><span>[</span><span class="string">"HTTP_REFERER"</span><span>]);  </span></span></li></ol>

PHP超全局对象

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="vars">$a</span><span> = 1;  </span>
</li>
<li class="alt">
<span class="vars">$b</span><span> = 2;  </span>
</li>
<li>
<span class="keyword">function</span><span> Sum()  </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span class="vars">$GLOBALS</span><span>[</span><span class="string">"b"</span><span>] = </span><span class="vars">$GLOBALS</span><span>[</span><span class="string">"a"</span><span>] + </span><span class="vars">$GLOBALS</span><span>[</span><span class="string">"b"</span><span>];  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>Sum();  </span></li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="vars">$b</span><span>;  </span>
</li>
<li><span>?>  </span></li>
</ol>

PHP 表单取值

如果 mothod="get" 就用 $_GET["test"] 代替$test
如果 mothod="post" 就用 $_POST["test"] 代替$test

PHP取得当前IP

<ol class="dp-c">
<li class="alt"><span><span>=</span><span class="vars">$HTTP_SERVER_VARS</span><span>[</span><span class="string">"REMOTE_ADDR"</span><span>]?>  </span></span></li>
<li><span>PHP 取得当前时间  </span></li>
<li class="alt"><span> </span></li>
<li><span><?php  </span></span></li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d G:i:s"</span><span>);  </span>
</li>
<li><span>?>  </span></li>
<li class="alt">
<span class="func">date</span><span>(</span><span class="string">"Y年m月d日"</span><span>)  </span>
</li>
<li>
<span class="func">Date</span><span> (</span><span class="string">"Y-n-j"</span><span>) </span>
</li>
</ol>

希望通过本文的介绍,能给你带来帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445750.htmlTechArticlePHP ,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn