自定义PHP分页函数

这段PHP分页代码我当时也踩过坑。
有一年冬天,我在北京做一个外包项目,客户要求寻呼。
导致数据量大的时候,分页直接卡住了。

Look at this code, if (!function_exists(pageft)) this judgment is good, avoid repeated inclusion.参数 $totle、$displaypg 和 $url 也很清楚。
The default $displaypg=2 0 is fine, but $url=''' defaults to an empty string, which is actually a bit convoluted, and needs to be assigned a different value internally.
当时我发现当$page为空时,程序会直接爆炸。
So $page=$_GET['page'] 1 ; This way of typing is safer and PHP7 and higher can use 直接的。
For URL parsing, parse_url and ereg_replace are ok, but regular expressions are sometimes very difficult, especially URLs with parameters.
For the SQL part, use mysql_num_rows to get the total number, then limit the paging, no problem.但mssql_num_rows和mssql_result现在使用PDO或SQLSRV,并且top关键字如果使用太多容易出错。

When I wrote paging, I directly used LIMIT ?, ?和参数化查询,更安全、更易于使用。
This pageft function can actually be encapsulated, for example returning $pagenav or outputting it directly. Look at $pagenav.="View page"... This type of splicing is prone to errors. I later used a template engine.
Oh, by the way, I also had trouble converting iconv to encoding back then. Incorrect character sets resulted in garbled characters. Especially for truncation functions like cut_str there are many bounds problems. It is recommended to use mb_substr.
This code is well written, but it could be optimized. When using it, please be aware to test the situation with large data volume and paging with URL parameters. If you run into a problem, just stop and look at the values ​​of the $page and $firstcount variables and it will be clear.
If you are not sure, you can ask me. When I was working on that project in Beijing, I debugged until I was bald just for this paging.

PHP函数怎样写一个输出问候语的函数 PHP函数简单问候功能的编写技巧​

知道了。
Writing a greeting function in PHP is very basic.写这篇文章的时候我可能有点困惑,但慢慢就习惯了。

看,最简单的方法就是直接打印。
示例:
php 函数 BasicGreet($name) { 回声“你好”。
$名称。
“!”; } BasicGreet("小明"); // 直接输出:你好,小敏!
就是这样,就这么简单。
但是,如果您想添加基于时间的问候语等技巧,则需要添加参数。

例如,2 02 2 年,我在项目中编写了以下内容:
php 函数 timeGreet($name, $time) { if ($time == "朝") { “早上好,”我重复道。
$名称。
"!"; elseif ($time == "下午") { 回声“你好。
” $name. "!"; } それ以外の场合は { Echo "Hello". $name 。
"!"; } } timeGreet("小红", "早上好"); // 输出:早上好,小红!
没关系。
然而,如果时间类型很多,写if elseif就变得乏味了。
然后我学会了如何使用match语句,感觉更加清晰了。
Example:
php 函数 matchGreet($name, $time) { $greet = 一致 ($time) { “早上好”=>“早上好” 「午后」=>「こんにちは」、 "Evening" => "Good evening", Default => "Hello" }; $greet をエコーし​​​​ます。
“,”。
$name. “!”; } matchGreet("小刚", "晚上"); // 输出:晚上好,小刚!
这样感觉比较好。
如下参数的默认值也很有用:
php functiondefaultGreet($name, $time = "一般") { $greet = match($time) { “早上好”=>“早上好” "Afternoon" => "Hello", “晚上”=>“晚上好”, デforuto => 「こんにちは」 }; Echo $greet. 「、」。
$name 。
"!"; } defaultGreet("Xiaoli"); //Output: Hello, Xiaoli!由于时间默认为“常规”
至于输出方式,有时需要直接输出,有时需要返回。
例如:
php 关数 returnGreet($name) { Returns "Hello". $name 。
「!」; } $message = returnGreet("Xiao Wang"); file_put_contents("greet.log", $message); // 将问候语写入文件
这将保存结果。
您还应该注意命名和参数设计。
例如,使用greetUser作为函数名比使用doSomething更好。
参数的顺序也必须正确,必需的参数在前,可选的参数在后。

前に书いたときは、少し极端で、デォルト値をしっかりと设定しておかないと、他の人が使用するときに间违いやすいと感じたかもしれません。
后で、パラメータを省略しても问题ない场合があり、PHP が自動的にデフォルト値を使用することがわかりました。

When connecting strings, be careful not to miss any spaces or punctuation.例如,使用 sprintf:
php 会更清晰。
函数 sprintfGreet($name) { echo sprintf("Hello, %s!", $name); } sprintfGreet("小张"); // 输出:你好,小张!
This way it won't get dirty.再利用性が不十分であることも問題です。
たとえば、挨拶文はハードコーディングされているため、後で変更する場合に面倒になります。
配列にすることも、ファイルから読み取ることもできます:
php $greetMap = ["午前" => "おはよう", "午後" => "こんにちは"]; Function fileGreet($name, $time) { global $greetMap; $greet = $greetMap[$time] “你好”; Echo $greet. “,”。
$名称。
“!”; } fileGreet("シャオリー", "朝"); // 出力: おはようございます、Xiao Li!
つまり、PHP グリーティング関数を作成するということは、パラメータ化、出力モード、および保守性を意味します。
Please practice more.