>  기사  >  백엔드 개발  >  PHP经验分享:常用的小技巧_PHP教程

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

WBOY
WBOY원래의
2016-07-20 10:57:461082검색

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으로 문의하세요.