phpinterpolationquoting

Variable value is not being respected inside of a single quoted string


When I try to use the $city value in a string, it is not working.

$city = "vancouver";

$insert1 = "https://www.example.com/buy/vancouver/28130965/";    

$url2 = str_replace('/$city/', 'index.php?deal=', $insert1);

Output:

https://www.example.com/buy/vancouver/28130965/

Solution

  • You can interpolate variables only in double quotes. Use:

    str_replace("/$city/", 'index.php?deal=', $insert1);
    

    Or:

    str_replace('/' . $city . '/', 'index.php?deal=', $insert1);