Eduardo Arsand

Understanding Common Edge Cases in PHP and JavaScript

39

Edge cases are inputs or situations that sit at the boundaries of expected behavior — rare, extreme, malformed, or unexpected conditions that can break otherwise working code.

They are not bugs by themselves. They are valid but uncommon scenarios that must be handled deliberately.

Common Edge Cases in PHP

1. Type Juggling (Loose Comparisons)

PHP performs implicit type coercion.

<?php
	var_dump("0" == false);     // true
	var_dump("123abc" == 123);  // true
?>

Risk: Authentication bypass, logic errors.

Mitigation: Always use strict comparisons (===) and enable strict types.

2. null, Empty Strings, "0", and false

<?php
	empty("0"); // true
?>

"0" is considered empty. Avoid using empty() in business logic and use explicit checks instead.

3. Array Key Confusion

<?php
	$array = [
		"1" => "a",
		1 => "b"
	];
	print_r($array); // only one key
?>

Numeric string keys are converted to integers.

4. Floating Point Precision

<?php
	var_dump(0.1 + 0.2 === 0.3); // false
?>

Never use floats for money. Use integers (cents) or BCMath.

5. Undefined Index or Property

<?php
	echo $_GET['id']; // Notice if not set
?>

Use null coalescing:

<?php
	$id = $_GET['id'] ?? null;
?>

6. Encoding Issues (UTF-8)

<?php
	strlen("á");     // 2
	mb_strlen("á");  // 1
?>

Always use multibyte-safe functions.

Common Edge Cases in JavaScript

1. Truthy and Falsy Values

Boolean([])        // true
Boolean({})        // true
Boolean("")        // false
Boolean("0")       // true
Boolean(0)         // false
Boolean(null)      // false
Boolean(undefined) // false

Avoid relying on implicit truthiness in critical logic.

2. == vs ===

"0" == 0      // true
"" == 0       // true
false == []   // true

Always use strict equality (===).

3. NaN

NaN === NaN // false

Use Number.isNaN(value) for proper checking.

4. Floating Point Precision

0.1 + 0.2 === 0.3 // false

5. Async Timing Issues

let result;

fetch(url).then(r => {
	result = r;
});

console.log(result); // undefined

Use async/await and avoid shared mutable state.

6. Undefined vs Null

typeof undefined  // "undefined"
typeof null       // "object"

Be explicit about API contracts.

How to Organize Code to Handle Edge Cases

1. Validate at the Boundaries

Never trust external input. Validate early and explicitly.

2. Use Defensive Programming

<?php
	return $user?->profile?->address?->street ?? null;
?>
return user?.profile?.address?.street ?? null;

3. Centralize Normalization

Normalize empty values once so the domain layer does not handle inconsistent data.

4. Separate Layers

  • Controller – HTTP concerns
  • Service – Business rules
  • Domain – Pure logic
  • Infrastructure – Database and APIs

Sanitize in controllers, validate in services, assume correctness in domain logic.

5. Use Value Objects

Encapsulate validation in constructors so invalid states cannot exist in core logic.

6. Test Weird Inputs

  • null
  • Empty strings
  • Negative numbers
  • Large numbers
  • Invalid JSON
  • Corrupted encoding
  • Race conditions

7. Prefer Explicit State Over Implicit

if (user !== null && user !== undefined)

Engineering Approach

Think in three categories:

  • Invalid input – reject early
  • Unexpected state – throw exception
  • Valid but rare scenario – handle explicitly

Advanced systems are designed so that:

  • Invalid states are unrepresentable
  • Input normalization happens once
  • Domain logic assumes correctness
  • Strict typing is enforced
  • Tests target boundary conditions first

When structured correctly, edge cases stop being surprises and become defined behavior.


Comments ({{ modelContent.total_comments }})