0
0
PHPprogramming~10 mins

Constants in classes in PHP - Interactive Code Practice

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

Complete the code to define a constant named PI with value 3.14 inside the class.

PHP
<?php
class Circle {
    const [1] = 3.14;
}
?>
Drag options to blanks, or click blank then click option'
Api
BPI
CRadius
DVALUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase constant names like 'pi' instead of 'PI'.
Trying to use variables instead of constants.
2fill in blank
medium

Complete the code to access the constant PI from the Circle class.

PHP
<?php
class Circle {
    const PI = 3.14;
}
echo Circle::[1];
?>
Drag options to blanks, or click blank then click option'
API
Bpi
CRadius
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'pi' instead of 'PI'.
Trying to access constants like variables with $.
3fill in blank
hard

Fix the error in the code to correctly access the constant MAX_SPEED inside the Car class.

PHP
<?php
class Car {
    const MAX_SPEED = 200;
    public function getMaxSpeed() {
        return self::[1];
    }
}
?>
Drag options to blanks, or click blank then click option'
A$MAX_SPEED
Bmax_speed
CMax_Speed
DMAX_SPEED
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or camel case names instead of uppercase.
Using $ sign before constant name.
4fill in blank
hard

Fill both blanks to define a constant SPEED_LIMIT and access it inside the class method.

PHP
<?php
class Bike {
    const [1] = 100;
    public function getSpeedLimit() {
        return self::[2];
    }
}
?>
Drag options to blanks, or click blank then click option'
ASPEED_LIMIT
Bspeed_limit
CMAX_SPEED
DLIMIT
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for declaration and access.
Using lowercase names for constants.
5fill in blank
hard

Fill all three blanks to define a constant COLOR, access it inside the class, and print it outside the class.

PHP
<?php
class Car {
    const [1] = 'red';
    public function getColor() {
        return self::[2];
    }
}
echo Car::[3];
?>
Drag options to blanks, or click blank then click option'
Acolor
BCOLOR
CColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or different case names.
Trying to access constants like variables.