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 on Trending Technologies
Technical articles with clear explanations and examples
How 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 MoreHow to check whether an array is empty using PHP?
In PHP, there are several methods to check whether an array is empty. The most common approaches include using the empty() function and the count() function. Using empty() Function The empty() function returns true if the array has no elements −
Read MoreHow to create a copy of an object in PHP?
In PHP, you can create a copy of an object using the clone keyword. This creates a shallow copy of the object, copying all properties to a new instance while maintaining the same values. Basic Object Cloning The simplest way to copy an object is using the clone keyword − Original: Jack Kevin Copy: Tom Ryan Cloning Objects with Constructor You can also clone objects that have constructors. The clone will preserve the initialized values − Demo Object ( ...
Read MoreMerge two arrays keeping original keys in PHP
In PHP, you can merge two arrays while preserving their original keys using the array union operator (+). This operator combines arrays by keeping all unique keys from both arrays, with values from the left array taking precedence when duplicate keys exist. Syntax The basic syntax for merging arrays with original keys is − $result = $array1 + $array2; Example 1: Merging Arrays with Different Keys Here's how to merge two associative arrays with unique keys − array(8) { ["p"]=> string(3) "150" ...
Read MoreWhat is the use of the @ symbol in PHP?
PHP supports the error control operator (@), which suppresses error messages when prepended to an expression. Any error messages that might be generated by that expression are ignored, though the script continues executing. Basic Syntax The @ symbol is placed directly before the expression that might generate an error ? PHP Notice: Undefined variable: undefined_array in /home/cg/root/6985034/main.php on line 3 Script continues executing Common Use Cases File Operations Suppressing file operation errors while providing custom error handling ? Failed to read file ...
Read MoreConvert 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 More