Programmers who have done PHP development should know that there are many built-in functions in PHP. Mastering them can help you become more comfortable in PHP development. This article will share 8 essential PHP functions for development, each of which They are all very practical and I hope all PHP developers can master them.
1. Pass any number of function parameters
In .NET or JAVA programming, the number of function parameters is generally fixed, but PHP allows you to use any number of parameters.The following example shows you the default parameters of a PHP function:
Php code
- // Function with two default parameters
- function foo($arg1 = ”, $arg2 = ”) {
- echo “arg1: $arg1n”;
- echo “arg2: $arg2n”;
- }
- foo(‘hello’,’world’);
- /* Output:
- arg1: hello
- arg2: world
- */
- foo();
- /* Output:
- arg1:
- arg2:
- */
- The following example is the usage of variable parameters in PHP, which uses the [url=http://us2.php.net/manual/en/function.func-get-args.php]func_get_args()[/url] method:
- // Yes, the parameter list is empty
- function foo() {
- // Get the array of all incoming parameters
- $args = func_get_args();
- foreach ($args as $k => $v) {
- echo “arg”.($k+1).”: $vn”;
- }
- }
- foo();
- /* Nothing will be output */
- foo(‘hello’);
- /* Output
- arg1: hello
- */
- foo(‘hello’, ‘world’, ‘again’);
- /* Output
- arg1: hello
- arg2: world
- arg3: again
- */
- // Get all files with the suffix PHP
- $files = glob(‘*.php’);
- print_r($files);
- /* Output:
- Array
- (
- [0] => phptest.php
- [1] => pi.php
- [2] => post_output.php
- [3] => test.php
- )
- */
- // Get PHP files and TXT files
- $files = glob(‘*.{php,txt}’, GLOB_BRACE);
- print_r($files);
- /* Output:
- Array
- (
- [0] => phptest.php
- [1] => pi.php
- [2] => post_output.php
- [3] => test.php
- [4] => log.txt
- [5] => test.txt
- )
- */
- $files = glob(‘../images/a*.jpg’);
- print_r($files);
- /* Output:
- Array
- (
- [0] => ../images/apple.jpg
- [1] => ../images/art.jpg
- )
- */
- $files = glob(‘../images/a*.jpg’);
- //applies the function to each array element
- $files = array_map(‘realpath’,$files);
- print_r($files);
- /* output looks like:
- Array
- (
- [0] => C:wampwwwimagesapple.jpg
- [1] => C:wampwwwimagesart.jpg
- )
- */
- echo “Initial: “.memory_get_usage().” bytes n”;
- /* Output
- Initial: 361400 bytes
- */
- //Use memory
- for ($i = 0; $i
- $array []= md5($i);
- }
- // Delete half of the memory
- for ($i = 0; $i
- unset($array[$i]);
- }
- echo “Final: “.memory_get_usage().” bytes n”;
- /* prints
- Final: 885912 bytes
- */
- echo “Peak: “.memory_get_peak_usage().” bytes n”;
- /* Output peak value
- Peak: 13687072 bytes
- */
- print_r(getrusage());
- /* 输出
- Array
- (
- [ru_oublock] => 0
- [ru_inblock] => 0
- [ru_msgsnd] => 2
- [ru_msgrcv] => 3
- [ru_maxrss] => 12692
- [ru_ixrss] => 764
- [ru_idrss] => 3864
- [ru_minflt] => 94
- [ru_majflt] => 0
- [ru_nsignals] => 1
- [ru_nvcsw] => 67
- [ru_nivcsw] => 4
- [ru_nswap] => 0
- [ru_utime.tv_usec] => 0
- [ru_utime.tv_sec] => 0
- [ru_stime.tv_usec] => 6269
- [ru_stime.tv_sec] => 0
- )
- */
- ru_oublock: 块输出操作
- ru_inblock: 块输入操作
- ru_msgsnd: 发送的message
- ru_msgrcv: 收到的message
- ru_maxrss: 最大驻留集大小
- ru_ixrss: 全部共享内存大小
- ru_idrss:全部非共享内存大小
- ru_minflt: 页回收
- ru_majflt: 页失效
- ru_nsignals: 收到的信号
- ru_nvcsw: 主动上下文切换
- ru_nivcsw: 被动上下文切换
- ru_nswap: 交换区
- ru_utime.tv_usec: 用户态时间 (microseconds)
- ru_utime.tv_sec: 用户态时间(seconds)
- ru_stime.tv_usec: 系统内核时间 (microseconds)
- ru_stime.tv_sec: 系统内核时间?(seconds)
- // sleep for 3 seconds (non-busy)
- sleep(3);
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* 输出
- User time: 0.011552
- System time: 0
- */
- // loop 10 million times (busy)
- for($i=0;$i
- }
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* 输出
- User time: 1.424592
- System time: 0.004204
- */
- $start = microtime(true);
- // keep calling microtime for about 3 seconds
- while(microtime(true) – $start
- }
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* prints
- User time: 1.088171
- System time: 1.675315
- */
- // this is relative to the loaded script’s path
- // it may cause problems when running scripts from different directories
- require_once(‘config/database.php’);
- // this is always relative to this file’s path
- // no matter where it was included from
- require_once(dirname(__FILE__) . ‘/config/database.php’);
- // some code
- // …
- my_debug(“some debug message”, __LINE__);
- /* Output
- Line 4: some debug message
- */
- // some more code
- // …
- my_debug(“another debug message”, __LINE__);
- /* Output
- Line 11: another debug message
- */
- function my_debug($msg, $line) {
- echo “Line $line: $msgn”;
- }
- // generate unique string
- echo uniqid();
- /* Output
- 4bd67c947233e
- */
- // generate another unique string
- echo uniqid();
- /* Output
- 4bd67c9472340
- */
- // Prefix
- echo uniqid(‘foo_’);
- /* Output
- foo_4bd67d6cd8b8f
- */
- // There is more entropy
- echo uniqid(”,true);
- /* Output
- 4bd67d6cd8b926.12135106
- */
- // All
- echo uniqid(‘bar_’,true);
- /* Output
- bar_4bd67da367b650.43684647
- */
- // A complex array
- $myvar = array(
- ‘hello’,
- 42,
- array(1,’two’),
- ‘apple’
- );
- // Serialization
- $string = serialize($myvar);
- echo $string;
- /* Output
- a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3: "two";}i:3;s:5:"apple";}
- */
- //Desequential instantiation
- $newvar = unserialize($string);
- print_r($newvar);
- /* Output
- Array
- (
- [0] => hello
- [1] => 42
- [2] => Array
- (
- [0] => 1
- [1] => two
- )
- [3] => apple
- )
- */
- // a complex array
- $myvar = array(
- ‘hello’,
- 42,
- array(1,’two’),
- ‘apple’
- );
- // convert to a string
- $string = json_encode($myvar);
- echo $string;
- /* prints
- ["hello",42,[1,"two"],"apple"]
- */
- // you can reproduce the original variable
- $newvar = json_decode($string);
- print_r($newvar);
- /* prints
- Array
- (
- [0] => hello
- [1] => 42
- [2] => Array
- (
- [0] => 1
- [1] => two
- )
- [3] => apple
- )
- */
- $string =
- "The truth is that the pain itself is good, it will be successful.
- customer service. Now let's get it out of my hands
- coaching There is no easy way. It's a pillow,
- wisdom or flight of the vestibule, I will not give the price of the hospital,
- He did not raise the lake as much as before. Thank you very much.
- let it be good, it will be able to attract the customer. Some
- the price of any body that is targeted. Yes, and mass
- but it was an ugly time of mourning. I'm sorry but I'm sorry
- soft homework It is the very day, it will be the result of life
- to decorate a, something from now In that great and pushing
- to lay the ground But not my fear, but Lacinia
- advertise But unless it's big, decorate it in soft, soft
- but now. Even at just the right time for homework.
- There is no need to fear chocolate and chocolate
- not football. To drink the unsavory lake of football
- that euismod urn members “;
- $compressed = gzcompress($string);
- echo "Original size:". strlen($string).”n”;
- /* output original size
- Original size: 800
- */
- echo "Compressed size:". strlen($compressed)."n";
- /* output 剧情后 size
- Compressed size: 418
- */
- // 解剧情
- $original = gzuncompress($compressed);
原文出处:8个安全必备的PHP function

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

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

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

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

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

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
