EvilZone
Programming and Scripting => Web Oriented Coding => : Nerotic7 May 27, 2013, 06:53:04 PM
-
I am trying to learn PHP, and I just ran into a problem that I don't understand.
The problem:
<html>
<head>
<title>Our Shop</title>
</head>
<body>
<p>
<?php
$items = 6; // Set this to a number greater than 5!
if ($items > 5) {
echo "You get a 10% discount!";
} else {
print "You get a 5% Discount";
}
?>
</p>
</body>
My Instructions:
Go ahead and add an elseif condition to your if/else. It should check whether $items == 1, and it should echo or print "Sorry, no discount!"
I just really don't understand this.
Can someone give me some help with this?
Thanks
-
I don't get why you're so stumped?
<?php
$items = 6; // Set this to a number greater than 5!
if ($items > 5) {
echo "You get a 10% discount!";
}
elseif ($items == 1) {
echo "Sorry, no discount!";
}
else {
print "You get a 5% Discount";
}
?>
-
<html>
<head>
<title>Our Shop</title>
</head>
<body>
<p>
<?php
$items = 6; // Set this to a number greater than 5!
if ($items > 5) {
echo "You get a 10% discount!";
} else {
print "You get a 5% Discount";
}
?>
</p>
</body>
My Instructions:
Go ahead and add an elseif condition to your if/else. It should check whether $items == 1, and it should echo or print "Sorry, no discount!"
First off, please try to avoid using different fonts and sizes, it's just annoying to the eyes.
Also, when posting code, use the [ code ] tags. On this case, you can use [ code=php ] (without the spaces), like this:
<?php
$items = 6; // Set this to a number greater than 5!
if ($items > 5) {
echo "You get a 10% discount!";
} else {
print "You get a 5% Discount";
}
?>
Now, as for your question: You have an if/else statement and the instructions ask you to add an elseif statement. This is accomplished like this:
<?php
if (condition1)
{
command_to_run;
}
elseif(condition2)
{
command_to_run;
}
else
{
command_to_run;
}
?>
Does that answer your doubts?
EDIT: Damn, vezzy beat me to it.
@vezzy: Giving the answer right away won't help him fully understand, he may just copy-paste. Just my $0.02.
-
Thank you for your answers.
-
There is no reason (for you) to use print and echo at the same time, use one (echo).
Also please learn about programming, take up some very very very basic tutorials.
-
There is no reason (for you) to use print and echo at the same time, use one (echo).
Also please learn about programming, take up some very very very basic tutorials.
I am learning PHP. That question was from my book