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 1059 of 2547
Function 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 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 More