Nine useful functions of PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:44:32
Original
1157 people have browsed it

1. Any number of parameters of the function

You may know that PHP allows you to define a function with default parameters. But you may not know that PHP also allows you to define a function with completely arbitrary parameters

Here is an example showing you a function with default parameters:

  // 两个默认参数的函数

  function foo($arg1 = , $arg2 = ) {

  echo "arg1: $arg1 ";

  echo "arg2: $arg2 ";

  }

  foo(hello,world);

  /* 输出:

  arg1: hello

  arg2: world

  */

  foo();

  /* 输出:

  arg1:

  arg2:

  */ 

 

// Function with two default parameters

Function foo($arg1 = , $arg2 = ) {

echo "arg1: $arg1 ";

  // 是的,形参列表为空

  function foo() {

  // 取得所有的传入参数的数组

  $args = func_get_args();

  foreach ($args as $k => $v) {

  echo "arg".($k+1).": $v ";

  }

  }

  foo();

  /* 什么也不会输出 */

  foo(hello);

  /* 输出

  arg1: hello

  */

  foo(hello, world, again);

  /* 输出

  arg1: hello

  arg2: world

  arg3: again

  */ 

 

echo "arg2: $arg2 ";

 }

foo(hello,world);

/* Output:

  // 取得所有的后缀为PHP的文件

  $files = glob(*.php);

  print_r($files);

  /* 输出:

  Array

  (

  [0] => phptest.php

  [1] => pi.php

  [2] => post_output.php

  [3] => test.php

  )

  */ 

 

arg1: hello arg2: world  */ foo(); /* Output: arg1: arg2:
 */
Now let’s take a look at a function with variable parameters, which uses the ?func_get_args() 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).": $v ";  }  } foo(); /* Nothing will be output */ foo(hello);  /* Output arg1: hello  */ foo(hello, world, again);  /* Output arg1: hello arg2: world arg3: again  */
 2. Use Glob() to find files Many PHP functions have a relatively long self-explanatory function name. However, when you see ?glob(), you may not know what this function is used for unless you are familiar with it. Very familiar. You can think of this function as scandir(), which can be used to find files.
// 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  )  */

You can also search for multiple suffixes

  // 取PHP文件和TXT文件

  $files = glob(*.{php,txt}, GLOB_BRACE);

  print_r($files);

  /* 输出:

  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);

  /* 输出:

  Array

  (

  [0] => ../images/apple.jpg

  [1] => ../images/art.jpg

  )

  */ 

 

// Get PHP files and TXT files

$files = glob(*.{php,txt}, GLOB_BRACE);

print_r($files);

  $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

  )

  */ 

 

/* Output: Array  (  [0] => phptest.php  [1] => pi.php  [2] => post_output.php  [3] => test.php  [4] => log.txt  [5] => test.txt  )  */ You can also add the path: $files = glob(../images/a*.jpg); print_r($files); /* Output: Array  (  [0] => ../images/apple.jpg  [1] => ../images/art.jpg  )
 */
If you want to get the absolute path, you can call the ?realpath() function:
$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  )  */

 3. Memory usage information

Observing the memory usage of your program allows you to better optimize your code.

PHP has a garbage collection mechanism and a very complex memory management mechanism. You can find out how much memory your script is using. To know the current memory usage, you can use the memory_get_usage() function. If you want to know the peak memory usage, you can call the memory_get_peak_usage() function.

  echo "Initial: ".memory_get_usage()." bytes ";

  /* 输出

  Initial: 361400 bytes

  */

  // 使用内存

  for ($i = 0; $i < 100000; $i++) {

  $array []= md5($i);

  }

  // 删除一半的内存

  for ($i = 0; $i < 100000; $i++) {

  unset($array[$i]);

  }

  echo "Final: ".memory_get_usage()." bytes ";

  /* prints

  Final: 885912 bytes

  */

  echo "Peak: ".memory_get_peak_usage()." bytes ";

  /* 输出峰值

  Peak: 13687072 bytes

  */

echo "Initial: ".memory_get_usage()." bytes ";

 /* Output

 Initial: 361400 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

  )

  */ 

 

// Use memory  for ($i = 0; $i < 100000; $i++) {<🎜> <🎜> $array []= md5($i);<🎜> <🎜> }<🎜> <🎜> // Delete half of the memory <🎜> <🎜> for ($i = 0; $i < 100000; $i++) {<🎜> <🎜>unset($array[$i]);<🎜> <🎜> }<🎜> <🎜> echo "Final: ".memory_get_usage()." bytes ";<🎜> <🎜> /* prints<🎜> <🎜>Final: 885912 bytes<🎜> <🎜> */<🎜> <🎜> echo "Peak: ".memory_get_peak_usage()." bytes ";<🎜> <🎜> /* Output peak value<🎜> <🎜>Peak: 13687072 bytes<🎜>
<🎜> */ <🎜> <🎜> <🎜>
<🎜> <🎜> 4. CPU usage information<🎜> <🎜>Using the ?getrusage() function can let you know the CPU usage. Note that this feature is not available under Windows. <🎜> <🎜>
<🎜>print_r(getrusage());<🎜> <🎜> /* Output<🎜> <🎜>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  )  */

This structure seems very obscure, unless you know the CPU very well. Some explanations below:

  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) 

 

ru_oublock: block output operation

ru_inblock: Block input operation

ru_msgsnd: message sent

ru_maxrss: Maximum resident set size ru_ixrss: total shared memory size ru_idrss: All non-shared memory size ru_minflt: Page recycling ru_majflt: Page invalid
ru_nsignals: Received signals ru_nvcsw: Active context switching
ru_nivcsw: Passive context switching  ru_nswap: Swap area ru_utime.tv_usec: User time (microseconds) ru_utime.tv_sec: user time (seconds) ru_stime.tv_usec: System kernel time (microseconds)  ru_stime.tv_sec: System kernel time? (seconds)
http://www.bkjia.com/PHPjc/478742.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478742.htmlTechArticle1. Any number of parameters for a function You may know that PHP allows you to define a function with default parameters. But you may not know that PHP also allows you to define a function with completely arbitrary parameters...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template