Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Day 2: Learning the basics of PHP (part 2) and coding a rock-papers-scissor script
Author Message
Shadows Away
Administrator
*******

Posts: 213
Joined: Aug 2009
Reputation: 0
Post: #1
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:

PHP Code:
echo 'Your score: ' intval($_GET['user_score']) . ' -- Computer\'s score: ' intval($_GET['pc_score']); 
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.


PHP Code:
<form name="form1" method="post" action="">
  <
label>
    <
select name="select" id="select">
      <
option value="none" selected>Please choose a value</option>
      <
option value="scissors">Scissors</option>
      <
option value="rock">Rock</option>
      <
option value="paper">Paper</option>
    </
select>
  </
label>
  <
label>
    <
input type="submit" name="play" id="play" value="Play!">
  </
label>
</
form>

<?
php
if(!empty($_GET['user_score']) && !empty($_GET['pc_score'])) 
{
    echo 
'Your score: ' intval($_GET['user_score']) . ' -- Computer\'s score: ' intval($_GET['pc_score']);
}
else
{
    echo 
"Play and a score will appear here.";

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!


PHP Code:
if($_GET['pc_won'] == "yes" && $_GET['user_won'] == "yes")
{
    echo 
'You have both won';
}
elseif(
$_GET['pc_won'] == "yes") {
    echo 
"You have lost.";
}
elseif(
$_GET['user_won'] == "yes") {
    echo 
"You won!";


This is the complete code we have until now:

PHP Code:
<form name="form1" method="post" action="">
  <
label>
    <
select name="select" id="select">
      <
option value="none" selected>Please choose a value</option>
      <
option value="scissors">Scissors</option>
      <
option value="rock">Rock</option>
      <
option value="paper">Paper</option>
    </
select>
  </
label>
  <
label>
    <
input type="submit" name="play" id="play" value="Play!">
  </
label>
</
form>

<?
php
if(!empty($_GET['user_score']) && !empty($_GET['pc_score'])) 
{
    echo 
'Your score: ' intval($_GET['user_score']) . ' -- Computer\'s score: ' intval($_GET['pc_score']);
}
else
{
    echo 
"Play and a score will appear here.";
}

if(
$_GET['pc_won'] == "yes" && $_GET['user_won'] == "yes")
{
    echo 
'You have both won';
}
elseif(
$_GET['pc_won'] == "yes") {
    echo 
"You have lost.";
}
elseif(
$_GET['user_won'] == "yes") {
    echo 
"You 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: !=


PHP Code:
if($_POST['select'] != 'none')
//check pc's hand
    
$pc_choice rand(13);
    if(
$pc_choice == 1)
    {
        
$pc_choice 'paper';
     }
     elseif(
$pc_choice == 2)
     {
         
$pc_choice 'scissors';
     }
     elseif(
$pc_choice == 3)
     {
         
$pc_choice 'rock';
      }

So our script becomes:
PHP Code:
<form name="form1" method="post" action="">
  <
label>
    <
select name="select" id="select">
      <
option value="none" selected>Please choose a value</option>
      <
option value="scissors">Scissors</option>
      <
option value="rock">Rock</option>
      <
option value="paper">Paper</option>
    </
select>
  </
label>
  <
label>
    <
input type="submit" name="play" id="play" value="Play!">
  </
label>
</
form>

<?
php
if($_POST['select'] != 'none')
//check pc's hand
    
$pc_choice rand(13);
    if(
$pc_choice == 1)
    {
        
$pc_choice 'paper';
     }
     elseif(
$pc_choice == 2)
     {
         
$pc_choice 'scissors';
     }
     elseif(
$pc_choice == 3)
     {
         
$pc_choice 'rock';
      }
}

if(!empty(
$_GET['user_score']) && !empty($_GET['pc_score'])) 
{
    echo 
'Your score: ' intval($_GET['user_score']) . ' -- Computer\'s score: ' intval($_GET['pc_score']);
}
else
{
    echo 
"Play and a score will appear here.";
}

if(
$_GET['pc_won'] == "yes" && $_GET['user_won'] == "yes")
{
    echo 
'You have both won';
}
elseif(
$_GET['pc_won'] == "yes") {
    echo 
"You have lost.";
}
elseif(
$_GET['user_won'] == "yes") {
    echo 
"You won!";
}
?>

Now we need to check who won and set the score, but that will be for the next lesson.

See you at the next lesson!
(This post was last modified: 07-12-2010 11:10 PM by Shadows.)
07-09-2010 10:35 PM
Find all posts by this user Quote this message in a reply
nealrelics Offline
Junior Member
**

Posts: 5
Joined: Jul 2010
Reputation: 0
Post: #2
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.

acekard ds
07-14-2010 03:36 PM
Find all posts by this user Quote this message in a reply
princepearce Offline
Junior Member
**

Posts: 3
Joined: Aug 2010
Reputation: 0
Post: #3
RE: Day 2: Learning the basics of PHP (part 2) and coding a rock-papers-scissor script
I'd advise you not to use the EA logo directly and make some like a mock/parody version of it. Just to cover our asses.

iPhone Accessories
08-13-2010 04:04 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Day 1: Learning the basics of PHP Shadows 5 75 08-20-2010 02:44 AM
Last Post: micheljhonson87
  Day 3: Coding a rock paper scissors script (Part 2) Shadows 2 43 08-13-2010 04:06 AM
Last Post: princepearce

Forum Jump:


User(s) browsing this thread: 1 Guest(s)