Chapter 4 :: Using variables
One of the most important aspects of PHP, and any programming language is variables. Variables can hold strings (text) and/or numbers.
A PHP script using variables would look like this:
<html>
<head>
<title>PHP using variables</title>
</head>
<body>
<?php
$number1 = 5;
$number2 = 6;
$sum = $number1 + $number2;
echo $sum;
?>
</body>
</html>
This script is very much like the script we did before. In PHP, to use a variable you do: $myvariable. You can put a dollar sign before any word to make it a variable. Here are some conditions for variable naming. Variables can have underscores(_), and can have capital and lowercase letters. Variables cannot have spaces.
Back to the script above. We create 3 variables in this script. First, we create a variable called number1. We set its value to 5. We then create a variable called number2. We set that variables value to 6. We then have a variable called sum. We set the value of the variable sum to $number1 + $number2. Because $number1 = 5, and $number2 = 6, this is like saying $sum = 5 + 6. So after that line is parsed, the value of $sum is 11.
In PHP, variables are not explititly typed. What that means is that you do not tell PHP what type of variable to create (string or number). PHP automatically knows what kind of variable to make it. Lets say you want to make a variable to hold a string value, heres what it would look like:
<?php
$mytext = 'This is my string variable';
?>
In that example, you will notice that the value of $mytext is enclosed within a single quote (') and a closed by a single quote ('). This declares a string variable. When ever you are creating a variable that will just hold text, you must always inclose it within double quotes (") or single quotes (').
Right now you're probably asking what the difference is if you quote a string variable with double quotes, which would look like this:
<?php
$mytext = "This is a test";
?>
Or with single quotes, which would look like this:
<?php
$mytext = 'This is a test';
?>
The fact is there is very little difference. If you try these examples, both will work perfectly. With that said, for this example, one was is correct and one way is wrong. When you are creating a variable that will just hold text, and will not have any variables within it, you should always use single quotes. When PHP sees statements with single quotes, is means there are no variables in it, therefore the PHP parser does not try to interpret variables within the statement, and the page is parsed a tiny bit faster.
Here are some examples of when you would use sinngle quotes and when it would be correct to use double quotes.
Here is the first example:
<?php
$mytext = 'This is just text, and there is no variable here.';
?>
In the example above, it is ok to use single quotes because the string is just plain text. Using double quotes in the example above would just cause the page to parse a tiny bit slower.
Here is another example"
<?php
$name = 'David';
$greeting = "Welcome to my site $name";
echo $greeting;
?>
This is an interesting example. You will first notice that the value of the $name variable is enclosed in single quotes. This is because there is only text in that variable, therefore it is the proper way to do it. Then you will notice that the contents of the $greeting variable are enclosed in double quotes. This is proper because within the value of $greeting, you are supplying a variable (the $name variable.). So the value of greeting becomes "Welcome to my site David", and that will display on the page if you try this example. If you put single quotes around the contents of $greeting, the output of the page will be "Welcome to my site $name". This is because everything within single quotes is interpreted as plain text.
Chapter 5 :: Using some basic conditional statements
In this chapter, you will learn some very important features of PHP, conditional statements. Conditional statements allow you to add logic to PHP scripts.
One of if not the most used conditional statement is called the "if" statement. The if statement allows you to say "If something is true, do this, else do this."
Here is the outline of an if statement:
if ([condition])
{
[do everything in here if condition is true]
}else{
[do everything in here if condition is false]
}
To better show how the if statement works, here is an actualy PHP example. Lets say we have a variable called name, and we want to see if that name is equal to "David". If the value of the name variable is David, we want the script to output "The name is David", and if the value of the name variable is not David, we want it to output "This name is not David". For this example, we will make the name variable equal to "David", so our condition will be true.
Here's how we would do this:
<html>
<head>
<title>What is the name?</title>
</head>
<body>
<?php
$name = 'David';
if ($name == 'David')
{
echo 'The name is David';
}else{
echo 'The name is not David';
}
?>
</body>
</html>
This example is pretty simple. To start the script off, we make the value of the $name variable equal to "David". We then start the if statement. We say "If the value of the name variable is equal to David, do the follwing". You will notice that it says $name == 'David'. In PHP, to check for equality, you use ==. If you use =, it will not work right because = sets things to different values. In this case, the condition is true, so the line:
echo 'The name is David';
is executed. If the name variable was not equal to David, the else would have came into play, and this line would have been executed:
echo 'The name is not David';
That is the end of this tutorial. Post if you have any questions.