PHP Articles

Page 50 of 81

Parse HTML with PHP's HTML DOMDocument

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 571 Views

PHP's DOMDocument class provides powerful tools for parsing and manipulating HTML content. You can extract specific elements using XPath queries or DOM traversal methods. Example The following example shows how to extract text from nested elements using XPath −

Read More

How can I get PHP to display the headers it received from a browser?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

PHP provides several methods to display HTTP headers received from a browser. You can access individual headers through the $_SERVER superglobal or retrieve all headers using the getallheaders() function. Using $_SERVER Superglobal To access a specific header, use the $_SERVER array with the header name prefixed by HTTP_ ? Using getallheaders() Function The getallheaders() function returns all HTTP request headers as an associative array ? Alternative Method Using $_SERVER You can also extract all HTTP headers by filtering the $_SERVER array ? ...

Read More

How to get the length of longest string in a PHP array

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

In PHP, you can find the length of the longest string in an array by combining array_map() and max() functions. The array_map() function applies strlen() to each element, and max() returns the highest value. Syntax $max_length = max(array_map('strlen', $array)); Example Here's how to find the longest string length in a PHP array − Length of longest string: 8 How It Works The process involves two steps: array_map('strlen', $array) creates a new array containing the length of each string max() function finds the maximum ...

Read More

Returning two values from a function in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 917 Views

In PHP, you cannot directly return two variables from a function, but you can achieve this by returning them in an array or using other data structures. This approach allows you to return multiple values and access them efficiently. Method 1: Using Indexed Array The most common approach is to return an array containing the values − Name: John Age: 25 Method 2: Using Associative Array For better readability, use associative arrays with meaningful keys − Area: 15 Perimeter: 16 Method ...

Read More

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 484 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 954 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
Showing 491–500 of 802 articles
« Prev 1 48 49 50 51 52 81 Next »
Advertisements