Course outline

PHP / Basic: values and request foundations / Lesson 7

Strict comparison and field presence

Distinguish PHP strict comparisons, null values, and missing keys.

About 22 min read20 worked examples20 practice exercisesContent reviewed
On this page

Lesson goals

  • Use strict comparison when types matter.
  • Distinguish a missing key from a present null.
  • Classify missing, null and false-like values under an explicit record contract.
  • Use strict search and separately validate normalized values.

Distinguish boundary states

External data can contain zero, false, null, an empty string, or a missing field, and these are not interchangeable domain states. PHP offers operations with different comparison and presence semantics. State which values are accepted before choosing an operator, and test each relevant case. A convenient truthiness check can otherwise turn a valid zero into apparent absence.

Compare type and value

Strict equality compares both type and value, while loose comparison follows conversion rules. The example contrasts an integer zero and a string containing zero. Choose strict comparison when the contract distinguishes them, but validate and convert external text deliberately before comparing it to a numeric domain value. Strictness alone does not establish that a value lies in an allowed range.

php A strict boundary check
<?php
declare(strict_types=1);

var_export(0 === "0");
echo "\n";
Expected output
false

Integer zero and string zero have different types.

Check key presence

array_key_exists reports whether a key is present even when its value is null. The second example contrasts a present null with a missing key. isset has different behavior for null, so choose the operation that expresses the contract. After determining presence, validate the value separately; a present field can still have an invalid type or domain value.

php A strict boundary check
<?php
declare(strict_types=1);

$data = ["count" => null];
var_export(array_key_exists("count", $data));
echo " ";
var_export(array_key_exists("missing", $data));
echo "\n";
Expected output
true false

The null-valued key exists; the other key does not.

Search operations can also use loose or strict comparison. When using in_array on values where type matters, pass its strict option and check the Boolean result. For an external identifier, define normalization and allowed types before searching; changing every comparison to strict without understanding existing input can break a documented contract. Keep validation close to the boundary.

Test false-like values

Test missing, null, false, zero, string zero, and an ordinary value wherever the contract distinguishes them. Assert both acceptance and the resulting value after conversion. Use synthetic input and repeat the original request after changing validation. A test containing only a nonempty string cannot detect confusion among the boundary states.

Name the states before choosing an operator

A field can be missing, present with null, false, integer zero, string "0", empty text, or an ordinary value. These states can have different meanings in a record even when a truthiness condition sends several of them down the same branch. Begin by writing the contract: for example, an absent optional note might mean “leave unchanged,” null might mean “clear,” and an empty string might mean an explicitly empty note. A required count could instead reject both absence and null while accepting integer zero. Choose the operation after naming those meanings, then test presence, type and domain range separately. Do not read a missing array key and suppress the warning to simulate a default; use a presence-aware operation. The examples use isolated synthetic values and PHP 8.3-compatible syntax, with runnable cases qualified only on PHP 8.5.12 CLI.

Compare type and value deliberately

The strict identity operator === checks type as well as value; 0 and "0" are not identical even if a loose comparison can treat some values as equal under PHP’s conversion rules. Strict comparison is useful only after the accepted input contract is clear. If external form data arrives as text and the domain expects an integer, validate its complete grammar and range before converting it; blindly adding === can reject a previously accepted representation, while blindly casting can disguise malformed input. Comparison does not establish that an otherwise correctly typed value is permitted. For example, integer 50 is strictly an integer but can still violate a range of 0 through 10. Avoid extrapolating one loose-comparison result to all PHP versions or operand types; consult the relevant manual and test the target baseline. Keep expected type, normalized value and final domain decision visible in the test.

Distinguish existence, non-nullness and fallback

array_key_exists asks whether a key exists in the specified array, including a key whose value is null. isset returns false for a missing key and for a key whose value is null, while it returns true for present zero, false and empty text. The null coalescing operator chooses its right-hand fallback when the left side is missing or null; it preserves those other false-like values. In contrast, empty applies a broader false-like test and does not answer whether a key is present with a particular value. When a record uses absence and null differently, first check existence, then inspect the actual value and type. If the contract deliberately treats both as “use a default,” coalescing can be appropriate. A fallback does not validate a supplied value, enforce a range or report an invalid array shape. Write focused tests for each state instead of relying on one convenient branch.

