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
PHP Articles
Page 57 of 81
How 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 MoreFunction Overloading and Overriding in PHP
Function overloading and overriding are important object-oriented programming concepts in PHP that allow you to create flexible and reusable code structures. Function Overloading in PHP Function overloading is a feature that permits creating several methods with a similar name that works differently from one another based on the type or number of input parameters it accepts as arguments. PHP achieves this through the magic method __call(). Example Let us now see an example to implement function overloading − Output This will produce the following output − Circle area: ...
Read MoreGenerating Random String Using PHP
In PHP, you can generate random strings using various methods depending on your security requirements. For basic random strings, you can use md5() with mt_rand(), while for cryptographically secure strings, use random_bytes(). Using md5() with mt_rand() This method creates a random string by hashing a random number and extracting a substring ? Displaying random string... 1c856 Using random_bytes() For cryptographically secure random strings, use random_bytes() with bin2hex() ? Displaying cryptographically secure random string... 335b83d9e9 Combining Both Methods Here's an example ...
Read MoreHow to access an associative array by integer index in PHP?
In PHP, associative arrays use string keys instead of numeric indices. To access an associative array by integer index, you need to first get the array keys using array_keys() and then use the integer index to access the corresponding key. Basic Access Method The most common approach is to extract all keys into an indexed array and then use integer indices to access them ? Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 120 key: s, value: 110 Modifying Values by Integer Index ...
Read More