
Wikis & learning
Give a configuration limit one name
A named constant is suitable for a value that should remain the same during a request. Using one name for the page size prevents the limit and the display calculation from drifting apart.
A named constant is suitable for a value that should remain the same during a request. Using one name for the page size prevents the limit and the display calculation from drifting apart.
Walkthrough
Normal variables use the dollar prefix; named constants do not. A magic constant can describe the location of the code without requiring a hard-coded file path.
PHP example
<?php
const PAGE_SIZE = 12;
function startingOffset(int $page): int {
return max(0, $page - 1) * PAGE_SIZE;
}
echo startingOffset(3), ' entries to skip';Expected output
24 entries to skipChange and check
Change the constant value and run the example again. Print __LINE__ on two different lines to compare it with an ordinary named constant.
Discussion
Sign in to participate.