in_array accepts a strict flag as its third argument. With that flag, a string identifier "7" is not the integer 7 in a list; without it, the documented comparison rules can yield a different answer. Normalize identifiers only when the product contract defines equivalent forms and the boundary has validated them; leading zeroes can be meaningful identifiers rather than numeric decoration. Other APIs have different return shapes. array_search can return a key such as integer 0 or false, so compare its result with false strictly rather than treating zero as absence. A successful search establishes membership under that specific comparison and collection, not authorization for an unrelated operation. Test a match at key zero, a later key, an absent value, and a type mismatch. For nested records, identify the exact array being searched; checking a parent key does not establish that a child key exists.

Verify the decision and the value together

A useful boundary test records the synthetic input, its PHP type, the expected presence state, accepted or rejected decision and resulting normalized value. A test that checks only “accepted” can miss a value silently converted or replaced by a fallback. Include missing, null, false, integer zero, string zero, empty string, a valid ordinary value and an invalid range case where the contract distinguishes them. For a patch, assert the stored field after each operation so “leave unchanged,” “clear” and “replace” cannot collapse into one behavior. For an allowlist, test the strict membership result and the separate authorization decision. Local unit tests do not establish that an HTTP adapter parsed every content type correctly; verify that integration separately in an isolated environment. Avoid collecting real request values or credentials in fixtures. A structural check can confirm item counts, but semantic review must still examine the actual cases and assumptions.

20 worked examples

1. Integer identity

Problem: Interpret this synthetic comparison or presence result: Integer identity.

  1. Both operands are integers with value 7.
  2. Strict identity therefore returns true for this pair.
php Synthetic PHP CLI fixture
<?php
$a = 7; $b = 7; echo $a === $b ? "same\n" : "different\n";
Expected output
same

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: Both operands are integers with value 7. Strict identity therefore returns true for this pair.

2. Boolean and integer

Problem: Compare a boolean flag with an integer count using strict identity.

  1. The flag is boolean false while the count is integer zero.
  2. Strict identity is false because those values have different PHP types.
php Synthetic PHP CLI fixture
<?php
$enabled = false; $count = 0; echo $enabled === $count ? "same\n" : "different\n";
Expected output
different

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: Strict identity distinguishes the boolean flag from the integer count.

3. Loose and strict comparison

Problem: Interpret this synthetic comparison or presence result: Loose and strict comparison.

  1. For this numeric string and integer PHP’s loose comparison reports equality.
  2. Strict identity rejects the type difference; this result should not be generalized to every operand.
php Synthetic PHP CLI fixture
<?php
$a = '7'; $b = 7; echo ($a == $b ? 'loose-yes ' : 'loose-no '), ($a === $b ? "strict-yes\n" : "strict-no\n");
Expected output
loose-yes strict-no

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: For this numeric string and integer PHP’s loose comparison reports equality. Strict identity rejects the type difference; this result should not be generalized to every operand.

4. Presence of zero

Problem: Interpret this synthetic comparison or presence result: Presence of zero.

  1. The count key is present with integer zero.
  2. The presence operation reports the key rather than evaluating the value’s truthiness.
php Synthetic PHP CLI fixture
<?php
$r = ['count' => 0]; echo array_key_exists('count', $r) ? "present\n" : "missing\n";
Expected output
present

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The count key is present with integer zero. The presence operation reports the key rather than evaluating the value’s truthiness.

5. isset with false

Problem: Interpret this synthetic comparison or presence result: isset with false.

  1. The enabled key exists and its value is not null.
  2. isset returns true even though the boolean value itself is false.
php Synthetic PHP CLI fixture
<?php
$r = ['enabled' => false]; echo isset($r['enabled']) ? "set\n" : "not-set\n";
Expected output
set

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The enabled key exists and its value is not null. isset returns true even though the boolean value itself is false.

6. isset with null

Problem: Interpret this synthetic comparison or presence result: isset with null.

  1. The note key is present but its value is null.
  2. isset answers false for this null value; it is not a presence-only check.
php Synthetic PHP CLI fixture
<?php
$r = ['note' => null]; echo isset($r['note']) ? "set\n" : "not-set\n";
Expected output
not-set

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The note key is present but its value is null. isset answers false for this null value; it is not a presence-only check.

