So I have decided it would be good to impart my knowledge of php on the forums in several different threads. Why do so? Well because teaching something forces you to relearn and make sure you know for certain.
So lets start at the beginning.
How do you make a php file? Its simple, open any ide or even note pad and put the follow code in
These essentially function as the opening and closing brackets that tell the server to process whatever is between using PHP, you alternatively can use
for the same result, however i prefer to only use that when space is an issue which it isn't when you are starting to code php.
Now you will probably have a lot of questions, so for now lets answer a very basic one, how do I make the screen say stuff. Well its simple the echo function. This serves as the print function if you are familiar with java or C. I'll also use this to demonstrate proper syntax. So to display the text "hello sythe" you would use the echo function like this:
Code:
<?php
echo "Hello Sythe";
?>
The webpage would display the text Hello Sythe exactly like that. The semicolon that proceeds the echo function is telling PHP that that is the end of the line and separates the commands if you are jamming them all onto one line, but that will not be the case for our goals, however after every command you will want one of these or you will get an error.
Also if you put quotation marks it will literally echo what you type, if you want to use a variable statement you don't put quotation marks.
That brings us the the next part of the tutorial, variables. Variables in PHP don't need to be predeclared like in C or Java, you can call them when you wish to use their values if you are using a single file for a webpage. If you wanted to echo the same phrase using a variable named message it would look like this:
Code:
<?php
$message = "Hello Sythe";
echo $message;
?>
If your variables are numbers (integers) you can also perform mathematical operations to them like so:
Code:
<?php
$x = "5";
$y = "7";
$z = $x + $y;
echo $z;
?>
The result would be the number 12 appearing on the screen. If you want to multiply you would use * (shift 8) to divide you use /.
If you want echo multiple variables you would do it like this
Code:
<?php
$x = "5";
$y = "7";
$z = $x + $y;
echo $x . " + " . $y . " = " . $z;
?>
The result should be 5 + 7 = 12
The . actually tells php to echo both of the variables together (called the concatenation function), but its important to remember that it DOES NOT ADD ANY SPACES BETWEEN THE VARIABLES so if you put echo "hi" . $z; it would appear as hi12.
Lastly we are going to look at the if statement which is very important to dynamic web pages. I will explain some more syntax aswell that will help you be more efficient with PHP. Using if there are 3 main commands: IF which means that the code is executed if the following statement applies; ELSEIF which goes AFTER the initial if which is the code used if something specific other than the original if is true; ELSE is the last of the 3 and executes when the IF and ELSEIF's aren't true. Lets use an example with the date then talk about the syntax.
(don't worry about the $d = date("D"); that is the date function and is a global one)
Code:
<?php
$d = date("D");
if ($d == "Mon")
{
echo "Hungover Day";
}
elseif ($d == "Wed")
{
echo "Hump Day";
}
else
{
echo "Other Day";
}
?>
On Monday you would see Hungover Day, Wednesday you would see Hump day and every other day you would see Other Day.
A note on syntax here is that the brackets { and } are NOT necessary, but it makes the coding easier to read, and easier to find mistakes if you have a lot of code to go through. If you use a { you MUST have a } after it or you will run into a syntax error.
Going back to IF,ELSEIF and ELSE. For each if statement you only need the if statement, the others are not needed by syntax in your code. It is very common to use both IF and ELSE. ELSEIF is sort of an uncommon statement that applies to specific situations really.
Now looking at the actual statement if ($d == "Mon"), this is like when using echo, if you are using a variable you don't need to use quotation marks but if you aren't you do. The statement also MUST be closed in brackets. Lastly you NEED two equal signs to allow PHP to know you are comparing the values and not give you an error.
You can also say if something is not equal by using: if ($d != "Mon"), and if you have numerical variables you can use greater than or equal to and less than or equal to by using the following respectively:
if ($x >= $y) (greater than)
if ($x <= $y) (less than)
IF YOU TRY TO COMPARE TEXT USING THE ABOVE TWO YOU WILL GET AN ERROR.
Okay I lied one more thing, comments its simple and easy type // before anything and it becomes a comment and ignored by php. If you have a multi line comment then you can use /* to open and */ to close. so it would look like this:
Code:
<?php
/*THIS IS A
MULTI LINE
COMMENT*/
echo "hello sythe"; // THIS IS A ONE LINE COMMENT
?>
That's all for now i'll get into more specifics later.