FAQ: PHP Numbers - Mathematical Assignment Operators

This community-built FAQ covers the “Mathematical Assignment Operators” exercise from the lesson “PHP Numbers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Mathematical Assignment Operators

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello!

I have a doubt. Why does it output 1 whatever number the variable holds?

$answer=3; 
$answer-=$my_num;
echo $answer; #outputs 1

you mean with this code:

	$my_num = 12;

	$answer = $my_num;

	$answer += 2;

	$answer *= 2;

	$answer -= 2;

	$answer /= 2;

	$answer -= $my_num;

	echo $answer;

Because some clever math is done here.

we add two, then we multiple by two, so now in essence we have added 4 to the original number, now we subtract two, and divide by two, so the number is one higher then the number we started out with

sort of like doing plus two and then minus one, but then slightly different due to the multiplication.

or +4 and -2, which essentially happens here. The 2 we added gets multiplied to become 4. Its clever little trick, difficult to explain.

1 Like

Yes, I meant by this code! I got it by doing step-by-step execution.
Here’s what I did:

<?php
// Write your code below:
$answer=4;
$my_num=2;
$answer=$my_num;#2
$answer+=2;#4
$answer*=2;#4*2=8
$answer-=2;#8-2=6
$answer/=2;#6/2=3
#part 1: now the answer is 3
$answer-=$my_num; # part 2: original values won't change, so 4-2=2
echo $answer; #part 1- part 2

Hope that’s clear how the execution was done. If by chance I got it wrong, do correct me! : )

the comment here is wrong, as you say in the comment above:

$answer now holds/has a value of 3, so its 3 - 2 = 1

I am confused by the fact that you have a comment where you say: $answer = 3, and then on the next line your comment says: $answer = 4.

the value of this variable doesn’t magically change.

1 Like

I GOT IT NOW.
$my_num is 2! My bad! Thank you for checking it out and correcting it. : )

I got it by doing step-by-step execution.
Here’s what I did:

<?php
// Write your code below:
$answer=4;
$my_num=2;
$answer=$my_num;#2
$answer+=2;#4
$answer*=2;#4*2=8
$answer-=2;#8-2=6
$answer/=2;#6/2=3
#part 1: now the answer is 3
$answer-=$my_num; 
echo $answer; #3-2(my_num value)

EDITED AGAIN after @stetim94 corrected me. : )

For anyone not understanding how it will always end up as 1, consider your number as x, then follow along:

1) x        (chosen number)
2) x+2      (add 2)
3) (x+2)*2  (multiply by 2)
   = 2x+4
4) (2x+4)-2 (subtract 2)
   = 2x+2 
5) (2x+2)/2 (divide by 2)
   = x+1
6) (x+1)-x  (subtract original number)
   = 1
1 Like

When I entered this ridiculously long number into the program, it outputs 0! The number is:2367823678289863829823643781. Why does this work?

Since your number is larger than the maximum integer value allowed in php, so the number is converted to float. But, floats can only be precise to a specific number of digits. Your number as a float will not have the same number of digits as the original number. To confirm this, try the following

$my_num = 2367823678289863829823643781;
echo $my_num;

So, adding/subtracting small numbers such as 1 or 2 (see zunaid.hassan’s post above for a breakdown of the steps) from such numbers isn’t going to show up because of the precision.

Try running the following:

<?php
  $my_num1 = 2367823678289863829823643781;
  $my_num2 = $my_num1 + 100;
  echo $my_num1;
  echo "\n";
  echo $my_num2;
  echo "\n";
  echo $my_num1 - $my_num2;  // The answer will be 0