


9 Practical PHP Functions and Functions You Must Know_PHP Tutorial
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
