Search code examples
phpstring-concatenation

Concatenate string value with function return value


I have an error on the following code:

$name = "Bush Dvd Player";

print "<a href='?name".print str_replace(' ', '-', $name).".html' >Previous</a>";

On the page is shows the following:

Car-Parts-and-Accessories however not in a url format?


Solution

  • str_replace() returns a string that will be concatenated to the output string, therefore there is no need for the additional print, so just omit it:

    print "<a href='?name" . str_replace(' ', '-', $name) . ".html' >Previous</a>";
    

    Your old code would print out the result of str_replace, followed by the concatenation of the first string literal, along with the return value of print str_replace(' ', '-', $name), and then then 2nd string literal.