How to define PHP code
The Basics
Creating a PHP page is just as simple as saving the page as filename.php as opposed to filename.html. So create a PHP file for practicing the areas covered in this tutorial so you can try the examples for yourself. As long as your host has PHP support then your page should function properly.
The PHP code must begin with a "<?php" tag and end with "?>" so here is a short example;
Code: Select all
<?php
//Code goes here
?>
We comment in PHP by using the double slashes "//" Anything after those slashes on that line are commented. They don't do anything to the code, but if you go back to the code after a long time, or give the code to someone else, it will show what you were trying to achieve with that bit of code. It may seem boring to write a comment after everyline but it will help you in the long run. Not only is it a future reference, but by thinking through the problem in english words, you are more likely to understand how to solve it.
also comment in PHP using a "/*" to mark the start of the commented text and "*/" to mark the end. I only tend to use this if i want to stop a block of code from operating temporarily. Rather than deleting it, I comment it and then uncomment it later. Using the "/* */" syntax to comment allows you to comment multiple lines as opposed to just one line using "//".
Code: Select all
<?php
//This line is commented
This line isn't.
/* These
Lines
Are
Commented */
?>
The most simple way of outputting text is using the echo function.
Code: Select all
<?php
echo "Hello World";
?>
If you are trying to output the text "It was Maggie's" then we need to use double quotes. If we use single quotes, then it would recognise the apostrophe after Maggie as the closing single quote and then the PHP would wonder why there was an "s" and another single quote after that. In otherwords, it will throw up some error messages.
Similarly, if you were trying to output;
Maggie said "That is mine."
Then we will use single quotes.
There is another way around this and that is to use a backslash as an escape character. Look at this bit of code.
Code: Select all
<?php
echo "Maggie said \"That is mine.\"";
?>
The Semi Colon Of doom
You may have also noticed the semi-colon ";" at the ends of the echo function. This is a PHP syntax perk you are going to have to get used to. I am still forgetting to put the semi-colons at the end of my lines and almost half of my errors are specifically that. Basically you need to tell PHP "Hey, this is the end of this line, nothing comes after this so move onto the next line and continue processing". If i done this;
Code: Select all
<?php
echo "I am trying to cause an error"
echo "Will it throw up an error?";
?>
A new line
The final trick of outputting data in this tutorial is taking a new line. Look at this example;
Code: Select all
<?php
echo "line 1";
echo "line 2";
?>
Code: Select all
Line 1
Line 2
Code: Select all
Line 1Line 2
Code: Select all
<?php
echo "Line 1\n";
echo "Line 2";
?>
Code: Select all
<?php
echo "Line 1\nLine 2";
?>
So I hope you have enjoyed this tutorial and it has taught you something. Maybe I even helped you create your very first PHP page. Please leave any comments, or suggestions to my tutorial and I will try and implement them into it, if certain areas are vague
Anish