What are the best practices for creating and handling URLs with PHP variables in web development?

When creating and handling URLs with PHP variables in web development, it is important to properly sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP's urlencode() function to encode the variables before appending them to the URL. Additionally, it is recommended to use a combination of GET parameters and routing techniques to maintain clean and user-friendly URLs.

// Example of creating a URL with PHP variables
$variable1 = "example";
$variable2 = "123";

$url = "http://www.example.com/page.php?var1=" . urlencode($variable1) . "&var2=" . urlencode($variable2);

echo $url;