A nameless razor fusses
LEARN TO CODE
Tuesday, 27 August 2013
Monday, 26 August 2013
IF STATEMENTS IN PHP TUTORIAL 6
Conditional Logic uses the "IF" word a lot. For the most part, you use Conditional Logic to test what is inside of a variable. You can then makes decisions based on what is inside of the variable. As an example, think about the username again. You might have a variable like this:
$User_Name = "My_Regular_Visitor";
The text "My_Regular_Visitor" will then be stored inside of the variable called $User_Name. You would use some Conditional Logic to test whether or not the variable $User_Name really does contain one of your regular visitors. You want to ask:
"IF $User_Name is authentic, then let $User_Name have access to the site."
In PHP, you use the "IF" word like this:
if ($User_Name == "authentic") {
//Code to let user access the site here;
}
Without any checking, the if statement looks like this:
if ( ) {
}
You can see it more clearly, here. To test a variable or condition, you start with the word "if". You then have a pair of round brackets. You also need some more brackets - curly ones. These are just to the right of the letter "P" on your keyboard (Well, a UK keyboard, anyway). You need the left curly bracket first { and then the right curly bracket } at the end of your if statement. Get them the wrong way round, and PHP refuses to work. This will get you an error:
if ($User_Name = = "authentic") }
//Code to Let user access the site here;
{
And so will this:
if ($User_Name == "authentic") {
//Code to Let user access the site here;
{
The first one has the curly brackets the wrong way round (should be left then right), while the second one has two left curly brackets.
In between the two round brackets, you type the condition you want to test. In the example above, we're testing to see whether the variable called $User_Name has a value of "authentic":
($User_Name = = "authentic")
Again, you'll get an error if you don't get your round brackets right! So the syntax for the if statement is this:
if (Condition_or_Variable_to_test) {
//your code here;
}
In the next lesson, we'll use if statements to display an image on the page.
We'll use the print statement to "print out" HTML code. As an example, take the following HTML code to display an image:
<IMG SRC =VISHWA.jpg>
Just plain HTML. But you can put that code inside of the print statement:
print ("<IMG SRC =images/VISHWA.jpg>");
When you run the code, the image should display. Of course, you'll need an image called VISHWA.JPG, and in a folder called images.
You can find these amongst the files you can download for this course, in the folder called images.
Copy the images folder to your www (root) directory. Then try the following script:
<?PHP
print ("<IMG SRC =images/VISHWA.jpg>");
?>
Save your script to the same folder as the images folder
ADDITION IN PHP TUTORIAL 5
You don't have to use variable names to add up. You can do this:
print (10 + 20 + 30);
Or even this:
$number = 10;
print ($number + 30);
But the point is the same - use the plus symbol (+) to add up.
In the few next parts, you'll learn how to Subtract, Divide and Multiply.
similarly u can do for multiplication, addition,sub,division.
similarly u can do for multiplication, addition,sub,division.
Concatenation IN PHP TUTORIAL 4
You can join together direct text, and whatever is in your variable. The full stop (period or dot, to some) is used for this. Suppose you want to print out the following "My variable contains the value of 10". In PHP, you can do it like this:
<?php
$first_number = 10;
$direct_text = 'My variable contains the value of ';
$direct_text = 'My variable contains the value of ';
print($direct_text . $first_number);
?>
So now we have two variables. The new variable holds our direct text. When we're printing the contents of both variables, a full stop is used to separate the two. Try out the above script, and see what happens. Now delete the dot and then try the code again. Any errors?
You can also do this sort of thing:
<?php
$first_number = 10;
print ('My variable contains the value of ' . $first_number);
?>
This time, the direct text is not inside a variable, but just included in the Print statement. Again a full stop is used to separate the direct text from the variable name. What you've just done is called concatenation. Try the new script and see what happens.
WORKING OUT WITH VARIABLES TUTORIAL 3
First, we'll take a look at how to display what's in your variables. We're going to be viewing our results on a web page. So see if you can get this script working first, because it's the one we'll be building on. Using a text editor like Notepad, or your PHP software, type the following. (You can copy and paste it, if you prefer. But you learn more by typing it out yourself - it doesn't really sink in unless you're making mistakes!)
<html>
<head>
<title>Variables - Some Practice</title>
</head>
<body>
<head>
<title>Variables - Some Practice</title>
</head>
<body>
<?php print("It Worked!"); ?>
</body>
</html>
</html>
When you've finished typing it all, save the page as variables.php. Then Run the script. Remember: when you're saving your work, save it to the WWW folder, as explained here. To run the page, start your browser up and type this in the address bar:
http://localhost/variables.php
If you've created a folder inside the www folder, then the address to type in your browser would be something like:
http://localhost/FolderName/variables.php
If you were successful, you should have seen the text "It worked!" displayed in your browser. If so, Congratulations! You have a working server up and running! (If you're using Wampserver, you should see an icon in the bottom right of your screen. Click the icon and select Start All Services from the menu.)
The PHP script is only one line long:
<?php print("It Worked!"); ?>
The rest of the script is just plain HTML code. Let's examine the PHP in more detail.
We've put the PHP in the BODY section of an HTML page. Scripts can also, and often do, go between the HEAD section of an HTML page. You can also write your script without any HTML. But before a browser can recognise your script, it needs some help. You have to tell it what kind of script it is. Browsers recognise PHP by looking for this punctuation (called syntax):
<?php ?>
So you need a left angle bracket ( < ) then a question mark ( ? ). After the question mark, type PHP (in upper or lowercase). After your script has finished, type another question mark. Finally, you need a right angle bracket ( > ). You can put as much space as you like between the opening and closing syntax.
To display things on the page, we've used print( ). What you want the browser to print goes between the round brackets. If you're printing direct text, then you need the quotation marks (single or double quotes). To print what's inside of a variable, just type the variable name (including the dollar). Finally, the line of code ends as normal - with a semi-colon (;). Another way to display things on the page is to use an alternative to print() – echo( ).
Now let's adapt the basic page so that we can set up some variables. We'll try some text first. Keep the HTML as it is, but change your PHP from this:
<?php print("It Worked!"); ?>
To this:
<?php
print("It Worked!");
?>
OK, it's not much of a change! But spreading your code out over more than one line makes it easier to see what you're doing. Now, it's clear that there's only one line of code - Print. So add this second line to your code (the one in red):
<?php
$test_String = "It Worked!";
print("It Worked!");
?>
We've set up a variable called $test_String. After the equals sign, the text "It Worked!" has been added. The line is then ended with a semi-colon. Don't run your script yet. Change the Print line to this:
print($test_String);
Then add some comments ...
<?php
//--------------TESTING VARIABLES------------
//--------------TESTING VARIABLES------------
$test_String = "It Worked!";
print($test_String);
?>
Comments in PHP are for your benefit. They help you remember what the code is supposed to do. A comment can be added by typing two slashes. This tells PHP to ignore the rest of the line. After the two slashes, you can type anything you like. Another way to add a comment, is like this:
<?php
/* --------------TESTING VARIABLES------------
Use this type of comment if you want to spill over to more than one line.
Notice how the comment begin and end.
*/
Use this type of comment if you want to spill over to more than one line.
Notice how the comment begin and end.
*/
$test_String = "It Worked!";
print($test_String);
?>
Whichever method you choose, make sure you add comment to your code: they really do help. Especially if you have to send your code to someone else!
But you can now run the script above, and test it out.
How did you get on? You should have seen that exactly the same text got printed to the page. And you might be thinking - what's the big deal? Well, what you just did was to pass some text to a variable, and then have PHP print the contents of the variable.
PUTTING TEXT INTO VARIABLES TUTORIAL 2
you saw how to put numbers into variables. But you can also put text into your variables. Suppose you want to know something about the coats you own. Are they Winter coats? Jackets? Summer coats? You decide to catalogue this, as well. You can put direct text into your variables. You do it in a similar way to storing numbers:
$coats1 = "Winter Coats";
Again, our variable name starts with a dollar sign ($). We've then given it the name coats1. The equals sign follows the variable name. After the equals sign, however, we have direct text - Winter Coats. But notice the double quotation marks around our text. If you don't surround your direct text with quotation marks, then you'll get errors. You can, however, use single quotes instead of double quotes. So you can do this:
$coats1 = 'Winter Coats';
But you can't do this:
$coats1 = 'Winter Coats";
In the above line, we've started with a single quote and ended with a double quote. This will get you an error.
We can store other text in the same way:
$coats2 = "Jackets";
$coats3 = "Summer Coats";
$coats3 = "Summer Coats";
The direct text will then get stored in the variable to the left of the equals sign.
So, to recap, variables are storage areas. You use these storage areas to manipulate things like text and numbers. You'll be using variables a lot, and on the next few pages you'll see how they work in practice.
P H P BASICS (VARIABLES) TUTORIAL 1
A variable is just a storage area. You put things into your storage areas (variables) so that you can use and manipulate them in your programmes. Things you'll want to store are numbers and text.
If you're ok with the idea of variables, then you can move on. If not, think of them like this. Suppose you want to catalogue your clothing collection. You enlist two people to help you, a man and a woman. These two people are going to be your storage areas. They are going to hold things for you, while you tally up what you own. The man and the woman, then, are variables.
You count how many coats you have, and then give these to the man. You count how many shoes you have, and give these to the woman. Unfortunately, you have a bad memory. The question is, which one of your people (variables) holds the coats and which one holds the shoes? To help you remember, you can give your people names! You could call them something like this:
mr_coats
mrs_shoes
mrs_shoes
But it's entirely up to you what names you give your people (variables). If you like, they could be called this:
man_coats
woman_shoes
woman_shoes
Or
HimCoats
HerShoes
HerShoes
But because your memory is bad, it's best to give them names that help you remember what it is they are holding for you. (There are some things your people balk at being called. You can't begin their names with an underscore (_), or a number. But most other characters are fine.)
OK, so your people (variables) now have name. But it's no good just giving them a name. They are going to be doing some work for you, so you need to tell them what they will be doing. The man is going to be holding the coats. But we can specify how many coats he will be holding. If you have ten coats to give him, then you do the "telling" like this:
mr_coats = 10
So, the variable name comes first, then an equals sign. After the equals sign, you tell your variable what it will be doing. Holding the number 10, in our case. (The equals sign, by the way, is not really an equals sign. It's called an assignment operator. But don't worry about it, at this stage. Just remember that you need the equals sign to store things in your variables.)
However, you're learning PHP, so there's something missing. Two things, actually. First, your people (variables) need a dollar sign at the beginning (people are like that). So it would be this:
$mr_coats = 10
If you miss the dollar sign out, then your people will refuse to work! But the other thing missing is something really picky and fussy - a semi-colon. Lines of code in PHP need a semi-colon at the end:
$mr_coats = 10;
If you get any parse errors when you try to run your code, the first thing to check is if you've missed the semi-colon off the end. It's very easy to do, and can be frustrating. The next thing to check is if you've missed out a dollar sign. But back to our people (variables).
So the man is holding ten coats. We can do the same thing with the other person (variable):
$mrs_shoes = 25;
So, $mrs_shoes is holding a value of 25. If we then wanted to add up how many items of clothes we have so far, we could set up a new variable (Note the dollar sign at the begining of the new variable):
$total_clothes
We can then add up the coats and the shoes. You add up in PHP like this:
$total_clothes = $mr_coats + $mrs_shoes;
Remember, $mr_coats is holding a value of 10, and $mrs_shoes is holding a value of 25. If you use a plus sign, PHP thinks you want to add up. So it will work out the total for you. The answer will then get stored in our new variable, the one we've called $total_clothes. You can also add up like this:
$total_clothes = 10 + 35;
Again, PHP will see the plus sign and add the two together for you. Of course, you can add up more than two items:
$total_clothes = 10 + 35 + 7 + 38 + 1250;
But the idea is the same - PHP will see plus signs and then add things up. The answer is then stored in your variable name, the one to the left of the equals sign.
In the next part, we'll take a look at how to put text into variables.
Subscribe to:
Comments (Atom)