PHP Numbers | 7+ Expert Logical MCQ Questions on PHP Numbers

PHP Numbers

This PHP numbers MCQ quiz has a pool of 25 questions on integers and floats, numeric strings, rounding functions like ceil(), floor() and round(), the % operator, intdiv() and random numbers. It is made for diploma, engineering, BCA and BCS students and freshers preparing for a PHP interview or exam. Every attempt gives you 10 random questions from the pool. Answer them, press Submit, and you will see your score, which answers were right or wrong, and a short explanation for each one.

Numbers are part of PHP’s data types, so if you are new to them, first try our PHP data types MCQ.

Take the PHP Numbers MCQ Quiz

PHP Numbers MCQ Quiz

1 / 25

Which of these is an integer value in PHP?

2 / 25

What is the output of this code?

<?php
echo (int)((0.1 + 0.7) * 10);

3 / 25

Which function should you use for a secure random number between 1 and 100, for example for an OTP?

4 / 25

What is the output of this code?

<?php
echo number_format(1234567.891, 2);

5 / 25

What is the output of this code?

<?php
echo round(2.5) . " " . round(3.5);

6 / 25

What happens when you run this code in PHP 8?

<?php
echo "abc" * 2;

7 / 25

What happens when you run this code in PHP 8?

<?php
$a = 10;
$b = "10 apples";
echo $a + $b;

8 / 25

What is the output of this code?

<?php
var_dump("5" + "5");

9 / 25

What is the output of this code?

<?php
var_dump(is_numeric("12.5"), is_numeric("12abc"));

10 / 25

What happens when you run this code on a 64-bit system?

<?php
var_dump(PHP_INT_MAX + 1);

11 / 25

What is the output of this code?

<?php
var_dump(0.1 + 0.2 == 0.3);

12 / 25

What is the output of this code?

<?php
echo 2 ** 3 ** 2;

13 / 25

What is the output of this code?

<?php
$a = 2;
$b = 3;
$c = 4;
echo $a * $b ** $c;

14 / 25

What is the output of this code?

<?php
$x = 7;
$y = $x++ + ++$x;
echo $y;

15 / 25

What is the output of this code?

<?php
var_dump(7 / 2);

16 / 25

What is the output of this code?

<?php
var_dump(10 / 2);

17 / 25

What is the output of this code?

<?php
echo intdiv(7, 2);

The % operator converts both numbers to integers first. fmod() works with floats, so fmod(7.5, 2) gives 1.5. mod() and remainder() do not exist in PHP.

18 / 25

Which function gives the remainder of a division with decimal numbers, for example 7.5 divided by 2?

 

In PHP the result of % takes the sign of the left number. 7 % 3 is 1, so -7 % 3 is -1.

19 / 25

What is the output of this code?

<?php
echo -7 % 3;

% gives the remainder of a division. 10 divided by 3 is 3 with 1 left over, so 10 % 3 is 1.

20 / 25

What is the output of this code?

<?php
$a = 10;
$b = 3;
echo $a % $b;

intval() reads the number at the start of the string and stops at the first character that is not a digit. "12abc" gives 12.

21 / 25

What is the output of this code?

<?php
echo intval("12abc");

22 / 25

What is the output of this code?

<?php
echo (int)3.9;

23 / 25

What is the output of this code?

<?php
echo round(7.25, 1);

24 / 25

What is the output of this code?

<?php
echo floor(-3.7);

25 / 25

What is the output of this code?

<?php
echo ceil(4.3);

Your score is

The average score is 0%

0%

What This Quiz Covers

Most questions ask “What is the output of this code?”, because that is how interviewers test numbers in PHP. The questions come from these topics:

  • Integers and floats: which values are int and which are float, var_dump() output and what happens after PHP_INT_MAX.
  • Numeric strings: is_numeric(), intval(), and what PHP 8 does with strings like "10 apples" or "abc".
  • Arithmetic: /, %, intdiv(), fmod(), **, ++ and operator order.
  • Rounding and formatting: ceil(), floor(), round(), (int) casting and number_format().
  • Floats and random numbers: float precision, and rand() vs random_int().

PHP Numbers: Key Points to Revise

These short notes cover everything the quiz tests. They do not give away the quiz answers, but if you understand them, you can answer every question. The correct answers and explanations appear after you submit the quiz.

int and float: the two number types

