tips and tricks

a++: Using PHP strings in unusual ways

Here's a PHP question for you: When the following code is executed, what will the output be (or will the code fail)?

<?php
$char = 'a';
print ++$char . PHP_EOL;
?>

The answer: The above will print a single line.

b

The reason for this is that an increment operation on a string increments the letter. In short, it treats strings (in this context) more like a char type as in C or Java. Well, it treats them sorta like a char. PHP has some special... behaviors when it comes to strings and incrementing operations. In this article, I'll show some of those behaviors.

Syndicate content