PHP Tutorial: Learning PHP (Part 2)

Comments 2 Standard

In Part 1, I talked about requirements for php, creating a file, variables, and if-else statements. Now on to harder concepts.

Else-If Statements

Before we covered the if-else statement. In an if-else statement if something is true take some action, otherwise do something else. The else-if statement builds on the power of an if-else. Look at this example:

<?php

$money = 20;

$bread = 5;

$candy = 5;

$cake = 15;

if ($money – $bread – $candy – $cake >= 0)
{
echo “I can afford to buy bread, candy and cake<br>”;
}
else if ($money – $cake – $candy == 15)
{
echo “I can afford cake and candy with 15 dollars left over<br>”;
}
else if ($money -$cake – $candy == 10)
{
echo “I can afford cake and candy with 10 dollars left over<br>”;
}
else if ($money – $cake – $candy == 5)
{
echo “I can afford cake and candy with 5 dollars left over<br>”;
}
else
{
echo “I shouldn’t buy cake and candy, it’s not good for me<br>”;
}

?>

What do you think will show up on the screen when you run this script? If you guessed it will tell you that you have 10 dollars left over you are correct. An else-if statement lets you add conditions to the else part of the statement. Now what happens if you change price of candy to $8? Play around with this script. Add more if-else statements and see what it does.

Important! You can only have one else statement at the end of if and if-else statements. If you try to add multiple else statements php will give you an error. Why do you think that is?

Do you think it make sense to have something like this:

<?php

$money = 5;

$candy = 5;

$bread = 10;

if ($money – $bread >= 0)
{
echo “I can afford bread”;
}
else
{
echo “I can’t afford bread”;
}
else
{
echo “I really really can’t afford bread”;
}

?>

The answer is no. An else statement marks the end of all the things you’re testing conditionally. Therefore there’s no reason to have multiple else statements at the end of an if or if-else statements. Think of it as trying to put two periods at the end of the sentance. You only need one period so having another period is considered to be an error.

Complex If Statements

Let’s put your knowledge to the test! Create a php file called bookstore.php. Put this inside of it:

<?php

//**************
//Filename: bookstore.php
//From: blog.design1online.com
//Modified By: (your name)
//Modified On: (todays date)
//Info: learning php tutorial part 2
// – this is called a header. If you make a commented header at the top of every
// php file to help you remember what you did and why.
//About: You walk into a bookstore and find three books you want. Since you like all the
//booksequally well you want to buy as many books as you can afford.
//**************

$book1 = “blue”;
$cost1 = 10;

$book2 = “green”;
$cost2 = 5;

$book3 = “red”;
$cost3 = 15;

$book4 = “yellow”;
$cost4 = 5;

$money = 25;

?>

Try it! Can you write a bunch of if-else statements that show which books you would buy without going over how much money you have? Here’s a hint, you may have to make more variables to help you figure it out. Once you’ve come up with an answer view the php page to see answer you get. It should tell you to buy hree books, either green red and yellow or yellow green and blue.

Think you have the answer? Even if you don’t its okay. Here’s a solution I came up with.

<?php

//**************
//Filename: bookstore.php
//From: blog.design1online.com
//Modified By: (your name)
//Modified On: (todays date)
//Info: learning php tutorial part 2
//About: This is my solution. Your script probably doesn’t look the same but it should
//give you the same results. That’s okay! It’s possible to get the correct answer in
//different ways.
//**************

$book1 = “blue”;
$cost1 = 10;
$book2 = “green”;
$cost2 = 5;

$book3 = “red”;
$cost3 = 15;

$book4 = “yellow”;
$cost4 = 5;
$money = 25;

$currentmoney = $money;
$totalbooks = 0;

if ($money – $cost1 – $cost2 – $cost3 – $cost4 >= 0) //trying buying all four books
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2 – $cost3 >= 0) //buying three books at at ime
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost3 – $cost4 >= 0)
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost2 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost3 – $cost4 >= 0)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2 – $cost4 >= 0)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;

$amount = $money – $cost1 – $cost2 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost2) //trying buying two books at a time
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;

$amount = $money – $cost1 – $cost2;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost3)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost1 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1 – $cost4)
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost1 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost3) //trying buying two books at a time
{
echo “I can afford the ” . $book1 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book2 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost2 – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2 – $cost4) //trying buying two books at a time
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
echo “I can afford the ” . $book4. ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost2 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost3 – $cost4) //trying buying two books at a time
{
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost3 – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost1) //trying buying one book only
{
echo “I can afford the ” . $book1 . ” book for ” . $cost1 . “<br>”;
$amount = $money – $cost1;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost2)
{
echo “I can afford the ” . $book2 . ” book for ” . $cost2 . “<br>”;
$amount = $money – $cost2;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost3)
{
echo “I can afford the ” . $book3 . ” book for ” . $cost3 . “<br>”;
$amount = $money – $cost3;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

if ($money – $cost4)
{
echo “I can afford the ” . $book4 . ” book for ” . $cost4 . “<br>”;
$amount = $money – $cost4;

echo “Which leaves me with ” . $amount . ” dollars left over”;
}

?> Phew! That was a lot of code! Try it and see what it does if you couldn’t get your own version working. Change the prices of the books. Does that change your result?

Programming Challenge: modify bookstore.php so it always returns the most books you can buy and you still have some money left over. For instance, in the example above you would pick books yellow, green, and blue because you’d still have $5 left over.

Continue on to Part 3 of this tutorial.

Advertisements

2 thoughts on “PHP Tutorial: Learning PHP (Part 2)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s