PHP9 a useful feature or function

1. Any parameter calls the function  

   You probably already know that PHP allows you to define the function of the optional parameters. However, there are an arbitrary number of function parameters allows completely.  

First, here is an example of optional arguments:

// function with 2 optional arguments  
function foo($arg1 = '', $arg2 = '') {  
  
    echo "arg1: $arg1\n";  
    echo "arg2: $arg2\n";  
  
}  
  
foo('hello','world');  
/* prints: 
arg1: hello 
arg2: world 
*/  
  
foo();  
/* prints: 
arg1: 
arg2: 
*/

Now, let's look at how to build function accept any number of arguments. This time we will use func_get_args ():

function foo() {  
    $args = func_get_args();    
    foreach ($args as $k => $v) {  
        echo "arg".($k+1).": $v\n";  
    }    
}  
  
foo();  
/* prints nothing */  
  
foo('hello');  
/* prints 
arg1: hello 
*/  
  
foo('hello', 'world', 'again');  
/* prints 
arg1: hello 
arg2: world 
arg3: again 
*/

2. Use the Glob () files found

PHP has many functions and a descriptive name. However, it may be difficult to know a function named glob () and, unless you are already familiar with that term is moved in from elsewhere.  

I think of it like a function of the ability to function more versions. It allows you to use file search mode.

/ get all php files  
$files = glob('*.php');  
  
print_r($files);  
/* output looks like: 
Array 
( 
    [0] => phptest.php 
    [1] => pi.php 
    [2] => post_output.php 
    [3] => test.php 
) 
*/

Various types of files, like this:

// get all php files AND txt files  
$files = glob('*.{php,txt}', GLOB_BRACE);  
  
print_r($files);  
/* output looks like: 
Array 
( 
    [0] => phptest.php 
    [1] => pi.php 
    [2] => post_output.php 
    [3] => test.php 
    [4] => log.txt 
    [5] => test.txt 
) 
*/

Note that the file can return path, depending on your query:

$files = glob('../images/a*.jpg');  
  
print_r($files);  
/* output looks like: 
Array 
( 
    [0] => ../images/apple.jpg 
    [1] => ../images/art.jpg 
) 
*/

If you want realpath () for each file, you can adjust the effect is a function of:

$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. Memory usage information by observing your memory using a script, you can optimize your code better.  

Some PHP garbage collection and a bunch of complex memory manager.

The amount of memory you are using a script. Scripts can be executed during up and down. To get the current use of memory, we can use memory_get_usage function, and get () function of the maximum number of memory usage at any time, we can use memory_get_peak_usage.

echo "Initial: ".memory_get_usage()." bytes \n";  
/* prints 
Initial: 361400 bytes 
*/  
  
// let's use up some memory  
for ($i = 0; $i < 100000; $i++) {  
    $array []= md5($i);  
}  
  
// let's remove half of the array  
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";  
/* prints 
Peak: 13687072 bytes 
*/

4. CPU usage information  

We want to take advantage of getrusage () function. Remember that it is impossible to provide the Windows platform.

print_r(getrusage());  
/* prints 
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 
) 
 
*/

Source text: http://net.tutsplus.com/tutorials/php/9-useful-php-functions-and-features-you-need-to-know/

Reproduced in: https: //my.oschina.net/tiwer/blog/199848

Guess you like

Origin blog.csdn.net/weixin_34041003/article/details/91708509