PHP has two number types. An int is a whole number such as 42, -7 or 0. A float has a decimal point or an exponent, such as 3.14 or 1.5e3. A value in quotes, like "42", is a string, even if it looks like a number. Use var_dump() to see both the type and the value.

<?php
var_dump(42);      // int(42)
var_dump(3.14);    // float(3.14)
var_dump("42");    // string(2) "42"
var_dump(PHP_INT_MAX);   // int(9223372036854775807) on 64-bit

PHP_INT_MAX is the biggest int PHP can store. If a calculation goes beyond it, PHP does not give an error. It changes the result into a float. You can read the full rules in the PHP manual pages on integers and floats.

Numeric strings in PHP 8

When you use a string in maths, PHP tries to turn it into a number. A fully numeric string like "12.5" works without any message. A string that only starts with a number, like "10 apples", uses the number but shows a Warning. A string with no number at the start, like "abc", throws a TypeError in PHP 8. Use is_numeric() to check a value first, for example before using form input in a calculation.

Division, remainder and intdiv()

PHP has four ways to divide, and each one gives a different kind of answer. / returns an int only when the division is exact; otherwise it returns a float. % works on integers and returns the remainder. intdiv() returns only the whole part. fmod() returns the remainder for decimal numbers.

Operator order and the ** operator

** (power) runs before *, / and %, which run before + and -. Most operators of the same level run from left to right, but ** runs from right to left. Use brackets when you are not sure. For ++, remember that $x++ gives the old value and then adds 1, while ++$x adds 1 first and gives the new value.

ceil(), floor(), round() and (int)

These four look similar but behave differently, especially with negative numbers. ceil() always goes up, floor() always goes down, round() goes to the nearest whole number, and (int) or intval() just cuts off the decimal part.

round() also takes a second argument for decimal places, for example round(3.14159, 2) gives 3.14. By default it rounds an exact .5 away from zero. To show a number with commas and fixed decimals, use number_format(), which returns a string.

Why floats are not exact

Floats are stored in binary, and many decimal numbers, like 0.1, cannot be stored exactly. Small errors appear in calculations, so never compare two floats with ==. Round them first, or check that the difference is very small. For money, store amounts in paise (as integers) instead of rupees with decimals.

<?php
$a = 0.1 + 0.2;
echo $a;                            // 0.3 (looks fine)
var_dump(abs($a - 0.3) < 0.00001);  // bool(true), the safe way to compare

Random numbers

rand(min, max) and mt_rand(min, max) give a random int between two numbers and are fine for games or shuffling. For anything related to security, such as OTPs, tokens or passwords, use random_int(min, max), because its numbers cannot be predicted.

How to Score Better Next Time

  • Run every code question yourself. Save it as a .php file in XAMPP’s htdocs folder, or use an online PHP sandbox.
  • For every rounding question, check whether the number is negative before you answer.
  • When you see var_dump(), think about the type (int, float or string) as well as the value.
  • Read the explanation of every question you got wrong on the result page, then retake the quiz for a new set of 10.

For a mixed PHP test with basics, arrays and string functions, try our PHP MCQ questions with answers.

Frequently Asked Questions

How many number types are there in PHP?

Two: int (whole numbers) and float (numbers with a decimal point or exponent). A numeric string like "25" is a string, but PHP converts it to a number when you use it in maths.

What is the difference between ceil() and floor() in PHP?

ceil() rounds a number up to the next whole number, and floor() rounds it down. For negative numbers, up means toward zero and down means away from zero. Both return a float.

What is the difference between % and intdiv() in PHP?

% gives the remainder of a division, and intdiv() gives the whole-number result. For 17 and 5, 17 % 5 is 2 and intdiv(17, 5) is 3, because 17 = 5 × 3 + 2.

Where can I see the correct answers?

Press Submit at the end of the quiz. The result page shows your score, the correct answer for every question and a short explanation. The answers are not listed on this page so that you test yourself first.

Conclusion

This PHP numbers MCQ quiz tested the number rules that interviewers use in short output questions: int vs float, numeric strings in PHP 8, % and intdiv(), rounding negative numbers and float precision. If you scored 8 or more out of 10, retake the quiz once or twice to see new questions from the pool of 25. If not, read the key points again, run the code yourself, and try again.

Get one new quiz every week. Join Quiz Lancer on Telegram →

Scroll to Top