Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 544 of 840
Creating anonymous objects in PHP
Starting from PHP 7, you can create anonymous classes without explicitly defining a class name. Anonymous classes are useful for creating one−time objects, implementing interfaces on the fly, or extending existing classes temporarily. Basic Anonymous Class Here's how to create a simple anonymous class ? Hello from Anonymous Object Object class: class@anonymous Extending a Parent Class Anonymous classes can extend existing classes ? From anonymous child: Parent Is instance of ParentClass? bool(true) Implementing Interfaces Anonymous classes can implement interfaces for ...
Read MoreWhat is the difference between array_merge and array + array in PHP?
In PHP, both array_merge() and the + operator can combine arrays, but they handle duplicate keys differently. The + operator preserves values from the left array when keys conflict, while array_merge() overwrites duplicate string keys with values from the right array. Using Array + Operator The + operator keeps the original values when duplicate keys exist − array(8) { ["p"]=> string(3) "150" ["q"]=> string(3) "100" ["r"]=> string(3) "120" ...
Read MoreWhat is the difference between echo, print, and print_r in PHP?
In PHP, echo, print, and print_r are used to display output, but they serve different purposes and have distinct characteristics. Key Differences Function Return Value Parameters Purpose echo void (no return) Multiple allowed Display strings/variables print 1 (always) Single parameter only Display strings/variables print_r true on success Single parameter Display array/object structure Basic Usage Here's how each function works with simple strings and variables − Hello World Value: 25 Hello World Value: 25 Print returned: 1 print_r with string: Hello ...
Read MoreReturn all dates between two dates in an array in PHP
In PHP, you can return all dates between two given dates in an array using various approaches. The most common method involves using strtotime() and date() functions to iterate through dates. Using strtotime() and date() This approach converts dates to timestamps and iterates through each day ? array(11) { [0]=> string(10) "10-11-2019" [1]=> string(10) "11-11-2019" [2]=> string(10) "12-11-2019" [3]=> string(10) "13-11-2019" [4]=> ...
Read MoreReset keys of array elements using PHP ?
To reset keys of array elements in PHP, you can use the array_values() function. This function returns a new indexed array with numeric keys starting from 0, preserving the original order of values while discarding the original keys. Syntax array_values(array $array) Parameters $array − The input array whose keys need to be reset. Return Value Returns an indexed array with numeric keys starting from 0. Example 1: Resetting String Keys Here's how to reset associative array keys to numeric indexes ? Original array: array(4) ...
Read MoreHow to remove the first character of string in PHP?
In PHP, you can remove the first character of a string using several methods. The most common approaches are substr() for general use and ltrim() for removing specific characters. Using substr() The substr() function is the most straightforward method to remove the first character from any string ? This will produce the following output ? Before removing the first character = Test After removing the first character = est Using ltrim() The ltrim() function removes specific characters from the beginning of a string. Use this when you know ...
Read MoreRemoving Array Element and Re-Indexing in PHP
In PHP, when you remove an element from an array using unset(), the original array keys are preserved, which can create gaps in the index sequence. To re-index the array with consecutive numeric keys starting from 0, you can use the array_values() function. Example The following example demonstrates removing an array element and re-indexing − Original Array... Index 0 = John Index 1 = Jacob Index 2 = Tom Index 3 = Tim After removing index 1 (without re-indexing)... Index 0 = John Index 2 = Tom Index 3 = Tim ...
Read MoreRemove new lines from string in PHP
In PHP, you can remove newlines from a string using several built-in functions. The most common approaches are using preg_replace() for pattern-based removal and str_replace() for simple character replacement. Using preg_replace() The preg_replace() function removes all types of newlines using regular expressions ? Original string with newlines: Demo text for reference After removing newlines: Demo text for reference Using str_replace() The str_replace() function targets specific newline characters directly ? ...
Read MoreHow to get parameters from a URL string in PHP?
In PHP, you can extract parameters from a URL string using the parse_url() function combined with parse_str(). The parse_url() function breaks down a URL into its components, while parse_str() parses the query string into an associative array. Syntax parse_url($url, $component) parse_str($query_string, $result_array) Basic Example Here's how to extract specific parameters from a URL ? Email = example12@domain.com Extracting Multiple Parameters You can access all parameters from the same URL ? Name = demo Email = example12@domain.com Error ...
Read MoreHow to get file name from a path in PHP?
In PHP, you can extract the file name from a file path using several built-in functions. The most common approaches are using pathinfo() and basename() functions. Using pathinfo() The pathinfo() function returns an array containing information about a file path ? main.php Using basename() The basename() function extracts the filename from a path. You can optionally specify a suffix to remove ? main Comparison Function Returns Can Remove Extension pathinfo() Array with detailed path info Access ...
Read More