PHP Articles

Page 59 of 81

How to create comma separated list from an array in PHP?

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

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 More

How to get a variable name as a string in PHP?

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

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 More

How to trim all strings in an array in PHP ?

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

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 More

PHP to check substring in a string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

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

How to add days to $Date in PHP?

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

In PHP, you can add days to a date using several methods. The most common approaches are using strtotime() with date() or the object-oriented DateTime functions. Method 1: Using strtotime() and date() The simplest approach uses strtotime() to parse date strings and add time intervals ? Displaying date... Date = 2019-11-11 Displaying updated date... 2019-12-01 Method 2: Using DateTime Functions For more object-oriented approach, use date_create(), date_add(), and date_format() functions ? Displaying Date...2019/11/11 Displaying Updated Date... 2019/12/06 Method 3: Using DateTime ...

Read More

How to convert string to boolean in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 834 Views

To convert string to boolean in PHP, you can use the filter_var() function with the FILTER_VALIDATE_BOOLEAN filter. This function recognizes specific string values and converts them to their boolean equivalents. Basic String to Boolean Conversion The filter_var() function converts string values to boolean based on predefined rules − bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(false) bool(false) bool(false) Using FILTER_NULL_ON_FAILURE Flag You can use the FILTER_NULL_ON_FAILURE flag to return null for invalid boolean strings instead of false − bool(true) bool(false) NULL NULL ...

Read More

How to get first day of week in PHP?

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

In PHP, you can get the first day of the week using the date() and strtotime() functions. The default first day varies by locale, but you can specify whether you want Monday or Sunday as the first day. Getting Monday as First Day By default, PHP considers Monday as the first day of the week when using "this week" − First day = Monday - 09/12/2019 Getting Sunday as First Day To get Sunday as the first day of the week, you can use different approaches − ...

Read More

How to get the first element of an array in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

In PHP, there are several methods to get the first element of an array. The approach depends on whether you're working with indexed arrays, associative arrays, or need to preserve the array pointer. Using array_values() for First Element The most reliable method works with both indexed and associative arrays ? First element: 150 Using reset() Function The reset() function moves the internal pointer to the first element and returns its value ? First element: 150 Using array_slice() Method Extract the ...

Read More

How to get random value out of an array in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 456 Views

In PHP, you can get a random value from an array using the array_rand() function. This function returns a random key from the array, which you can then use to access the corresponding value. Syntax array_rand(array, number) Parameters: array − The input array number − Optional. Number of random keys to return (default is 1) Getting Single Random Value Here's how to get one random value from an array ? Array values ... Value 1 = 150 Value 2 = 100 Value 3 = 120 ...

Read More

Concatenation of two strings in PHP program

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

In PHP, string concatenation is performed using the dot operator (.). This operator joins two or more strings together to create a single string. Syntax $result = $string1 . $string2; Example 1: Concatenating Simple Strings Let's start with a basic example of concatenating two string variables ? This will produce the following output ? First Name = Jack Second Name = Sparrow Concatenation = JackSparrow Example 2: Adding Spaces Between Strings You can also concatenate strings with spaces or other characters in between ? This will produce the following output ? Full Name: John Doe Hello, John Doe! Example 3: Concatenating Numbers and Strings PHP automatically converts numbers to strings during concatenation ?

Read More
Showing 581–590 of 802 articles
« Prev 1 57 58 59 60 61 81 Next »
Advertisements