Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

clamp() Is Polite In Chinese

<?php
  
// 我: I, me
// 你: you
echo clamp(-1, "我", "你");

//PHP Fatal error:  Uncaught ValueError: clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)

clamp() makes sure a value is within a borded interval. This works with integers and date fines, with the function actually checking that the upper bound is higher than the lower one.

With strings, it also checks the limits, though it uses the underlying character. With multi-bytes characters, which have no sorting anyway, this means some values are actually ranked higher than others. In the case of Chinese characters here, you is sorted below me, so using them in the wrong order gets you a fatal error.

Note that in English, it doesn’t work, as I is capital, so it ranks below you.

Anyway, clamp() should probably not used with data that cannot be sorted.

See Also

PHP Error Messages

PHP Features

Last updated: 19 November 2026