Home>

A few recently written PHP practical functions are organized,Into a document,Write a note,Easy to use later! There are 4 php utility functions,Share with you now,Favorite friends can collect it,Maybe later.

1. Any number of arguments to a php functionYou 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 of a function that shows you the default parameters:

//function with two default parameters
function foo ($arg1="", $arg2="") {
echo "arg1:$arg1 \ n";
echo "arg2:$arg2 \ n";
}
foo ("hello", "world");
/* Output:
arg1:hello
arg2:world
* /
foo ();
/* Output:
arg1:
arg2:
* /

Now let ’s take a look at a function with indefinite parameters.It uses the func_get_args () method:

//yes, the parameter list is empty
function foo () {
//Get an array of all incoming parameters
$args=func_get_args ();
foreach ($args as $k =>$v) {
echo "arg". ($k + 1). ":$v \ n";
}
}
foo ();
/* Nothing is output * /
foo ("hello");
/* Output
arg1:hello
* /
foo ("hello", "world", "again");
/* Output
arg1:hello
arg2:world
arg3:again
* /

2.glob () find filesThere are many PHP functions that 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 already familiar with it.

You can think of this function as "scandir ()", which can be used to find files.

//Get all files with 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 find multiple suffix names

//take php file and txt file
$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
)
* /

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 i 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:\ wamp \ www \ images \ apple.jpg
[1] =>c:\ wamp \ www \ images \ art.jpg
)
* /

3. View memory usageObserving the memory usage of your program will allow you to better optimize your code.

PHP has a garbage collection mechanism,And there is a very complicated memory management mechanism.You can know the memory used by your script.To know the current memory usage,You can use the?memory_get_usage () function, if i want to know the peak value of memory usage,You can call the memory_get_peak_usage () function.

echo "initial:" .memory_get_usage (). "bytes \ n";
/* Output
initial:361400 bytes
* /
//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 \ n";
/* prints
final:885912 bytes
* /
echo "peak:" .memory_get_peak_usage (). "bytes \ n";
/* Output peak
peak:13687072 bytes
* /

4. View CPU usage

Using the?getrusage () function lets you know the usage of the cpu.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 looks very obscure,Unless you know the cpu well. Here are some explanations:

ru_oublock:Block output operation

ru_inblock:Block input operation

ru_msgsnd:message to send

ru_msgrcv:received message

ru_maxrss:maximum resident set size

ru_ixrss:Total shared memory size

ru_idrss:total non-shared memory size

ru_minflt:page recycling

ru_majflt:page invalid

ru_nsignals:received signals

ru_nvcsw:Active context switch

ru_nivcsw:passive context switch

ru_nswap:swap area

ru_utime.tv_usec:user mode time (microseconds)

ru_utime.tv_sec:user mode time (seconds)

ru_stime.tv_usec:system kernel time (microseconds)

ru_stime.tv_sec:system kernel time (seconds)

php
  • Previous Java garbage collection finalize ()
  • Next MFC method to append data at the end of the file