Hello World
A small step for PHP (Hypertext Preprocessor) everywhere is just a simple “Hello World”. So we’ll out line a few basic commands here.
- Make sure you have a PHP enabled server on your computer. My preference is Wamp Server it installs a complete package with the latest PHP, MySQL tools and systems.
- Use your favorite processor to start a .php file. If you aren’t sure which allows you to do this, simply use notepad. If you use notepad when you save the file, choose all files from the list, then make your filename end in .php You should make sure that you save the .php file to your PHP testing server, generally on your local computer this folder will be called WWW.
- Second you need to tell the internet browser that your starting a section that will have PHP code inside of it. so you should use the basic identifiers to incase the code.
<?php ?>
Everything contained within the <?php and ?> will be considered PHP code. All functions inside will be processed before the website loads. For this reason if there is an error in the PHP code, the entire website will likely not work, and either show an error, or a blank page. For this reason its important to test all PHP pages before launching them.
- Now we’ll move on and create a new variable. Think algebra, ‘X=4‘ etc. In PHP all variables have always have a $ sign. Therefore it would be ‘$X=4‘. Finally you have to add one more thing to make this a complete variable declaration (actually making $X equal 4). You need to add a colon ; after. This lets PHP know that your done with your command. The complete code looks like this.
<?php $X=4;
?>
So now X is equal to 4. Of course the object of this lesson is to write “Hello World” so instead of making $X equal to 4 lets make it equal to “Hello World”.
<?php $X=”Hello World”;
?>
Quite easy to change isn’t it. PHP is unique in that it defines variables based on what you put place in it. This makes it much easier then many programs since you don’t often have to worry about what types of variables are.
- We’re nearing the finish of our short tutorial. Now you need to actually place the words “Hello World” onto the screen. This is done by using the ‘echo’ function. The code works like this ‘echo‘ ‘$variable‘ ‘;‘ this allows you to place whatever is in the variable onto the screen. So lets place that into our code from above and make the program work.
<?php $X=”Hello World”;
echo $X;
?>
- Now you need to make sure you saved this to a place on your PHP enabled server. Go to it with your favorite web browser and you should see the words “Hello World“. Feel free to change the words around and experiment with it.
- Common errors:
- Forgetting to place a ; after a statement.
- Capitalization can cause no end of problems. PHP is a case sensitive programming language, which means that you could have two variables, $X and $x and they could be two completely different things! You also need to call the functions by their correct name, for example if you said Echo instead of echo the above function wouldn’t have worked.
This is the end of the first PHP Tutorial
