Bird
Raised Fist0
PHPprogramming~10 mins

Integer type and behavior in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an integer variable with value 10.

PHP
<?php
$number = [1];
echo $number;
?>
Drag options to blanks, or click blank then click option'
A10
B"10"
C10.0
D'10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number makes it a string, not an integer.
Using a decimal point makes it a float, not an integer.
2fill in blank
medium

Complete the code to check if a variable is an integer using a built-in function.

PHP
<?php
$var = 5;
if ([1]($var)) {
    echo "Integer";
} else {
    echo "Not integer";
}
?>
Drag options to blanks, or click blank then click option'
Ais_int
Bis_string
Cis_float
Dis_numeric
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_string or is_float instead of is_int.
Using is_numeric which returns true for floats and numeric strings too.
3fill in blank
hard

Fix the error in the code to correctly add two integers and print the result.

PHP
<?php
$a = 7;
$b = 3;
$sum = $a [1] $b;
echo $sum;
?>
Drag options to blanks, or click blank then click option'
A-
B+
C.
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator . which concatenates strings instead of adding numbers.
Using subtraction or multiplication operators instead of addition.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers 1 to 5 using a loop.

PHP
<?php
$squares = [];
for ($i = 1; $i [1] 5; $i++) {
    $squares[] = $i [2] $i;
}
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A<=
B*
C+
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causes the loop to stop before 5.
Using addition instead of multiplication for squares.
5fill in blank
hard

Fill all three blanks to create an associative array with keys as numbers and values as their cubes, only for numbers greater than 2.

PHP
<?php
$cubes = [];
foreach (range(1, 5) as [1]) {
    if ([2] > 2) {
        $cubes[[3]] = [2] * [2] * [2];
    }
}
print_r($cubes);
?>
Drag options to blanks, or click blank then click option'
A$num
D$val
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and condition causes errors.
Using undefined variables like $val.

Practice

(1/5)
1. Which of the following is a valid integer value in PHP?
easy
A. 42
B. 3.14
C. "100"
D. true

Solution

  1. Step 1: Understand integer definition in PHP

    Integers are whole numbers without decimals, like 42 or -7.
  2. Step 2: Check each option

    42 is 42, a whole number. 3.14 is 3.14, a float. "100" is a string. true is a boolean.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    Integer = 42 [OK]
Hint: Integers have no decimal point or quotes [OK]
Common Mistakes:
  • Confusing strings with integers
  • Thinking floats are integers
  • Using booleans as integers
2. Which of the following is the correct way to declare an integer variable in PHP?
easy
A. $num = 10;
B. $num = '10';
C. $num = 10.0;
D. $num = true;

Solution

  1. Step 1: Identify integer assignment syntax

    In PHP, integers are assigned without quotes and without decimal points.
  2. Step 2: Evaluate each option

    $num = '10'; assigns a string '10'. $num = 10; assigns integer 10 correctly. $num = 10.0; assigns a float 10.0. $num = true; assigns a boolean true.
  3. Final Answer:

    $num = 10; -> Option A
  4. Quick Check:

    Integer assignment = $num = 10; [OK]
Hint: No quotes or decimals for integers [OK]
Common Mistakes:
  • Using quotes around numbers
  • Assigning floats instead of integers
  • Confusing booleans with integers
3. What will be the output of the following PHP code?
$a = 5;
$b = 2;
$c = $a / $b;
echo (int)$c;
medium
A. 2.5
B. 3
C. Error
D. 2

Solution

  1. Step 1: Calculate division result

    $a / $b = 5 / 2 = 2.5 (float)
  2. Step 2: Cast float to integer

    (int)2.5 converts to 2 by dropping decimal part.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Integer cast truncates decimals = 2 [OK]
Hint: Casting float to int drops decimals [OK]
Common Mistakes:
  • Expecting rounding instead of truncation
  • Confusing output with float value
  • Thinking casting causes error
4. Identify the error in this PHP code snippet:
$num = 0123;
echo $num;
medium
A. Outputs 123 as decimal
B. Outputs 83 because of octal interpretation
C. Syntax error due to leading zero
D. Runtime error

Solution

  1. Step 1: Understand leading zero in integer literals

    In PHP, numbers starting with 0 are octal (base 8) numbers.
  2. Step 2: Convert octal 0123 to decimal

    Octal 123 = 1*64 + 2*8 + 3 = 64 + 16 + 3 = 83 decimal.
  3. Final Answer:

    Outputs 83 because of octal interpretation -> Option B
  4. Quick Check:

    Leading zero means octal = 83 [OK]
Hint: Leading zero means octal number in PHP [OK]
Common Mistakes:
  • Assuming leading zero is ignored
  • Expecting decimal output 123
  • Thinking it causes syntax error
5. You want to store a large integer value beyond PHP's 32-bit limit. Which approach correctly handles this in PHP?
hard
A. Use a float type to store the large number
B. Use an array to split the number into parts
C. Use a string to store the number and perform math with BCMath functions
D. Use an integer variable; PHP automatically supports large integers

Solution

  1. Step 1: Recognize PHP integer size limits

    PHP integers have size limits (32-bit or 64-bit depending on system). Large numbers can overflow.
  2. Step 2: Choose correct method for large integers

    Using strings with BCMath functions allows precise math on large numbers beyond integer limits.
  3. Final Answer:

    Use a string to store the number and perform math with BCMath functions -> Option C
  4. Quick Check:

    Large integer math needs strings + BCMath [OK]
Hint: Use strings + BCMath for big integers [OK]
Common Mistakes:
  • Using floats causes precision loss
  • Assuming PHP integers auto-expand
  • Trying to split number manually in arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard