Programming Articles

Page 1047 of 2547

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 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 834 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 730 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

Generate all combinations of a specific size from a single set in PHP

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

To generate all combinations of a specific size from a single set in PHP, we can use a recursive function that builds combinations by concatenating characters from the original set. Example Here's a function that generates all possible combinations of a specified size from a character set ? Output This will produce the following output ? array(9) { [0]=> string(2) "aa" [1]=> string(2) "ab" [2]=> string(2) "ac" [3]=> string(2) "ba" [4]=> ...

Read More

Access variables from parent scope in anonymous PHP function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 660 Views

In PHP, anonymous functions (closures) cannot access variables from the parent scope by default. The use keyword allows you to bind variables from the parent scope into the anonymous function's scope. Syntax The basic syntax for accessing parent scope variables in anonymous functions − $variable = function() use ($parentVar) { // Access $parentVar here }; Example Here's how to use the use keyword to access parent scope variables − The output of the above code is − string(9) "undefined" string(11) "hello there" ...

Read More

Will enabling XDebug on a production server make PHP slower?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

Yes, enabling XDebug on a production server significantly slows down PHP performance. XDebug introduces substantial overhead because it hooks into PHP's execution process, monitors every line of code, and maintains debugging metadata even when not actively debugging. Performance Impact XDebug can reduce PHP performance by 50−300% depending on your application. Here's a simple benchmark comparison − Without XDebug: 45.67 ms, 8.2 MB With XDebug: 156.34 ms, 24.8 MB Why XDebug Slows Down PHP XDebug impacts performance through several mechanisms − Code Coverage: Tracks which lines are executed ...

Read More

What does [Ss]* mean in regex in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 488 Views

The regular expression [Ss]* in PHP matches any character (including whitespace and newlines) zero or more times. The character class [Ss] combines \S (non-whitespace characters) and \s (whitespace characters), effectively matching any character in the string. Basic Usage Here's how [Ss]* works in practice ? Match found: Hello World with spaces Comparison with Dot Modifier The [Ss]* pattern serves as an alternative to using the dot with the s flag ? Using [Ss]*: 21 characters Using .*: 6 characters Using .* with s ...

Read More
Showing 10461–10470 of 25,466 articles
Advertisements