Articles on Trending Technologies

Technical articles with clear explanations and examples

Convert ASCII TO UTF-8 Encoding in PHP?

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

In PHP, you can convert ASCII to UTF-8 encoding using built-in functions like iconv() and mb_convert_encoding(). Both functions provide different approaches for character encoding conversion. Using iconv() Function The iconv() function converts strings between different character encodings. It requires specifying the source and target encodings − Original ASCII: Hello World UTF-8 Encoded: Hello World Example with Special Characters Original: café To UTF-8: café Using mb_convert_encoding() Function The mb_convert_encoding() function provides automatic encoding detection and conversion − ...

Read More

Download file through an AJAX call in PHP

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

While AJAX isn't ideal for file downloads due to browser security restrictions, you can trigger file downloads from PHP using JavaScript redirection methods like window.location or by creating downloadable links dynamically. Using window.location for File Download The simplest approach is to redirect the browser to a PHP script that serves the file ? function downloadFile(fileId) { window.location.href = '/download.php?file=' + fileId; } The corresponding PHP download script ?

Read More

Parse HTML with PHP's HTML DOMDocument

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 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 235 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 937 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 506 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
Showing 22461–22470 of 61,297 articles
Advertisements