Programming Articles

Page 1052 of 2547

Get all the images from a folder in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 5K+ Views

In PHP, you can retrieve all images from a folder using the glob() function. This function searches for files matching a specific pattern and returns an array of matching file paths. Using glob() with Single Extension The simplest approach is to specify the folder path and file extension pattern − Getting Multiple Image Formats To retrieve different image formats (PNG, JPG, GIF), you can use the GLOB_BRACE flag − Using scandir() Alternative You can also use scandir() with array_filter() for more control − ...

Read More

PHP: Unlink All Files Within A Directory, and then Deleting That Directory

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 497 Views

In PHP, removing a directory and all its contents requires unlinking all files first, then removing subdirectories recursively. PHP provides several approaches using functions like glob(), unlink(), and rmdir(). Note: File system operations require proper permissions. Ensure the PHP script has write access to the target directory. Method 1: Recursive Directory Removal This function recursively removes all files and subdirectories within a directory ? Method 2: Using array_walk() (PHP 5.3+) A more concise approach using anonymous functions for file removal ? Method 3: Using ...

Read More

What are the differences in die() and exit() in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

In PHP, die() and exit() are functionally identical language constructs that terminate script execution. However, there are some subtle differences worth understanding. Official Documentation The PHP Manual confirms their equivalence ? For exit() ? "This language construct is equivalent to die()." For die() ? "This language construct is equivalent to exit()." Functional Comparison Both functions work identically and can accept an optional parameter ? Before exit Script terminated with exit() Before die Script terminated with ...

Read More

Search for partial value match in an Array in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can search for partial value matches in an array using various methods. The most common approaches include strpos() with loops, array_filter() with callback functions, and preg_grep() for pattern matching. Using strpos() with foreach Loop The simplest method uses strpos() to check if a substring exists within each array element −

Read More

How to create Web API service in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 971 Views

Web APIs in PHP allow different applications to communicate and exchange data over HTTP. SOAP and REST are the most widely used API architectures for building web services. Creating a Simple REST API Let's start with a basic PHP class that manages database entries ? Creating the API Endpoint To make this functionality accessible to external applications, create an API endpoint that acts as an interface ? Complete REST API Example Here's a more comprehensive API that handles multiple HTTP methods ? ...

Read More

Convert spaces to dash and lowercase with PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

In PHP, you can convert spaces to dashes and make text lowercase using a combination of str_replace() and strtolower() functions. This is commonly used for creating URL-friendly strings or slugs. Basic Approach The most straightforward method is to combine strtolower() with str_replace() to first convert the string to lowercase, then replace spaces with dashes ? hello-have-a-good-day-everyone Using preg_replace() for Multiple Spaces For handling multiple consecutive spaces, preg_replace() with regular expressions provides better control ? hello-have-multiple-spaces Complete URL Slug Function Here's ...

Read More

PHP file that should run once and delete itself. Is it possible?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 338 Views

Yes, it is possible to create a PHP file that deletes itself after execution. This can be achieved using PHP's unlink() function along with the __FILE__ magic constant. Simple Self-Deletion The most straightforward approach is to call unlink() at the end of your script ? Using Destructor Method A more robust approach uses a class destructor to ensure the file is deleted even if the script exits unexpectedly ? Key Considerations When implementing self-deleting scripts, keep these important points in mind ? File Permissions: ...

Read More

Parsing JSON array with PHP foreach

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 7K+ Views

In PHP, you can parse JSON arrays using json_decode() and iterate through them with foreach. The json_decode() function converts JSON strings into PHP arrays or objects. Syntax json_decode($json_string, $associative = false) Parameters Parameter Description $json_string The JSON string to decode $associative When true, returns associative array instead of object Example Here's how to parse a JSON array and access its nested values using foreach ? Output This will produce the following output ? 3490abc ...

Read More

How to use RegexIterator in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

RegexIterator is a built-in PHP class that filters iterators using regular expressions. It's particularly useful for filtering file listings, array elements, or any iterable data based on pattern matching. Basic File Filtering The most common use case is filtering files in a directory based on their names or paths −

Read More

PHP - How to use $timestamp to check if today is Monday or 1st of the month?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can use the date() function with a timestamp to check if today is Monday or the 1st of the month. The date() function formats a timestamp based on the specified format string. The timestamp parameter is optional and defaults to the current time if not provided. Syntax date(format, timestamp) Parameters format − Required. Specifies the format of the outputted date string timestamp − Optional. Specifies a Unix timestamp. Default is the current date and time Example Here's how to check if a given timestamp represents Monday ...

Read More
Showing 10511–10520 of 25,466 articles
Advertisements