PHP Articles

Page 58 of 81

How to check whether an array is empty using PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 224 Views

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 More

How to create a copy of an object in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 140 Views

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 More

Merge two arrays keeping original keys in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

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 More

What is the use of the @ symbol in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Convert an object to associative array in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

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 More

PHP program to change date format

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

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 More

PHP program to add item at the beginning of associative array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 380 Views

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 More

Double not (!!) operator in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

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 More

How to convert a string into number in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

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

How to convert number to month name in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More
Showing 571–580 of 802 articles
« Prev 1 56 57 58 59 60 81 Next »
Advertisements