7. Absent key

Problem: Interpret this synthetic comparison or presence result: Absent key.

  1. The synthetic record contains only the name key.
  2. The presence check reports the requested note key as absent.
php Synthetic PHP CLI fixture
<?php
$r = ['name' => 'Ada']; echo array_key_exists('note', $r) ? "present\n" : "missing\n";
Expected output
missing

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The synthetic record contains only the name key. The presence check reports the requested note key as absent.

8. Null fallback

Problem: Interpret this synthetic comparison or presence result: Null fallback.

  1. The key is present with null.
  2. Null coalescing selects the specified fallback for this case.
php Synthetic PHP CLI fixture
<?php
$r = ['note' => null]; echo $r['note'] ?? 'none', "\n";
Expected output
none

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The key is present with null. Null coalescing selects the specified fallback for this case.

9. Empty string retained

Problem: Interpret this synthetic comparison or presence result: Empty string retained.

  1. The note is present and its value is an empty string, not null.
  2. The coalescing operator retains the empty string between the markers.
php Synthetic PHP CLI fixture
<?php
$r = ['note' => '']; echo '[', $r['note'] ?? 'none', "]\n";
Expected output
[]

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The note is present and its value is an empty string, not null. The coalescing operator retains the empty string between the markers.

10. String zero retained

Problem: Interpret this synthetic comparison or presence result: String zero retained.

  1. The value is the non-null string "0".
  2. Coalescing returns it unchanged instead of using the fallback.
php Synthetic PHP CLI fixture
<?php
$r = ['code' => '0']; echo '[', $r['code'] ?? 'none', "]\n";
Expected output
[0]

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The value is the non-null string "0". Coalescing returns it unchanged instead of using the fallback.

11. Truthiness of zero

Problem: Interpret this synthetic comparison or presence result: Truthiness of zero.

  1. The present count is integer zero.
  2. A boolean context treats it as false-like even though the key exists.
php Synthetic PHP CLI fixture
<?php
$r = ['count' => 0]; echo $r['count'] ? "truthy\n" : "false-like\n";
Expected output
false-like

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The present count is integer zero. A boolean context treats it as false-like even though the key exists.

12. Empty is broader

Problem: Interpret this synthetic comparison or presence result: Empty is broader.

  1. The present flag has boolean false as its value.
  2. empty reports true for this false-like value, not solely for absent keys.
php Synthetic PHP CLI fixture
<?php
$r = ['flag' => false]; echo empty($r['flag']) ? "empty-test-true\n" : "empty-test-false\n";
Expected output
empty-test-true

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The present flag has boolean false as its value. empty reports true for this false-like value, not solely for absent keys.

13. Strict membership

Problem: Interpret this synthetic comparison or presence result: Strict membership.

  1. The allowed list contains integer 7 but the needle is string "7".
  2. The strict flag requires matching types, so this membership check fails.
php Synthetic PHP CLI fixture
<?php
$allowed = [7, 9]; echo in_array('7', $allowed, true) ? "found\n" : "missing\n";
Expected output
missing

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The allowed list contains integer 7 but the needle is string "7". The strict flag requires matching types, so this membership check fails.

14. Normalized membership

Problem: Interpret this synthetic comparison or presence result: Normalized membership.

  1. The validator accepts the synthetic numeric text and returns integer 7.
  2. The strict search then matches that integer in the allowed list.
php Synthetic PHP CLI fixture
<?php
$raw = '7'; $n = filter_var($raw, FILTER_VALIDATE_INT);
echo $n !== false && in_array($n, [7, 9], true) ? "found\n" : "missing\n";
Expected output
found

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The validator accepts the synthetic numeric text and returns integer 7. The strict search then matches that integer in the allowed list.

15. Search at key zero

Problem: Interpret this synthetic comparison or presence result: Search at key zero.

  1. The strict search finds red at integer key 0.
  2. Comparing against false explicitly preserves that valid zero key.
php Synthetic PHP CLI fixture
<?php
$key = array_search('red', ['red', 'blue'], true);
echo $key === false ? "missing\n" : "key=$key\n";
Expected output
key=0

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The strict search finds red at integer key 0. Comparing against false explicitly preserves that valid zero key.

16. Missing search result

