Solutions for problems passing variables in PHP

Cannot pass variables in PHP? For some reason whatever you read in support forums don't work? Read this PHP tutorial for the solution!

Introduction to Passing Variables

There are 2 ways to pass variables in PHP: "GET" and "POST". You might recognize these from the form tag (<form method="GET">). The difference? GET passes the variables in the URL, and POST passes the variables hidden. Ever get the error "resubmit POST data" when you click back on your browser? This is POST in action: it is asking "do you want to send that hidden information again".

GET in PHP

You don't need a form to send variables in PHP. Simply use GET. The advantage to this is that you can save the data submitted to the page, because it is all in the URL. Just bookmarke the page. Here is how you do it:

http://www.domain.com/page.php?var1=value&var2=value2&var3=value3

That easy. var_ is the name of the variable, then equals, then the value you want to assign to that variable. You put a "?" before the first, then seperate the rest with "&".

Now, you can retrieve them in PHP. Usually, you can just enter this:

echo $var1;

Which will produce "value"

If globals for PHP are not enabled (keep reading for more information), you need to add a line:

$var1 = $_GET["var1"];

This is what people use for content management systems. The site URLs look like: http://www.domain.com/index.php?id=5 . The id=5 is read by a PHP script. Usually, this id matches a row in a MySQL database that has all of the information for that page.

POST in PHP

For PHP, POST data is usually sent from forms, so I'm going to go over how to retrieve your variables. First, you give a name to each of your <input name="varname"> tags. For each of these, you use this line of code to retrieve (where varname matches with the name of your input tag).

$varname = $_POST["varname"];

Do this for each input name, then you can use these variables in your script.

Globals

This is the most common problem. Most tutorials tell you that variables will automatically be available, but you can't use them. The variables are not being transfered in the URLs. The test? Type the line: echo $variable; (where variable is the name of the variable you are passing in the url). If nothing is displayed, then you need to use one of the following solutions.

Retrieve it in your code: This is the easiest, because you just add this line before you try to call the variable:

$variable = $_GET["variable"];

Thats it. You can use that variable now!

Enable Globals: Add this line to your .htaccess file (if you use apache). If you don't have this file, just make a blank file and call it .htaccess, then upload it to your server.

php_flag register_globals on

Thanks for reading, and I hope this helps. If you have any other solutions to common PHP problems, please submit a comment below!

fish oil benefits fish oil side effects best fish oil fish oil. | Looking for buying essays online? AnyEssays.com is at your disposal!