If you've seen some WordPress blogs or online stores you've probably seen url-s in the form of http://somepage.com/product.php?id=4. These sites pass variables to scripts using the url. The script will scan the url for these and you can use them in a database query for example. This is the technology that allows WordPress templates to function, since you only need one page for all your posts.
Instead of creating a separate HTML page for each post we make, we enter our posts into a database. Each post is then assigned an id. We simply create one page, say posts.php and the code for that page contains a query, plus all the necessary HTML for displaying a post. This query will pull one specific post from the database and put the correct pieces where they should go in the template. We can tell the script which one to pick by passing it a variable in the url. If the value of this variable is 135 for example, the post that will be pulled will be the one with this id.
You can see right away how valuable this is, you can build truly dynamic pages with this method. Your data is better organized in a database, and you can change the way you display it by simply modifying the HTML code, no need to recreate any content whatsoever. So how is this done on a code level? Let's take a look!
You need to use a PHP file obviously, and after the extension you need to write the names and the values of the variables you want to pass in the following format: http://url.com/page.php?variable=value&variable=value. Notice that initially we use a question mark to separate the variable name passed from the extension, but after each subsequent variable passed we use an ampersand.
You can easily retrieve these variables in the code, referencing them with $_GET['variable']. For example, let's say the url you use is the following: http://scriptastique.com/post.php?post_id=345. In this case inside the PHP file the value of $_GET['post_id'] is 345.
Using the 'get' method you can pass strings, not just integers, so you could pass the name of the referring url, the name of a user, whatever you need. You should be aware though, that using the get method is unsafe if you want to send sensitive data. For example, you wouldn't want to pass a user's password from one page to another using the get method for many reasons, but mainly because it is displayed in the url bar.
For other things like telling a script which post you want to show, or which category you want the description for it's fine, and a very comfortable way of organizing a huge site into just a few files, in fact, I think this is one of the best features of PHP!
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.