Server Side Programming Articles

Page 1015 of 2109

Remove comma from a string in PHP?

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

In PHP, you can remove commas from a string using the str_replace() function. This function searches for a specific character or substring and replaces it with another value or removes it entirely. Syntax str_replace(search, replace, subject) Parameters: search − The character or string to find (comma in our case) replace − What to replace it with (empty string to remove) subject − The original string to process Example Let's remove commas from a string containing a person's name ? The actual value is = John, ...

Read More

PHP preg_split with hyphen?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

In PHP, you can use preg_split() to split strings at specific patterns, including hyphens. The preg_split() function uses regular expressions to define where the string should be split, giving you more control than simple explode(). Basic Hyphen Splitting To split a string at every hyphen, use a simple pattern ? Array ( [0] => apple [1] => banana [2] => cherry ) Conditional Splitting with Lookahead For more complex splitting, such as splitting only at hyphens followed ...

Read More

How to pass reference parameters PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 607 Views

In PHP, you can pass parameters by reference using the & symbol before the parameter name in the function definition. This allows the function to modify the original variable instead of working with a copy. Syntax To pass a parameter by reference, prefix the parameter with & in the function definition ? function functionName(&$parameter) { // Modify the original variable } Example Here's how to pass a reference parameter to modify the original variable ? The actual value is = 900 The modified ...

Read More

Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

In PHP, you can use preg_replace() with regular expressions to replace specific characters from matched strings, even when you don't know how many instances exist. This is particularly useful for parsing delimited data where certain delimiters need conditional replacement. Problem Example Let's say we have this input string where we want to replace the pipe character "|" only when it appears between two numbers − FirstName|John |LastName|Smith|SalaryProvided|2000|5000 The expected output should replace the "|" between 2000 and 5000 with a space − FirstName|John |LastName|Smith|SalaryProvided|2000 5000 Solution Using preg_replace() The ...

Read More

PHP Inherit the properties of non-class object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

In PHP, you can inherit the structure (property names) of a non-class object while resetting the property values to null. This is useful when you need to create a new object with the same properties but empty values. Method 1: Using clone and foreach Clone the original object and then iterate through its properties to set them to null − Original object: object(stdClass)#1 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } Cloned object: object(stdClass)#2 (2) { ["Name"]=> string(4) "John" ...

Read More

Reading and storing a list as an array PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

In PHP, you can read and store a space-separated string of values as an array using several methods. The most efficient approach is using the explode() function, though you can also manually parse using loops. Using explode() Function The simplest and most efficient method to convert a space-separated string into an array ? 10 20 30 40 50 60 Using Manual Parsing with Loop You can also manually parse the string character by character to build the array ? 10 20 30 40 ...

Read More

Display game is in BUY or TRY mode PHP conditions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 109 Views

In PHP, you can determine if a game is in BUY or TRY mode using conditional statements like if, else if, and else. This is useful for implementing different game features based on the user's access level. Example The following PHP code demonstrates how to check game mode and display appropriate messages − Output GAME IS IN BUY MODE Enhanced Example with Different Modes You can also test multiple scenarios by changing the game mode − Output Testing mode: TRY - ...

Read More

Manipulate for loop to run code with specific variables only PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

In PHP, you can manipulate a for loop to execute specific logic based on variable values using conditional statements. This allows you to process array elements differently based on certain criteria. Let's say we have an array of marks and want to modify values less than 40 by doubling them, while keeping other values unchanged ? Original marks: Array ( [0] => 45 [1] => 67 [2] => 89 [3] => 34 [4] ...

Read More

PHP fetch a specific value that begins with a given number from a string with letters and numbers?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 227 Views

In PHP, you can extract a specific value that begins with a given number from a string containing letters and numbers using substr() combined with strpos(). Syntax substr(string, start_position, length) strpos(string, search_value) Example Let's extract a value beginning with "989" from a mixed string − The actual value is: 1045632245555fghjklm67535454663443gfdjkbvc9890000006777743 The filtered value is: 9890000006777743 How It Works The solution works in two steps − strpos($value, "989") − Finds the position where "989" first occurs in the string substr($value, position, 16) − Extracts ...

Read More

Generate random number from 0 – 9 in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 583 Views

To generate a random number from 0 to 9 in PHP, you can use the rand() function or select randomly from a string of digits. PHP provides several approaches to accomplish this task. Using rand() Function The simplest method is to use rand(0, 9) which directly generates a random integer between 0 and 9 ? Random number: 7 Using String Selection Method You can also store digits in a string and randomly select one character using rand() with strlen() ? The random value=3 ...

Read More
Showing 10141–10150 of 21,090 articles
Advertisements