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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Convert an object to associative array in PHP
To convert an object to associative array in PHP, you can use several methods. The most common approaches are type casting with (array) and using json_encode() with json_decode(). Method 1: Using JSON Encode/Decode This method converts the object to JSON string first, then decodes it back as an associative array ? Before conversion: object(department)#1 (2) { ["deptname"]=> string(9) "Marketing" ["deptzone"]=> string(5) "South" } After conversion: array(2) { ["deptname"]=> ...
Read MorePHP program to change date format
In PHP, you can change date formats using several built-in functions like date(), strtotime(), and date_create(). These functions allow you to convert dates between different formats and perform date calculations. Using date() and strtotime() The strtotime() function converts a date string to a timestamp, which can then be formatted using date() ? Displaying date... Date = 2019-11-11 05:25 PM Displaying updated date... 2019-12-01 17:25 Using date_create() and date_format() The date_create() function creates a DateTime object, which can be formatted using date_format() ? Displaying ...
Read MorePHP program to add item at the beginning of associative array
In PHP, you can add an item at the beginning of an associative array using the array_unshift() function. However, this function resets numeric keys and converts the array to indexed format for new elements. Using array_unshift() with String Keys When using array_unshift() on an associative array with string keys, the new element gets a numeric index ? Initial Array... Array( [p] => 150 [q] => 100 [r] => 120 [s] => 110 ...
Read MoreDouble not (!!) operator in PHP
In PHP, the double not operator (!!) is used to convert any value to its boolean equivalent. The first exclamation mark (!) negates the value, converting it to boolean and flipping its truthiness. The second exclamation mark (!) negates it again, effectively converting the original value to a clean boolean true or false. How It Works The double not operator works in two steps − First ! converts the value to boolean and negates it Second ! negates the result again, giving the original boolean value Example 1: String to Boolean Here's how ...
Read MoreHow to convert a string into number in PHP?
PHP provides several methods to convert strings into numbers. The most common approaches are type casting, built-in functions like intval() and floatval(), and adding zero to the string. Using Type Casting Type casting is the most direct method to convert a string to a specific numeric type ? Original String: 150 Converted Integer: 150 Using floatval() Function For decimal numbers, use floatval() to convert strings to floating-point numbers ? String: 100.56 Number (Float): 100.56 Using intval() Function The intval() function ...
Read MoreHow to convert number to month name in PHP?
In PHP, you can convert a month number to its corresponding month name using several built-in functions. The most common approaches are using date() with mktime() or the DateTime class. Using date() with mktime() The mktime() function creates a timestamp, which can then be formatted using date() to get the month name ? Month number: 11 Full month name: November Short month name: Nov Using DateTime Class The DateTime class provides a more object-oriented approach for month conversion ? Month number: 12 Full ...
Read MoreHow to create comma separated list from an array in PHP?
Creating a comma-separated list from an array in PHP is efficiently done using the built-in implode() function. This function joins array elements with a specified delimiter string. Syntax implode(separator, array) Parameters separator: The string to use as a delimiter between array elements (optional, defaults to empty string). array: The array whose elements you want to join. Basic Example Here's a simple example of creating a comma-separated list from a numeric array − Original array: Array ( [0] => 100 [1] => 200 [2] => 300 [3] => 400 [4] => 500 ) Comma separated list: 100, 200, 300, 400, 500 Handling Whitespace in Array Elements When array elements contain extra whitespace, you may want to trim them before creating the list −
Read MoreHow to get a variable name as a string in PHP?
In PHP, getting a variable name as a string is useful for debugging and dynamic programming. There are several approaches to retrieve variable names, each with different use cases and limitations. Using Variable Variables The simplest approach uses PHP's variable variables feature to demonstrate the relationship between variable names and their string representations ? This will produce the following output ? This is it! Using $GLOBALS Array A more practical approach searches through the $GLOBALS array to find a variable by its value and return its name ? ...
Read MoreHow to trim all strings in an array in PHP ?
In PHP, you can trim all strings in an array by removing leading and trailing whitespace from each element. There are several methods to achieve this, with array_map() being the most common approach. Using array_map() The array_map() function applies the trim() function to each element of the array ?
Read MorePHP to check substring in a string
In PHP, you can check if a substring exists within a string using the strpos() function. This function returns the position of the first occurrence of a substring, or false if not found. Using strpos() Function The strpos() function is case-sensitive and returns the numeric position of the substring or false if not found ? String = How I Met Your Mother Substring = Mother Substring found successfully Example with Substring Not Found Here's an example where the substring is not present in the string ? ...
Read More