PHP Articles

Page 24 of 81

Extract numbers after the last hyphen in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 910 Views

In PHP, you can extract numbers after the last hyphen in a string using various methods. The most straightforward approach is using the explode() function to split the string and access the last element. Using explode() with Array Index Split the string by hyphens and access the last element using its index ? 7898906756 Using array_pop() Method A more elegant approach using array_pop() to get the last element ? 7898906756 Using end() Function Another method using end() to get the ...

Read More

PHP How to cast variable to array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 689 Views

To cast a variable to array in PHP, use the array casting syntax. This is useful when you need to ensure a variable is always an array type, regardless of its original data type. Syntax The basic syntax for casting to array is ? $yourNewVariableName = (array)$yourVariableName; Example with Array Variable When casting an existing array, it remains unchanged ? Array ( [0] => Mike [1] => Sam [2] => David ) Example ...

Read More

PHP How to display array values with text "even" to be displayed for even index values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

In PHP, you can display array values with the text "even" for even index positions using a for loop with conditional logic. This technique checks if an index is divisible by 2 and assigns "Even" text or the actual value accordingly. Example The following example creates an array and displays "Even" for even index values ? Even 1 Even 3 Even How It Works The code uses the modulo operator (%) to check if an index is even. When $counter % 2 == 0 is true (for indices 0, ...

Read More

How to display array values with do while or for statement in my PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

In PHP, you can display array values using loop structures like do-while and for statements. These loops provide different approaches to iterate through array elements and display them. Syntax The basic syntax for a do-while loop is − do { // statements } while (condition); The basic syntax for a for loop is − for (initialization; condition; increment) { // statements } Using do-while Loop The do-while loop executes the code block at least once before checking the condition ? ...

Read More

What happens if ++ operator is used with string value in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

When you use the ++ operator with a string value in PHP, it performs an alphanumeric increment operation. For pure alphabetic strings, it increments the last character to the next letter in the alphabet. For numeric strings, it treats the string as a number and increments it mathematically. String Increment Behavior The increment operator follows these rules − For alphabetic characters: increments to next letter (a→b, z→aa) For numeric strings: converts to number and increments For mixed alphanumeric: increments the rightmost character Example The string modified value is=Joho ...

Read More

strlen() php function giving the wrong length of unicode characters ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 Views

The PHP strlen() function counts bytes, not characters, which causes incorrect results with Unicode characters that use multiple bytes. For accurate character counting with Unicode strings, use mb_strlen() instead. Why strlen() Fails with Unicode Unicode characters like accented letters (é, ñ, ȉ) often require 2-4 bytes in UTF-8 encoding. The strlen() function counts these bytes rather than the actual visible characters ? String: JohnSmȉth strlen() result: 10 mb_strlen() result: 9 Comparison of Methods Function What It Counts Unicode Support Result for 'JohnSmȉth' strlen() Bytes No ...

Read More

PHP Why does this && not trigger as false?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 130 Views

In PHP, the logical AND operator (&&) requires all conditions to be true for the overall expression to evaluate as true. If any single condition is false, the entire expression becomes false due to short-circuit evaluation. How && Works The && operator evaluates conditions from left to right and stops as soon as it encounters a false condition. This is called short-circuit evaluation ? The above condition is true Breaking Down the Logic Let's analyze why the above condition evaluates to true ? ...

Read More

CURL context options in PHP

Syed Javed
Syed Javed
Updated on 15-Mar-2026 483 Views

In PHP, CURL context options allow you to configure HTTP and FTP requests when using stream contexts with functions like file_get_contents() or fopen(). These options provide fine-grained control over request behavior without directly using the cURL extension. Note: CURL context options are available when the CURL extension was compiled using the --with-curlwrappers configure option (deprecated in newer PHP versions). Available CURL Context Options Option Description method HTTP method supported by the remote server. Defaults to GET. header Additional headers to be sent during request ...

Read More

PHP make sure string has no whitespace?

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

To check whether a string contains whitespace in PHP, you can use the preg_match() function with a regular expression pattern. This method detects any whitespace characters including spaces, tabs, and newlines. Syntax preg_match('/\s/', $yourVariableName); Example − Checking for Whitespace The following example demonstrates how to check if a string contains whitespace ? The name (John Smith) has whitespace Example − String Without Whitespace Here's an example with a string that contains no whitespace ? The username (JohnSmith) has no ...

Read More

How to add time in minutes in datetime string PHP?

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

In PHP, you can add minutes to a datetime string using the strtotime() function or the DateTime class. The strtotime() method provides a simple way to parse time expressions and add time intervals. Syntax $result = strtotime('datetime_string + X minute'); You can replace X with any integer value representing the number of minutes to add. Using strtotime() Here's how to add minutes to a datetime string using strtotime() ? 2020-10-30 10:15:20 Using DateTime Class Alternatively, you can use the DateTime class for more object-oriented ...

Read More
Showing 231–240 of 802 articles
« Prev 1 22 23 24 25 26 81 Next »
Advertisements