Skip to content
PHP Logo

How can PHP variables be properly concatenated with strings in a header(Location) function?

When concatenating PHP variables with strings in a header(Location) function, you need to make sure to properly concatenate them using the dot (.) operator. This ensures that the variables are correctly inserted into the string without causing any syntax errors. Additionally, it's important to properly encode any variables that may contain special characters using urlencode() to prevent any issues with the URL.

php
// Example code snippet for concatenating PHP variables with strings in a header(Location) function
$variable1 = "example";
$variable2 = "123";

// Properly concatenate the variables with the string using the dot (.) operator
header("Location: http://www.example.com/?var1=" . urlencode($variable1) . "&var2=" . urlencode($variable2));