Problem: Interpret this synthetic comparison or presence result: Missing search result.

  1. The value green is not present in the two-item list.
  2. array_search returns false, which the strict check identifies as absence.
php Synthetic PHP CLI fixture
<?php
$key = array_search('green', ['red', 'blue'], true);
echo $key === false ? "missing\n" : "key=$key\n";
Expected output
missing

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The value green is not present in the two-item list. array_search returns false, which the strict check identifies as absence.

17. Nested presence

Problem: Interpret this synthetic comparison or presence result: Nested presence.

  1. The profile array contains name but not note.
  2. Checking the child array specifically reports the missing child key.
php Synthetic PHP CLI fixture
<?php
$r = ['profile' => ['name' => 'Ada']];
echo array_key_exists('note', $r['profile']) ? "present\n" : "missing\n";
Expected output
missing

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The profile array contains name but not note. Checking the child array specifically reports the missing child key.

18. Present wrong type

Problem: Interpret this synthetic comparison or presence result: Present wrong type.

  1. The count key exists, but its value is a string.
  2. The integer-only record rule rejects that value despite its presence.
php Synthetic PHP CLI fixture
<?php
$r = ['count' => '3'];
echo array_key_exists('count', $r) && is_int($r['count']) ? "valid\n" : "invalid\n";
Expected output
invalid

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The count key exists, but its value is a string. The integer-only record rule rejects that value despite its presence.

19. Correct type outside range

Problem: Interpret this synthetic comparison or presence result: Correct type outside range.

  1. The synthetic value is an integer but exceeds the maximum 10.
  2. The separate range condition rejects it even though its type is correct.
php Synthetic PHP CLI fixture
<?php
$n = 12; echo is_int($n) && $n >= 0 && $n <= 10 ? "valid\n" : "invalid\n";
Expected output
invalid

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The synthetic value is an integer but exceeds the maximum 10. The separate range condition rejects it even though its type is correct.

20. Explicit patch clear

Problem: Interpret this synthetic comparison or presence result: Explicit patch clear.

  1. The patch includes note with an explicit null value.
  2. The fixture’s contract uses presence to apply null as a clear operation.
php Synthetic PHP CLI fixture
<?php
$stored = 'old'; $patch = ['note' => null];
if (array_key_exists('note', $patch)) { $stored = $patch['note']; }
echo $stored === null ? "cleared\n" : "kept\n";
Expected output
cleared

Qualified on PHP 8.5.12 CLI with isolated synthetic data; PHP 8.3-compatible syntax, not a PHP 8.3 execution claim.

Why this result follows: The patch includes note with an explicit null value. The fixture’s contract uses presence to apply null as a clear operation.

Common errors and fixes

A valid zero is rejected

Why it happens
A truthiness check treated it as absent.
How to fix it
Check presence and validate the value separately.
Prevent it next time
Test zero explicitly.

String zero matches integer zero unexpectedly

Why it happens
The comparison used conversion rules.
How to fix it
Use the comparison required by the contract.
Prevent it next time
Test both types.

A null-valued key appears missing

Why it happens
The selected presence check treats null differently.
How to fix it
Use array_key_exists when key existence is the question.
Prevent it next time
Test missing and null separately.

20 practice exercises

1. Is 0 strictly equal to "0"?

Show hint

Compare both the type and value.

Show worked solution

No. The values have different types, so strict equality returns false.

Integer 0 and string "0" have different PHP types, so 0 === "0" evaluates to false. A domain that accepts textual zero needs an explicit validated normalization policy; strict comparison itself does not perform that conversion.

Check your solution

  • The strict comparison returns false.
  • The answer identifies integer versus string types.

2. Does array_key_exists find a key whose value is null?

Show hint

Ask whether the key exists, not whether its value is non-null.

Show worked solution

Yes. It checks key existence, not whether the value is non-null.

array_key_exists returns true for a present key whose value is null, and false for an absent key. Follow the presence check with value and type validation when the record contract requires them.

Check your solution

  • A present null-valued key is reported present.
  • An absent key is reported absent.

3. A patch treats absent note as unchanged, null as clear, and a string as replacement. Starting with "old", determine the result of three separate patches: {}, {note:null}, and {note:""}.

Show hint

Check existence before examining the value.

Show worked solution

