0
0
PHPprogramming~5 mins

Declare strict_types directive in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the declare(strict_types=1); directive do in PHP?
It enforces strict type checking for scalar type declarations in the file, meaning PHP will not perform type coercion and will throw a TypeError if types do not match exactly.
Click to reveal answer
beginner
Where must the declare(strict_types=1); directive be placed in a PHP file?
It must be the very first statement in the PHP file, immediately after the opening <?php tag, before any other code or whitespace.
Click to reveal answer
intermediate
True or False: The strict_types directive affects all files included or required by the current PHP file.
False. The strict_types directive applies only to the file where it is declared. Each file must declare it separately if strict typing is desired.
Click to reveal answer
beginner
What happens if you call a function with a type mismatch when strict_types=1 is declared?
PHP throws a TypeError exception immediately instead of trying to convert the value to the expected type.
Click to reveal answer
beginner
Example: How to declare strict types in a PHP file?
Place this at the top of your PHP file:<br>
<?php
declare(strict_types=1);
// Your code here
Click to reveal answer
What is the correct way to enable strict typing in a PHP file?
Adeclare(strict_types=1); at the top of the file
Bset_strict_types(true); anywhere in the file
Cuse strict_types(); inside functions
Denable_strict_types in php.ini only
If strict_types=1 is declared, what happens when you pass a float to a function expecting an int?
APHP converts the float to int automatically
BPHP rounds the float silently
CPHP ignores the type and runs the function
DPHP throws a TypeError
Does the strict_types directive affect included or required files automatically?
AYes, it applies to all included files
BNo, each file must declare it separately
COnly if included files are in the same directory
DOnly if included files have the same namespace
Where must the declare(strict_types=1); statement appear in a PHP file?
AImmediately after the opening <code>&lt;?php</code> tag
BAnywhere before the first function call
CAfter all function definitions
DAt the end of the file
What is the default behavior if strict_types is not declared?
APHP throws errors on type mismatch
BPHP enforces strict type checking
CPHP silently converts types when possible
DPHP disables all type checking
Explain what the declare(strict_types=1); directive does in PHP and why you might want to use it.
Think about how PHP normally handles types and what changes with strict types.
You got /4 concepts.
    Describe where and how to correctly place the declare(strict_types=1); directive in a PHP file.
    Consider the file structure and PHP parsing rules.
    You got /3 concepts.