PHP Articles

Page 45 of 81

In PHP, how can I add an object element to an array?

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

In PHP, you can add an object element to an array using several methods. The most common approach is to create an object and then append it to an array using the array push syntax. Method 1: Using stdClass Object Create a standard object and add it to an array ? Array ( [0] => a [1] => c [2] => stdClass Object ( ...

Read More

Detect language from string in PHP

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

Detecting the language of a text string in PHP can be achieved using specialized libraries. While character-based detection isn't reliable, the Text_LanguageDetect PEAR package provides decent accuracy for language identification. Installation: Install the Text_LanguageDetect package using PEAR: pear install Text_LanguageDetect Using Text_LanguageDetect Package The Text_LanguageDetect package analyzes text patterns to identify the most likely languages ? Output The output shows detected languages with confidence scores ? Array ( [english] => 0.668518518519 [german] => 0.407037037037 [dutch] ...

Read More

How to read only 5 last line of the text file in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 715 Views

In PHP, you can read only the last 5 lines of a text file using the file() function combined with array slicing. The file() function reads the entire file into an array where each line becomes an array element. Using file() Function This method loads the entire file into memory and extracts the last 5 lines ? Using array_slice() Method A more concise approach using array_slice() to get the last 5 elements ? Memory-Efficient Method for Large Files For very large files, reading line by line ...

Read More

How to download large files through PHP script?

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

Downloading large files through PHP requires careful memory management to avoid script timeouts and memory exhaustion. The key is to read and output the file in small chunks rather than loading it entirely into memory. Chunked File Download Function Here's a function that downloads large files by reading them in chunks − Complete Download Script with Headers For actual file downloads, you need proper HTTP headers −

Read More

Make PHP pathinfo() return the correct filename if the filename is UTF-8

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

Most of the core PHP functions don’t deal with character sets apart from Latin-1. However, the pathinfo() function can be made to handle UTF-8 filenames correctly by setting the appropriate locale using setlocale() before calling pathinfo(). By default, PHP runs with ‘C’ locale, while CLI scripts run with a default UTF-8 locale. To handle UTF-8 filenames properly, the locale should be changed from ‘C’ to ‘C.UTF-8’ or ‘en_US.UTF-8’ before calling pathinfo(). Example Here’s how to set the locale and use pathinfo() with UTF-8 filenames − Filename: résumé_français Basename: résumé_français.pdf Extension: pdf ...

Read More

How do I iterate through DOM elements in PHP?

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

In PHP, you can iterate through DOM elements using the DOMDocument class along with methods like getElementsByTagName() and foreach loops to traverse child nodes. Sample XML Data Let's work with the following XML structure ? Value 1 Data 1 Value 2 Data 2 ...

Read More

What does the '@' prefix do in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 825 Views

The '@' symbol in PHP is an error control operator that suppresses error messages from being displayed. When prepended to an expression, it silences any error messages that would normally be generated by that expression. Syntax The '@' operator is placed directly before the expression you want to suppress errors for − @expression Example Here's how the '@' operator works in practice ? Without @ operator: Warning: Division by zero Result: INF With @ operator: Result: INF File operations: Warning: file_get_contents(nonexistent.txt): failed to open stream: ...

Read More

Is it possible to get list of defined namespaces in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 718 Views

PHP doesn't provide a direct built−in function to list all defined namespaces. However, you can determine if a namespace exists by examining the classes within it using get_declared_classes() and class_exists(). Method 1: Check if Namespace Exists This approach checks whether a specific namespace has been loaded by looking for classes within it ? TestNamespace exists Method 2: Extract All Namespaces This method builds a hierarchical array of all namespaces by parsing declared class names ? Array ( [FirstNamespace] => ...

Read More

How to open an Excel file with PHPExcel for both reading and writing?

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

PHPExcel allows you to read an existing Excel file, modify its contents, and save it back with the same filename. This effectively overwrites the original file with your changes, providing read-write functionality. Installation: Download PHPExcel library and ensure the Classes/PHPExcel/ directory is accessible in your project. Reading and Writing Excel Files The following example demonstrates how to load an Excel file, modify cell values, and save the changes back to the same file − How It Works The process involves three main steps: Load: createReader() creates a ...

Read More

Storing objects in PHP session

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

In PHP, you can store objects in sessions using the serialize() and unserialize() functions. The serialize() function converts an object into a storable string representation, while unserialize() recreates the object from that string. Storing Objects in Session To store an object in a session, first serialize it and then assign it to the $_SESSION superglobal ? The session is started using session_start(), a new object is created, then serialized and stored in the session variable. Retrieving Objects from Session To retrieve the stored object, use the unserialize() function ? ...

Read More
Showing 441–450 of 802 articles
« Prev 1 43 44 45 46 47 81 Next »
Advertisements