2

Im learning PHP now and was given a task which I thought I could follow through, but I get an

'int(459)'

printed out on the website.

Here is the task and my attempt to solve it: Multiply your age by the numbers of yours you went to school and put it isnide of variable named total. Then minus the total by 3. Then check, if total is greater or equal to 12 and put the result inside of another variable. Then use var_dump to see if its true, or false.


<?php


   $age = 33;
   $schoolyears = 14;
   $total = $age * $schoolyears;

$total -= 3;

$total >= 12;
$newVar = $total;


?>

<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>

  <?php
       var_dump($newVar);
   ?>
  </body>
</html>

Appreciate your answers! Rob

UPDATE!

After editing it lloks like this and it works.

<?php


   $age = 33;
   $schoolyears = 14;
   $total = $age * $schoolyears;

$total -= 3;

$total = $total >= 12;
$newVar = $total;


?>

<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>

  <?php
       var_dump($newVar);
   ?>
  </body>
</html>

It puts out :

bool(true)
0

4 Answers 4

1

This line doesn't do anything:

$total >= 12;

It produces a value, but you don't store that value anywhere. On the next line you just copy the value from $total (which is 459) to a new variable:

$newVar = $total;

It looks like you meant to combine these into this instead:

$newVar = $total >= 12;

In general, it looks like you're confusing these operators:

  • -=
  • >=

While they share the same second character, they are not related in any way. The first one subtracts the second value from the first and assigns it back to the first variable, kind of a double operation and a shorthand for:

$var1 = $var1 - $var2;

But the second one does no assigning. It literally semantically means "greater than or equal to". It performs a comparison between two values, but doesn't modify anything.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! Changing --- $total >= 12; --- to: $total = $total >= 12; also did the job.
0

When you state $total >= 12; it makes the check but does nothing with the information.

You need to set $newVar = $total >= 12.

Comments

0

From your question, step by step;

 $age = 33;
 $schoolyears = 14;
 $negative = 3;
 $total = $age * $schoolyears - $negative;
 if ($total >= 12) {$result = $total; $status = true;} else {$status = false;}
 var_dump($status);

Comments

0

I got it. This has to be....

$total >= 12;

like this:

$total = $total >= 12;

Thanks, I leave this up for other people looking foir similar questions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.