The absent patch retains "old"; the present null clears the note to null; the present empty string replaces it with "". Use array_key_exists to distinguish the first two, then validate that any non-null replacement is an allowed string.

Check your solution

  • The three results are "old", null and "" respectively.
  • The empty string is not mistaken for absence or null.

4. A required count must be an integer from 0 to 5. Classify missing, null, false, string "0", integer 0 and integer 6 without coercion.

Show hint

Presence, type and range are separate checks.

Show worked solution

Reject missing and null, then reject false and string "0" for type. Accept integer 0 and reject integer 6 for range. Record the type and normalized value in each test; a truthiness check would incorrectly reject the valid zero.

Check your solution

  • Only integer 0 is accepted.
  • Type and range failures are distinguished.

5. An allowlist contains integer IDs [7,9], while a request sends text "7". Compare strict search before and after an explicitly validated integer conversion.

Show hint

The representation changes only after validation.

Show worked solution

The strict search of the original string fails because its type differs from integer 7. If the documented input grammar accepts "7", validate the whole string and allowed range, convert it to integer 7, then the strict search succeeds; authorization remains a separate decision.

Check your solution

  • The original string does not strictly match integer 7.
  • The validated integer matches, without implying authorization.

6. An identifier contract preserves leading zeroes. The allowed string IDs are "007" and "010"; a proposal casts incoming "007" to int. Explain the loss and a safer search.

Show hint

Identifiers are not necessarily quantities.

Show worked solution

Casting loses the leading zeroes and produces integer 7, changing the identifier representation. Validate the accepted string form and use strict string membership against the allowed strings; test "007" and "7" as distinct inputs.

Check your solution

  • The identifier "007" remains a string with leading zeroes.
  • The strict search distinguishes "007" from "7".

7. A record has flag=false, count=0 and label="". Determine isset and null-coalescing behavior for each, without treating them as absent.

Show hint

All three values are present and non-null.

Show worked solution

isset is true for all three keys. Each ?? fallback is bypassed and the original false, 0 or empty string is returned. If the domain rejects one of these values, add a separate rule rather than relying on presence.

Check your solution

  • isset is true for all three keys.
  • Coalescing preserves false, 0 and empty text.

8. Compare arrays ["a" => 1, "b" => 2] and ["b" => 2, "a" => 1] with == and ===. Predict both results and explain why a record comparison must choose an ordering contract.

Show hint

Strict array identity includes key order and types.

Show worked solution

The two arrays have the same key-value pairs, so == is true, but their key order differs, so === is false. Decide whether field order is semantically meaningful before using strict identity for records; test both ordering and value changes under the chosen contract.

Check your solution

  • Loose array equality is true for the stated records.
  • Strict identity is false because key order differs.

9. A patch arrives with profile present but profile.note absent. Explain why checking the parent alone is insufficient and specify a safe nested decision.

Show hint

Each array level has its own keys.

Show worked solution

First verify profile is an array under the accepted shape, then use array_key_exists on that child for note. The parent’s presence does not prove the child exists; apply the patch contract only after checking the correct level.

Check your solution

  • The profile shape is checked before child access.
  • The missing child is not treated as a present null.

10. A field defaults to 10 when absent or null, but preserves 0 and false. Create a four-case decision table and identify the suitable operator.

Show hint

The contract intentionally combines only absence and null.

Show worked solution

Null coalescing expresses this rule: absent and null produce 10, while integer 0 and boolean false remain unchanged. Assert both result value and type; a truthiness fallback would incorrectly replace the last two cases.

Check your solution

  • Absent and null yield 10.
  • Integer 0 and boolean false retain their values and types.

11. A search list contains integer 5 and string "05". Classify strict searches for string "5", integer 5 and string "05" and explain why normalization is a product decision.

Show hint

Strict membership compares type and value.

Show worked solution

String "5" is absent, integer 5 is present and string "05" is present. Converting or padding identifiers would change which values match; only do so after a documented normalization policy and validation establish that those forms are equivalent.

Check your solution

  • The outcomes are absent, present, present in order.
  • Normalization is not silently assumed.

12. A test accepts a supplied value but never checks its type afterward. The intended result for textual "3" is integer 3 after validation. Improve the assertions.

Show hint

Acceptance alone can hide an unintended representation.

Show worked solution

