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
Programming Articles
Page 1058 of 2547
What 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 MoreHow to get current function name in PHP?
In PHP, you can get the current function name using magic constants like __FUNCTION__ and __METHOD__. These constants provide different levels of information about the executing function or method. Using __FUNCTION__ Magic Constant The __FUNCTION__ constant returns only the function name without class information ? Base class function! Base class function declared final!string(7) "display" Derived class function! Base class function declared final!string(7) "display" Using __METHOD__ Magic Constant The __METHOD__ constant returns both class name and method name in the format ClassName::methodName ? Base ...
Read MoreHow to delete an array element based on key in PHP?
In PHP, you can delete an array element based on its key using the unset() function. This function removes both the key and its associated value from the array. Syntax unset($array_name['key']); unset($array_name[$index]); Example 1: Deleting from Indexed Array Here's how to delete an element from a numeric indexed array − Original Array: Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Mango ) After deleting index 1: Array ( [0] => Apple [2] => Orange [3] => Mango ) Example 2: Deleting from Associative Array For associative arrays, you can delete elements using their string keys −
Read More