Home > Backend Development > PHP Tutorial > 9 Practical PHP Functions and Functions You Must Know_PHP Tutorial

9 Practical PHP Functions and Functions You Must Know_PHP Tutorial

WBOY
Release: 2016-07-21 14:54:13
Original
753 people have browsed it

Even if you have been using PHP for many years, you will stumble upon functions and capabilities that you never knew existed. 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

As you may already know, PHP allows defining functions with optional parameters. But there are also methods that completely allow any number of function parameters. The following are examples 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
)
*/

  • Total 5 pages:
  • Previous page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364604.htmlTechArticleEven if you have been using PHP for many years, you will stumble upon some unknown functions and functions. Some of them are very useful but underutilized. Not everyone will do it from beginning to end...
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