Assert the text passed the documented grammar and range, that the normalized result is strictly identical to integer 3, and that malformed text is rejected. This distinguishes successful conversion from merely preserving the string or accepting any value.

Check your solution

  • The resulting value is integer 3, not string "3".
  • A malformed input is rejected.

13. A developer uses empty($record["count"]) to decide whether a required field exists. Explain the result for missing, null and integer 0, then redesign the check.

Show hint

empty answers a broader false-like question.

Show worked solution

empty returns true for all three listed cases, so it cannot distinguish presence from a valid zero. Use array_key_exists for presence and then validate the value’s type and permitted range separately; define whether null is allowed.

Check your solution

  • Missing and present zero are distinguished by the redesigned check.
  • Type and range are validated after presence.

14. A match expression has arms 0 => "integer", "0" => "text", and default => "other". Classify integer 0, string "0", and false, and explain why this is not domain validation.

Show hint

match compares each arm strictly.

Show worked solution

Integer 0 selects "integer", string "0" selects "text", and false reaches "other" because neither arm has the same type and value. The branch labels only classify these inputs; separately decide whether each state is permitted by the domain contract.

Check your solution

  • The results are integer, text and other in order.
  • Domain acceptance is evaluated separately from classification.

15. A product ID is string "0". A function uses if ($id) before searching an allowlist. Explain the failure and define a type-aware alternative.

Show hint

String "0" is false-like in PHP.

Show worked solution

The truthiness condition skips a potentially valid string ID "0". Check the field’s presence, require the documented string shape and grammar, then perform strict membership in the string allowlist; test "0" alongside a missing key and an unlisted string.

Check your solution

  • A permitted string "0" reaches the strict search.
  • Missing and unlisted identifiers are rejected separately.

16. A record contains count="4" and the contract permits only integer 0–3. A strict type test fails. Explain why converting without a range test is still insufficient.

Show hint

Type normalization and domain acceptance are different.

Show worked solution

After validating the text grammar, conversion could produce integer 4, which passes an integer type check but exceeds the maximum 3. Reject it on the range rule and separately test a valid boundary such as text "3" if the contract permits textual input.

Check your solution

  • Converted integer 4 is rejected as out of range.
  • The valid maximum is tested under an explicit input policy.

17. A strict array_search returns key 2 for one value and key 0 for another. Design an assertion that accepts both keys but rejects a missing result.

Show hint

Inspect identity with false, not key truthiness.

Show worked solution

Compare each result with !== false to establish a match, then assert the exact expected key separately: 2 and 0. A missing value returns false and must fail the membership assertion; do not use a truthiness condition.

Check your solution

  • Keys 2 and 0 both count as matches.
  • The missing value returns false and fails the assertion.

18. A list allows false and integer 0 as distinct values. Predict strict membership for false, 0 and string "0" in [false, 0].

Show hint

Compare type and value for each candidate.

Show worked solution

Boolean false is present as the first element and integer 0 as the second; string "0" is absent because neither element is the same type and value. Keep the third argument true and assert each result independently.

Check your solution

  • The first two searches are true.
  • The string "0" search is false.

19. A CLI unit test proves that a synthetic record distinguishes missing and null. What remains to be checked for a JSON HTTP adapter?

Show hint

The parser and transport are separate boundaries.

Show worked solution

In an authorized isolated integration setup, send representative object payloads with an absent field and an explicit null, then verify the adapter passes distinct states to the decision and returns the documented response. The CLI unit result alone does not establish HTTP parsing, headers or status delivery.

Check your solution

  • The integration test includes both absent and explicit null.
  • Actual adapter parsing and HTTP response are checked separately.

20. A field is present as an array ["0"] but its contract accepts only a scalar string. Explain why checking the key alone is insufficient and define the decision.

Show hint

Presence does not establish shape.

Show worked solution

array_key_exists can confirm the field is present, but the array violates the scalar-string contract. Reject the shape before string validation or numeric conversion, associate the error with that field and preserve any safe companion values.

Check your solution

  • The key is present but the value is rejected as an array.
  • No conversion or domain effect follows the shape failure.

Key takeaways

  • Separate presence, type, and domain validation.
  • Choose comparison semantics deliberately.
  • Presence and truthiness answer different questions.
  • Strict comparisons do not validate business ranges.
  • A search result at key zero is not false.

Official references