PHP Articles

Page 51 of 81

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 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 378 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

Create nested JSON object in PHP?

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

In PHP, you can create nested JSON objects using associative arrays with the json_encode() function. This allows you to build complex data structures that can be easily converted to JSON format. Creating Nested JSON Structure Here's how to create a nested JSON object using associative arrays ? {"client":{"build":"1.0", "name":"MyApp", "version":"1.0"}, "protocolVersion":4, "data":{"distributorId":"DIST001", "distributorPin":"PIN123", "locale":"en-US"}} Pretty Formatted JSON To make the JSON output more readable, use the JSON_PRETTY_PRINT flag ? { "client": { ...

Read More

Add PHP variable inside echo statement as href link address?

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

In PHP, you can include variables inside echo statements to create dynamic href link addresses. There are several approaches depending on whether you're using PHP within HTML or generating HTML from PHP. Method 1: Concatenation with Echo Use string concatenation to combine the variable with the HTML anchor tag ? Visit TutorialsPoint Conclusion All methods achieve the same result − embedding PHP variables in href attributes. Use concatenation for simple cases, variable interpolation for readability, and printf() for complex formatting with multiple variables.

Read More

How do I sort a multidimensional array by one of the fields of the inner array in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

In PHP, you can sort a multidimensional array by one of the fields of the inner arrays using the usort() function with a custom comparison function, or using array_multisort() for simpler cases. Using usort() with Custom Comparison Function The usort() function sorts an array using a user-defined comparison function. This method provides the most flexibility for complex sorting logic ? Product B - $15 Product A - $25 Product C - $35 Using array_multisort() For simpler sorting by a single field, array_multisort() provides a more direct approach ? ...

Read More

Convert all types of smart quotes with PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 894 Views

Smart quotes are special Unicode quotation marks that appear curved or angled, unlike regular straight quotes. In PHP, you can convert these various smart quote types to standard ASCII quotes using character mapping and string replacement ? Basic Smart Quote Conversion The following function converts common smart quotes to regular quotes using a character mapping array ?

Read More

How do you pass objects by reference in PHP 5?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 Views

In PHP 5, objects are automatically passed by reference, meaning that when you pass an object to a function or assign it to another variable, you're working with the same object instance, not a copy. Understanding Object References A PHP reference is an alias that allows two different variables to point to the same value. In PHP 5, object variables don't contain the actual object − they hold an object identifier that points to the object in memory. When an object is passed as an argument, returned, or assigned to another variable, these variables contain a copy ...

Read More

How to find PHP execution time?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

In PHP, you can measure script execution time using several methods. The most common approaches include microtime() for simple timing and getrusage() for detailed CPU usage statistics. Using microtime() The simplest method to measure execution time uses microtime(true) to get timestamps ? Script execution time: 0.023456 seconds In milliseconds: 23.456 ms Using getrusage() For detailed CPU usage statistics, getrusage() provides user time and system time separately ? The process used 15.625 ms for the computations Spent 0.312 ms during system calls ...

Read More
Showing 501–510 of 802 articles
« Prev 1 49 50 51 52 53 81 Next »
Advertisements