Day 2: Learning the basics of PHP (part 2) and coding a rock-papers-scissor script
Hey!
In the previous lesson you have learnt about
Day 1: Learning the basics of PHP Wrote:Variables and execution
Types of variables
Assigning a variable and the function of ".="
PHP opening tags and basic commands
Echo
When to use " "
Die or exit
Include
PHP Comment tags
The difference between ' ' and " " and when it's best to use which
And in this lesson we will continue talking about the basics of PHP and we will even code a simple rock-papers-scissors script (which will be continued in the next lesson too)
Table of contents
1. Using post and get variables (retrieving information from forms)
2. Using an if()
2.1 The use of && and || in if()
3. Coding the simple rock-papers-scissors script
1. Using post and get variables (retrieving information from forms)
First of all, what are post and get variables? To make it easy, we will explain it using an example.
A post variable (or in PHP: $_POST[id]) is used for forms. For example if you try to login on a website, there will be 2 input boxes (username & password) usually and sometimes a checkbox to remember the login. Now we want to know what's inside the input box and if the checkbox is checked or not. To see this we will just use $_POST['~inputbox-id-here~']. If you echo $_POST[id] you will get what's inside the inputbox, after submitting the page with a submit button.
A get variable (or in PHP: $_GET[id]) is used for transmitting information from pages to other pages -- where the information is not so sensible, or the information is encrypted. Why? Because this is more dangerous since every $_GET variable is visible in the URL. For example:
A page asking which language you want to see the site in: you submit the _get method form and it goes to a webpage like: http://site.com/index.php?language=english
on that page, the value of $_GET['language'] is "english"
2. Using an if()
Ifs are really useful and used a lot.
If(condition) {
//do this
}
else { (or elseif()
do this
}
For example:
PHP Code:
$a = 1; $b = 100; $c = 21 if($a > $b) { echo "$a > $b"; } elseif($a == $b) { //notice the double = ? That's required for comparison // one = is used when you assign a value to a variable echo "$a = $b"; } else { //only one possibility left echo "$a < $b"; }
//or for example if($_GET['language'] == "en") { //display page in english }
//other example with $a & $b & $c from the first example //You can count in an if too, (notice the extra backets ( ) for readability and for PHP) if(($a + $b) > $c) { //then do this; }
2.1 The use of && and || in if()
So those were the basics of an if, here is the use of && and || Never code anything like this:
PHP Code:
//Never code anything like this!! if($z == $a) { if($a == $b) { //then do this } }
Instead you should code this which is more performant and uses less space in the php file and is easier to read:
PHP Code:
//so we need $z == $a and $a == $b then we can continue and do //the process -- the and symbol is: && -- the or symbol is: || if($z == $a && $a == $b) { //do this }
It's the exact same thing for or, but with || instead of &&
3. Coding the simple rock-papers-scissors script
Those basics are enough to start doing some practical php coding!
I will first explain in words what is needed -- and then use a button to show the code so you can try to figure it out yourself before seeing the code.
Step 1.
Open notepad or an editor like dreamweaver, but notepad should be enough. Save your file as index.php
First we'll want to add the html code for the form. If you don't know basic html, you should find some tutorials on the internet, it's really easy, if you do some practical tutorials you'll be able to do it in less than 4-5 days.
So what do we need in the form and html layout?
In the form:
- A selection box with 4 values: Paper, Rock, Scissors and "Please select an option" which is set as default shown and the id of it is "none" -- where the id of the value is the same as the name (except for "none")
Below this we need to display the score -- the score will be shown using a get variable though that is not safe but to make it safe we would need to use a hidden input & etc which is too complicated to use in just the second lesson, so we'll use this:
Although you also need to change this: if there is no score set, hide the scores! (Hint: use an if)
For a matter of security we use intval(); to be sure it's a number that is put in the url, otherwise the user could put code inside the url to hack the website.
hint: you can use !empty(var) to check if it's not empty or just check if($var == '')
Now we have to display who won the last match, we will do that using a get variable: $_GET['pc_won'] and $_GET['user_won']
if that variable is yes, then he won. It's upto you to code it! Oh and don't forget to show if both have won!
If the user has chosen an option from the select box, then we'll check what the pc has chosen. Using the function random -- rand(minimum value, max value) -- we'll define what the pc has chosen. Do a random from 1-3 where 1 is paper, 2 is scissors and 3 is rock Try it out! Hint in an if: is not, is: !=
RE: Day 2: Learning the basics of PHP (part 2) and coding a rock-papers-scissor script
The game used in this document, Paper, Scissors (RPS), which is sometimes also known as Roshambo. The game was chosen because it is a two player game with simple rules: if the game is personal, each player shows the hand they have chosen.