


When we were writing After many repeated operations, I decided to improve the traditional PHP function parameter passing method and use arrays as parameters. Please see the example below.
Let’s first look at a traditional custom function
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li class=""><span>* @Purpose: 插入文本域 </span></li> <li class="alt"><span>* @Method Name: addInput() </span></li> <li class=""><span>* @Parameter: str $title 表单项标题 </span></li> <li class="alt"><span>* @Parameter: str $name 元素名称 </span></li> <li class=""><span>* @Parameter: str $value 默认值 </span></li> <li class="alt"><span>* @Parameter: str $type 类型,默认为text,可选password </span></li> <li class=""><span>* @Parameter: str $maxlength 最长输入 </span></li> <li class="alt"><span>* @Parameter: str $readonly 只读 </span></li> <li class=""><span>* @Parameter: str $required 是否必填,默认为false,true为必填 </span></li> <li class="alt"><span>* @Parameter: str $check 表单验证function(js)名称 </span></li> <li class=""><span>* @Parameter: str $id 元素id,无特殊需要时省略 </span></li> <li class="alt"><span>* @Parameter: int $width 元素宽度,单位:象素 </span></li> <li class=""><span>* @Parameter: str $tip 元素提示信息 </span></li> <li class="alt"><span>* @Return: </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span>function addInput($title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,<br>$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>=""<br>.$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>=""<br>.$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width.<br>"px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> "; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span> </li>n"<span>; </span><li class=""><span>} </span></li> </ol>
This is the form class I wrote A function that inserts a text box.
The calling method of the PHP function parameter passing method is
<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,""); </span></span></li></ol>
At the beginning, only $title, $name, $value, $type, $maxlength, $readonly and other parameters were reserved. After a period of use, it was found that these basic parameters could not meet the needs. Text box JS verification is required, CSS styles need to be defined, prompt information needs to be added, etc...
After adding $required, $check, $id, $width, $tip and other parameters, I found that all previous calls to this function were found Everything needs to be modified, which adds a lot of workload.
The calling method of the PHP function parameter passing method becomes
<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,"","true",""<br>,"",100,"提示:编号为必填项,只能填写3位"); </span></span></li></ol>
If there are many places where this function is used, it will really take a long time to change each one.
The following is my improved function
<ol class="dp-xml"> <li class="alt"><span><span>function addInput($a) </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> if(is_array($a)) </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">title</font></span><span> = $a['title']; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">name</font></span><span> = $a['name']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">value</font></span><span> = $a['value'] ? $a['value'] : ""; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">type</font></span><span> = $a['type'] ? $a['type'] : "text"; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span> = $a['maxlength'] ? $a['maxlength'] : "255"; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">readonly</font></span><span> = $a['readonly'] ? $a['readonly'] : ""; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">required</font></span><span> = $a['required'] ? $a['required'] : "false"; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">check</font></span><span> = $a['check']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = $a['id']; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">width</font></span><span> = $a['width']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">tip</font></span><span> = $a['tip']; </span> </li> <li class=""><span> } </span></li> <li class="alt"> <span> $title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>="".$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>="".$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width."px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> "; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span> </li>n"<span>; </span><li class="alt"><span>} </span></li> </ol>
The calling method becomes >
<ol class="dp-xml"> <li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput( </span></span></li> <li class=""><span> array( </span></li> <li class="alt"><span> 'title' = "编码", </span></li> <li class=""><span> 'name' = "field0", </span></li> <li class="alt"><span> 'maxlength' = 3, </span></li> <li class=""><span> 'required' = "true", </span></li> <li class="alt"><span> 'width' = 100, </span></li> <li class=""><span> 'tip' = "提示:编号为必填项,只能填写3位", </span></li> <li class="alt"><span> ) </span></li> <li class=""><span>); </span></li> </ol>Passed by A comparison of the PHP function parameter passing methods before and after can be found:
Traditional functions change a lot when they need to be expanded. When used, they must be written in the order of parameters, which is easy to make mistakes.
When the improved function is expanded You can add new parameters at any time. You only need to add the corresponding array key value when calling. Each parameter is clear at a glance without considering the order. The readability of the code is enhanced.
However, there are still improvements in the PHP function parameter passing method. The disadvantage is that the amount of code increases, requiring programmers to write a lot more key values, and the judgment statements and ternary operation statements in the function may affect efficiency.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
