After looking around many websites and collecting different types of information I have created my own basics of PHP tutorial.
The first thing to know in PHP is obviously the open & close tags.
<?php this is the open tag for a php code, this lets the server know that a php code is about to begin.
?> this is the close tag for a php code, this lets the server know that the php code has now ended.
Everything in between the open tag and the close tag will be read as PHP code. The <?php tag can also be phrased as <? if desired.
The next thing to learn about is how to place comments within your code which will be ignored by the server and not displayed on your webpage. Comments are used for various reasons, they can be used as a refferance to what the code is doing so that when you go back later to edit the code you will know what everything is doing and what to edit. They can also be used if you are wanting to share the code with other people and want them to know whats going on at each part of the code and also if you want to include your name and terms of services within a code, there are two types of comments:
//This is a comment to be used one a single line.
/*
Using the method you can create
much larger comments which can go other mutiple lines and will still all be commented out
*/
Now that we know what the open & close tags are and also how to place comments into our codes now its time to move onto the echo & print statment. What this does it it outputs whatever you tell it to do and displays it as text on your webpage. There is a lot of debate about which is better to use or if there is any difference at all. Apparently in very large programs that are simply outputting text the ECHO statement will run slightly faster, but for the purposes of a beginner they are interchangeable so use which one you will find most comfortable to remember.
<?php
print "hello"; //this will display the text hello onto your website, notice that the hello is in quotation marks, when using the print or echo statment you must place the text you want to be displayed in quotation marks.
echo "hello";//this will display the text hello on your website just like the print statment.
?>
notice that each line ends with a semicolon, whenm your PHP code is longer than one line then you must seperate each line with a semicolon.
The next thing we are going to move onto are variables, variables are used to hold expressions or values. There are a couple of things that we must remember abiut variables, the first thing is that they must start with a $ sign which is then followed by the name of the variable. The second thing is that the name must begin with a letter the underscore character. The thrid thing is that the variable name should not contain spaces and only contain letters, numbers and the underscore charatcer. The final thing is that variable names are case sensative ( $My and $my would be too different variables).
$MyCar = "Audi"; //This creates a variable. The variable MyCar will now hold the value "Audi". Notice the quotation marks again.
$Age=7; //This also creates a variable. notice that there are no quotation marks as they must only be used on a text value.
Now we can use this variable to tell people what car we drive and learn to use variables with the print/echo statment:
$MyCar = "Audi";
print "Hello, I drive a $MyCar; //This will display Hello, I drive a Audi onto your webpage
Now we have the basics of Variables covered we can move onto Arrays. While Variables can hold one single piece of data, an array can hold a string of related data. An array can hold all your variable values under a single name and you can access the values by referring to the array name. Each element in an array has its own index so it can be found easily.
$friend[0] = "Justin"
$friend[1] = "Paul"
$friend[2] = "John"
$friend[3] = "Garry" //this creates an array
print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", ". $friend[3]; //this will print all the names of your friends from the array.
That array is arranged using integers as the key ( the key is the information between the [ ] brackets).
Arrays can also be arranged using text as the key:
$age["Justin"] = 45;
$age["Paul"] = 20;
$age["John"] = 50;
$age["Garry"] = 30; //this states another array using text as the key.
print "Paul is " . $age["Paul"] . " years old"; //this will display Paul is 20 years old.
Now we have the basics of Arrays its the time to move onto IF ELSE statements. Very often when you write code, you want to perform different actions for different decisions, you can use the IF ELSE statements to do this. Below is an example of an IF statement that would apply a senior's discount. If $over65 is false, everything within the {brackets} is simply ignored:
$over65 = true;
$price = 1.00;
if ( $over65 ) //this is the start of an IF statment. if over65 is false then everything in the brackets below is ignored.
{
$price = .90; //this states what the price will be if over65 is true.
}
print "your price is $" . $price; //this will print your price. If over65 is true it will output your price as .90 but if false then it will output as 1.00.
The if statment is used to execute some code if only the specified condition is true. Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.
However, sometimes just the IF statement isn't enough, you need the ELSE statement as well. When using just the IF statement the code within the brackets either will (true) or will not (false) be executed before carrying on with the rest of the program. When we add in the ELSE statement, if the statement is true it will execute the first set of code and if it is false it will execute the second (ELSE) set of code. Here is an example:
$over70 = true;
$cost = 3.00;
if ( $over70 ) //this states another IF statment. If false then everything in these brackets will be ignored.
{
$discount =.90; //This will be discounted from your price if over70 is true.
print "you have recieved our seniors discount. Your price is $" . //this will tell you your price if over 70 is true.
$price*$discount; //this will multiply your price by your discount if the over70 is true.
}
else // This states an else statement. If over70 is true then everything within these brackets will be ignored.
{
print "sorry you dont not qualify for our senior discount. Your price is $" . //this will display your price and tell you that over70 is false.
$price;
}
This is my basics of PHP tutorial, I hope you like it and I hope you like it and also that it helps people learn :)