EvilZone

Programming and Scripting => Web Oriented Coding => Topic started by: Nerotic7 on May 27, 2013, 06:53:04 PM

Title: PHP Question
Post by: Nerotic7 on 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
Title: Re: PHP Question
Post by: vezzy on May 27, 2013, 07:01:20 PM
I don't get why you're so stumped?

Code: [Select]
  <?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";       
        }
?>

Title: Re: PHP Question
Post by: Snayler on May 27, 2013, 07:05:48 PM
<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:
Code: (php) [Select]
      <?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:
Code: (php) [Select]
<?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.
Title: Re: PHP Question
Post by: Nerotic7 on May 27, 2013, 07:06:55 PM
Thank you for your answers.
Title: Re: PHP Question
Post by: Stackprotector on May 28, 2013, 09:38:55 AM
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.
Title: Re: PHP Question
Post by: Nerotic7 on May 28, 2013, 07:51:31 PM
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