2/4 conditionals and control flow


#1






<?php
$items = 6 > 5; // Set this to a number greater than 5!

    if($items > 5) {
      echo "You get a 10% discount!";
    }
  ?>
</p>


whats wrong with this?


#2

Please edit this post.

Select your code (ctrl + a) and click on <\> icon in the toolbar to format content of your post as a code.


#3

$items = 6 > 5; // Set this to a number greater than 5!

That means not $items = 6 > 5;
The variable $items should have a value greater than 5 maybe 6 like you suspected.

KR


#4

This is answer. Why?
I don't know, but I try, a lot of times in another way, and doesn't working!
<?php
$items = 6; // Set this to a number greater than 5!

    if( $items=5 ) {
      echo "You get a 10% discount!";
    }
  ?>

#5
<?php
$items = 6;   // Set this to a number greater than 5!
  if( $items=5 ) {
     echo "You get a 10% discount!";
   }
?>

Hi with = you give a variable a value.

$items = 6; // if you now echo $items it will show you six
$items = 5; // if you now echo $items it will show you five

In your if statement you didnt compare.

if($items=5)

For all Comparison Operators look here into the Manaual
PHP Manual Comparsion Operator


#6

<?php
$items = 15;

    if($items > 5) {
      echo "You get a 10% discount!";
    }
  ?>

#7

I did this but it didn't work


#8

You have to include the == operator:
if($items > 5 == true)


#9

Not necessarily, "$items>5" Is a boolean, It is either Labelled true, or false. And the if statments bassicaly works like this {
if (true) {
// do something
}
else { //meaning if the boolean is false
// do something else
}
so only say this "if ($item>5) {" would do the job
The problem with the intial code was with the $item variable, It was set to a boolean, not a number,

A boolean Cannot be compared to a number, which is why they are getting an error. To avoid this, simple change item to : "$item = 6" instead of "$item = 6 >5"


#10






<?php
$items = $items = 6 > 5; // Set this to a number greater than 5!

    if($items) {  
      echo "You get a 10% discount!";
    }
  ?>
</p>