How can variables be properly used within single quotes in PHP functions like str_replace?

When using variables within single quotes in PHP functions like str_replace, the variable will not be parsed and will be treated as a literal string. To properly use variables within single quotes, you can concatenate the variable with the string using the period (.) operator. This will ensure that the variable's value is correctly inserted into the string.

// Incorrect way to use variable within single quotes
$old_string = 'hello';
$new_string = 'world';
$text = 'Replace $old_string with $new_string';
$result = str_replace('$old_string', '$new_string', $text);
echo $result; // Output: Replace $old_string with $new_string

// Correct way to use variable within single quotes
$old_string = 'hello';
$new_string = 'world';
$text = 'Replace ' . $old_string . ' with ' . $new_string;
echo $text; // Output: Replace hello with world