1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

How do I pass variables in the URL in PHP 5?

By

Question: How do I pass variables in the URL in PHP 5?
If you pass variables in the URL in PHP4, and then move your scripts to a server running PHP5, you may notice they no longer work. Things such as www.yoursite.com/script.php?color=red in PHP4 would automatically assign $color = red.... how can you make it work in PHP5?

Answer: PHP5 is also capable of handling variables in this manor. The difference is that register_globals is turned OFF in php.ini by default in PHP5, and turned ON by default in PHP4. To make the passed variables work you have two options:

1.) Turn register_globals ON in the php.ini file

or

2.) Use $_GET['var'] In our example of www.yoursite.com/script.php?color=red you would use:


$color = $_GET['color']
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Troubleshooting
  5. Passing variables in PHP 5 in the URL

©2014 About.com. All rights reserved.