[{"id":"phpadvanced-001","courseId":"php-advanced","domain":"types-and-operators","topic":"Strict types declaration","prompt":"lib.php declares strict_types=1 and defines double(int $n). main.php has no declare and calls double('21'). What does main.php print?","hint":"Ask which file contains the call, not which file contains the function.","explain":"The strict_types switch is set file by file. Consider which file the call is written in, and what PHP does with a wrong scalar type when the switch is off.","source":"https://www.php.net/manual/en/language.types.declarations.php","interaction":{"type":"single"},"options":[{"id":"a","text":"int(42)","why":"The call sits in a file without strict mode, so coercion applies: '21' becomes 21 and the function returns 42."},{"id":"b","text":"TypeError, because lib.php is in strict mode","whyNot":"Strict mode is decided by the file making the call; the declare in lib.php does not reach into main.php."},{"id":"c","text":"TypeError, because '21' is a string, not an int","whyNot":"That would be a TypeError only in a strict calling file; this file lets PHP coerce the value."},{"id":"d","text":"A warning, and the call returns null","whyNot":"PHP does not return null for a wrong argument type; it coerces the value when it can, or throws a TypeError."}],"answer":"a","explanation":"Strict typing applies to function calls made from within a file with strict typing enabled, not to the functions declared in that file. A non-strict caller still gets coercion.","concept":"Strict typing applies to function calls made from within a file with strict typing enabled, not to the functions declared in that file. A non-strict caller still gets coercion.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"// lib.php\n<?php\ndeclare(strict_types=1);\nfunction double(int $n): int {\n    return $n * 2;\n}\n\n// main.php (no declare)\n<?php\nrequire 'lib.php';\nvar_dump(double('21'));","takeaways":["strict_types is checked in the file making the call, not in the file that defines the function."]},{"id":"phpadvanced-002","courseId":"php-advanced","domain":"types-and-operators","topic":"Union types","prompt":"On PHP 8.2 or later, which TWO of these union return types are rejected at compile time?","hint":"Look for a type that appears twice, or one that already covers another type.","explain":"A union lists several types separated by |. Each type may appear only once, and some types already include others, so listing them again adds nothing. Some singleton types have special rules.","source":"https://www.php.net/manual/en/language.types.declarations.php","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"function f(): bool|false {}","why":"If bool is used, false cannot be added as well, so this redundancy is a compile-time error."},{"id":"b","text":"function f(): int|INT {}","why":"Each name-resolved type may occur only once, and INT resolves to the same type as int."},{"id":"c","text":"function f(): string|null {}","whyNot":"Allowed: string|null is the same type as ?string."},{"id":"d","text":"function f(): Foo|Bar {}","whyNot":"Allowed: two class names can form a union; the compiler only rejects redundancy it can detect without loading classes."},{"id":"e","text":"function f(): int|float {}","whyNot":"Allowed: int and float are different types, so neither makes the other redundant."},{"id":"f","text":"function f(): false|null {}","whyNot":"Allowed since PHP 8.2, which made false and null usable as standalone types."}],"answer":"ab","explanation":"Union members must be distinct after name resolution, and bool already covers false and true, so bool|false is a compile-time error.","concept":"Union members must be distinct after name resolution, and bool already covers false and true, so bool|false is a compile-time error.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["In a union, bool already covers false and true, so listing them again is a compile-time error."]},{"id":"phpadvanced-003","courseId":"php-advanced","domain":"types-and-operators","topic":"Intersection types","prompt":"Put these PHP type-system changes in the order PHP introduced or changed them, oldest first.","hint":"Each change on the page is tagged with the PHP version where it happened.","explain":"Language changes are dated on the reference page by version number. Read the version attached to each feature before deciding where it belongs in the timeline.","source":"https://www.php.net/manual/en/language.types.declarations.php","interaction":{"type":"sort","items":[{"text":"Nullable types written as ?T","why":"Nullable types were added in PHP 7.1.0, the earliest change in this list."},{"text":"Returning by reference from a void function","why":"That became deprecated as of PHP 8.1.0."},{"text":"DNF types, which combine intersection and union types","why":"DNF types arrived in PHP 8.2.0; before that, intersection and union types could not be combined."},{"text":"Null default making a parameter nullable","why":"That old way has been deprecated since PHP 8.4, the latest version in this list."}]},"options":[],"answer":"o:abcd","explanation":"The types page dates its changes: nullable types from 7.1.0, the void by-reference deprecation from 8.1.0, DNF types from 8.2.0, and the null-default deprecation from 8.4.","concept":"The types page dates its changes: nullable types from 7.1.0, the void by-reference deprecation from 8.1.0, DNF types from 8.2.0, and the null-default deprecation from 8.4.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Type features carry version tags on the reference page; check them before relying on a feature in older code."]},{"id":"phpadvanced-004","courseId":"php-advanced","domain":"types-and-operators","topic":"never return type","prompt":"The function below is valid in PHP 8.1 or later, because its return line never runs.","hint":"Consider how the compiler treats a return the program never reaches.","explain":"A return type states how a function ends. A never function must not finish normally. Think about what its body may contain, and how strictly PHP checks statements it can never reach.","source":"https://www.php.net/manual/en/language.types.never.php","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Unreached returns are still checked: a return statement in a never function is a compile-time error even if it never runs."},{"id":"b","text":"False","why":"A return statement in a never function is a compile-time error, even if the return would not be reached during execution."}],"answer":"b","explanation":"A return statement in a never function is a compile-time error even if it would not be reached, and a never function that finishes normally throws a TypeError.","concept":"A return statement in a never function is a compile-time error even if it would not be reached, and a never function that finishes normally throws a TypeError.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"function stop(): never {\n    exit(1);\n    return 0;\n}","takeaways":["A never function cannot contain any return statement, even one that is unreachable."]},{"id":"phpadvanced-005","courseId":"php-advanced","domain":"types-and-operators","topic":"void return type","prompt":"On PHP 8.1 or later, this function fails to compile. Why?","hint":"Look closely at the return statement and what a void function may return.","explain":"A return type says what a function gives back to its caller. Some types promise a value, some promise a kind of value, and one promises no value. Check what each return statement is allowed to contain.","source":"https://www.php.net/manual/en/language.types.void.php","interaction":{"type":"single"},"options":[{"id":"a","text":"return null; gives an expression, and void forbids returning one","why":"A return with an expression is a compile-time error in a void function, even when unreached; use a bare return; to exit early."},{"id":"b","text":"A void function cannot contain an if statement","whyNot":"Control flow is unrestricted in a void function; only returning a value is forbidden."},{"id":"c","text":"The parameter $rows cannot be typed as array","whyNot":"Parameter types are unaffected by void, so array is allowed."},{"id":"d","text":"A void function must end with an explicit return;","whyNot":"A void function may end without any return statement; reaching the end of the body is fine."}],"answer":"a","explanation":"A void function may not return any expression, including null; use a bare return; to exit the function early.","concept":"A void function may not return any expression, including null; use a bare return; to exit the function early.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"function save(array $rows): void {\n    if (!$rows) {\n        return null;\n    }\n    // write rows\n}","takeaways":["In a void function, use a bare return; to exit early, and never write return null; explicitly."]}]