Validation Patterns
Length Checks
Check whether a string is long enough.
length-check
Length validation records both the measured length and the decision that uses it.
Length Checks
length_check.php
Replay: real traced execution (multi-file project)
<?php
$password = "code";
$length = strlen($password);
$status = $length >= 6 ? "strong" : "short";
echo "length=" . $length . "\n";
echo "status=" . $status . "\n";
<?php
$password = "secret12";
$length = strlen($password);
$status = $length >= 6 ? "strong" : "short";
echo "length=" . $length . "\n";
echo "status=" . $status . "\n";
<?php
$password = "x";
$length = strlen($password);
$status = $length >= 6 ? "strong" : "short";
echo "length=" . $length . "\n";
echo "status=" . $status . "\n";
$password ← code, $length ← 4, $status ← short
1<?php2$password→ code = "code"; //@password="secret12", "x"3$length→ 4 = strlen($passwordcode);4$status→ short = $length4 >= 6 ? "strong" : "short";56echo "length=" . $length4 . "\n";7echo "status=" . $statusshort . "\n";outputlength=4 status=short
$password ← secret12, $length ← 8, $status ← strong
1<?php2$password→ secret12 = "secret12";3$length→ 8 = strlen($passwordsecret12);4$status→ strong = $length8 >= 6 ? "strong" : "short";56echo "length=" . $length8 . "\n";7echo "status=" . $statusstrong . "\n";outputlength=8 status=strong
$password ← x, $length ← 1, $status ← short
1<?php2$password→ x = "x";3$length→ 1 = strlen($passwordx);4$status→ short = $length1 >= 6 ? "strong" : "short";56echo "length=" . $length1 . "\n";7echo "status=" . $statusshort . "\n";outputlength=1 status=short