Home >Backend Development >PHP Tutorial >9 practical PHP functions and features you must master
This article is published by Tuosheng Technology, please follow my WeChat toceansoft
Even if you have been using PHP for many years, you will stumble upon some functions and features that you never knew about. Some of them are very useful but underutilized. Not everyone will read the manual and function reference page after page from cover to cover!
1, function with any number of parameters
You may already know that PHP allows defining functions with optional parameters. But there are also methods that completely allow any number of function parameters. The following is an example of optional parameters:
// function with 2 optional arguments
function foo($arg1 = ”, $arg2 = ”) { echo “arg1: $arg1n”;
echo “arg2: $arg2n”; }
foo(‘hello’,’world’);
/* prints: arg1: hello arg2: world */ foo();
/* prints: arg1: arg2: */ Now let’s see how to create a function that accepts any number of arguments. This time you need to use the func_get_args() function:
// yes, the argument list can be empty
function foo() { // returns an array of all passed arguments
$args = func_get_args(); foreach ($args as $k => $v) {
echo “arg”.($k+1).”: $vn”; } }
foo();
/* prints nothing */ foo(‘hello’);
/* prints arg1: hello */ foo(‘hello’, ‘world’, ‘again’);
/* prints arg1: hello arg2: world arg3: again */
2, Use Glob() to find files
Many PHP functions have long, descriptive names. However, it can be difficult to tell what the glob() function can do unless you have used it many times and become familiar with it. Think of it as a more powerful version of the scandir() function, allowing you to search for files based on a pattern.
//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 ) */ You can get multiple 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 ) */ Please note that these files can actually return a path, depending on the query conditions:
$files = glob('../images/a*.jpg');
print_r($files);
/* output looks like: Array ( [0] => ../images/apple.jpg [1] => ../images/art.jpg ) */ If you want to get the full path of each file, 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
By detecting the memory usage of scripts, it is beneficial to code optimization. PHP provides a garbage collector and a very sophisticated memory manager. The amount of memory used by the script while executing may rise or fall. In order to get the current memory usage, we can use the memory_get_usage() function. If you need to get the highest memory usage at any point in time, you can use the memory_limit() function.
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 For this, we need to use the getrusage() function. Please keep in mind that this function is not available on Windows platforms. 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 ) */
This may seem a bit mysterious unless you already have sysadmin rights. Here's a description of each value (you don't need to remember these):
ru_oublock: block output operations
ru_inblock: block input operations ru_msgsnd: messages sent ru_msgrcv: messages received ru_maxrss: maximum resident set size ru_ixrss: integral shared memory size ru_idrss: integral unshared data size ru_minflt: page reclaims ru_majflt: page faults ru_nsignals: signals received ru_nvcsw: voluntary context switches ru_nivcsw: involuntary context switches ru_nswap: swaps ru_utime.tv_usec: user time used (microseconds) ru_utime.tv_sec: user time used (seconds) ru_stime.tv_usec: system time used (microseconds) ru_stime.tv_sec: system time used (seconds) To know how much CPU power the script consumes, we need to look at the values of the ‘user time’ and ‘system time’ parameters. The seconds and microseconds parts are provided separately by default. You can divide by 1 million microseconds and add the seconds parameter value to get a total number of seconds in decimal form. Let’s look at an example:
// 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); /* prints
User time: 0.011552 System time: 0 */ Even though the script took about 3 seconds to run, the CPU usage was very, very low. Because during sleep running, the script actually consumes no CPU resources. There are many other tasks that may take a while but do not take up CPU time like waiting for disk operations. So as you can see, the CPU usage and the actual length of the run time are not always the same. Here is an example:
// loop 10 million times (busy) for($i=0;$i<10000000;$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); /* prints User time: 1.424592 System time: 0.004204 */ This took about 1.4 seconds of CPU time, but almost all of it was user time since there were no system calls. System time is the CPU overhead spent executing a program's system calls. Here is an example: $start = microtime(true); // keep calling microtime for about 3 seconds while(microtime(true) – $start < 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); /* prints User time: 1.088171 System time: 1.675315 */ Now we have quite a bit of system time taken up. This is because the script calls the microtime() function multiple times, which requires a request to the operating system to get the required time. You may also notice that the run times add up to less than 3 seconds. This is because it is possible that there are other processes on the server at the same time and the script is not using 100% of the CPU for the entire 3 second duration. 5, magic constant PHP provides useful functions such as obtaining the current line number (__LINE__), file path (__FILE__), directory path (__DIR__), function name (__FUNCTION__), class name (__CLASS__), method name (__METHOD__) and namespace (__NAMESPACE__) Magic constants. I won’t go into them all in this article, but I will tell you some use cases. When including other script files, use the __FILE__ constant (or use the new __DIR__ constant in PHP5.3): // 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’); Using __LINE__ makes debugging easier. You can track the specific line number. // some code // … my_debug(“some debug message”,__LINE__); /* prints Line 4: some debug message */ // some more code // … my_debug(“another debug message”, __LINE__); /* prints Line 11: another debug message */ function my_debug($msg, $line) { echo “Line $line: $msgn”; } 6, Generate unique identifier In some scenarios, it may be necessary to generate a unique string. I see a lot of people using the md5() function even though it's not exactly meant for this purpose: // generate unique string echo md5(time() . mt_rand(1,1000000)); There is actually a PHP function named uniqid() that ismeant to be used for this. // generate unique string echo uniqid(); /* prints 4bd67c947233e */ // generate another unique string echo uniqid(); /* prints 4bd67c9472340 */ You may notice that although the string is unique, the first few characters are similar, this is because the generated string is relative to the server time. But there is actually a friendly aspect, since each newly generated ID will be arranged in alphabetical order, so sorting becomes simple. To reduce the probability of duplication, you can pass a prefix, or a second argument to increase entropy: // with prefix echo uniqid(‘foo_’); /* prints foo_4bd67d6cd8b8f */ // with more entropy echo uniqid(”,true); /* prints 4bd67d6cd8b926.12135106 */ // both echo uniqid(‘bar_’,true); /* prints bar_4bd67da367b650.43684647 */ This function will produce a shorter string than md5(), saving some space. 7, Serialization Have you ever encountered a situation where you need to store a complex variable in a database or text file? You may not be able to think of a good way to format a string and convert it to an array or object, PHP has this functionality ready for you. There are two popular ways to serialize variables. Here is an example using the serialize() and unserialize() functions: // a complex array $myvar = array( ‘hello’, 42, array(1,’two’), ‘apple’ ); // convert to a string $string = serialize($myvar); echo $string; /* prints 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";} */ // you can reproduce the original variable $newvar = unserialize($string); print_r($newvar);
/* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple
) */ This is the native PHP serialization method. However, due to the popularity of JSON in recent years, support for the JSON format has been added to PHP5.2. Now you can use json_encode() and json_decode() functions:
// 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
) */ This will be more efficient and especially compatible with many other languages such as JavaScript. However, for complex objects, some information may be lost.
8, Compressed String
When talking about compression, we usually think of file compression, such as ZIP compression, etc. String compression is also possible in PHP, but does not involve any compressed files. In the following example, we will make use of the gzcompress() and gzuncompress() functions:
$string =
“Lorem ipsum dolor sit amet, consectetur Adipiscing elit. Nunc ut elit id mi ultricies Adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum, nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam Pretium ullamcorper urna quis iaculis. Etiam ac massa sed turpis tempor luctus. Curabitur sed nibh eu elit mollis congue. Praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in, mollis sed nunc. Etiam at justo in leo congue mollis. Nullam in neque eget metus hendrerit scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. "; $compressed = gzcompress($string);
echo "Original size: ". strlen($string)."n";
/* prints Original size: 800 */ echo “Compressed size: “. strlen($compressed).”n”;
/* prints Compressed size: 418 */ // getting it back
$original = gzuncompress($compressed); The compression rate of this operation can reach about 50%. The additional functions gzencode() and gzdecode() can achieve similar results by using different compression algorithms.
9, registration shutdown function
There is a function called register_shutdown_function(), which allows you to execute some specified code before a certain script completes running. Let's say you need to capture some baseline statistics before the end of script execution, such as the length of time it ran:
// capture the start time
$start_time = microtime(true); // do some stuff
// … // display how long the script took
echo “execution took: “. (microtime(true) – $start_time). "seconds."; This may seem trivial, you just need to add the relevant code at the end of the script running. But if you have called the exit() function, the code will not run. Additionally, if there is a fatal error, or the script is unexpectedly terminated by the user, it may not run again. When you use the register_shutdown_function() function, the code will continue to execute regardless of whether the script stops running:
$start_time = microtime(true);
register_shutdown_function(‘my_shutdown’);
// do some stuff
// … function my_shutdown() {
global $start_time; echo “execution took: “.
(microtime(true) – $start_time). "seconds."; } If you like my article, please follow my WeChat toceansoft
|