Skip to content
PHP Logo

How can a PHP variable's content be modified to remove spaces?

To remove spaces from a PHP variable's content, you can use the `str_replace()` function to replace all spaces with an empty string. This function takes three parameters: the string to search for (in this case, a space), the string to replace it with (an empty string), and the original string. This will effectively remove all spaces from the variable's content.

php
// Original variable with spaces
$originalVariable = "Hello, World!";

// Remove spaces from the variable
$modifiedVariable = str_replace(' ', '', $originalVariable);

// Output the modified variable
echo $modifiedVariable;