Php Variables
uchiki.net » tutorials » Php Variables
Variables are very common in PHP, it's almost impossible to write PHP without them. This tutorial teaches you the simplest form of including variables in your PHP web pages. There are more difficult ways to work with variables but they are kind of hard to explain with my English knowledge so far.Why do I need variables?
You have two different images and therefore two different titles and descriptions. In order two show them seperately you'll need two different files. But if you work with variables you'll only need one single file.How to add variables
Variables starts with a dollar sign followed by the name of the variable. The variable for the image is called "imageurl". You can of course call it whatever you like.
<?php $imageurl = $_GET['i']; ?>
<img src="<?=$imageurl ?>">
How variables work
If you simply open the PHP file (Example: http://yourserver/yourphpfile.php) you won't see anything. No images. Now add:?i=http://uchiki.net/downloads/tutorials/php-variables/surprise.jpg
behind the url. It should look like this:http://yourserver/yourphpfile.php?i=http://uchiki.net/downloads/tutorials/php-variables/surprise.jpg
And look! You can see a Poring! No, you're definitely not day dreaming, this is real!Explainations
By adding ?i you told your server (Let's call it Carl) to "activate" ($_GET) the variable linked to i which is $imageurl. Now Carl is looking for $imageurl and finds it here:<img src="<?=$imageurl ?>">
Carl thinks: Aha! I have to look for an image because "img src" tells me to open an image file. I just need to find the url to that image! And he finds the url behind ?i=:http://uchiki.net/tutorialsx/php-variables/surprise.jpg
Now you can change the image as often as you want just by changing the url behind ?i=. You can of course add as many variables as you want and the url address can be as long as you want. It just might look stupid if it's TOO long.Two or more variables
The link for two or more variables looks like this:http://yourserver/yourphpfile.php?i=value for i&x=value for x
? only needs to be added once (at the beginning). The variables in the url are linked by &.A variable can change its form and you're the one telling it how to change by adding your values.
