Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the Increment Operator on Strings in PHP? [closed]

I am learning PHP for the first time and I find it surprising that the language allows using the increment operator on strings.

     $foo = 'xyZ';
     print ++$foo; // prints xzA

The tutorials I can find around this topic introduce toy examples only. I would be grateful if you mention a situation where using this 'feature' is beneficial.

Thanks in advance!

like image 496
Helmyano Avatar asked Oct 04 '22 16:10

Helmyano


1 Answers

I would be grateful if you mention a situation where using this 'feature' is beneficial.

This can be a very useful feature ...

Example

$guess = "21661093e56e24cd60b10092005c4ac7";
$next = "aaaa";
$count = 0;
while(md5($next) !== $guess) {
    $next ++;
    $count ++;
}
printf("Found `%s` after %s loops", $next, number_format($count));

Output

Found `baba` after 17,602 loops

I don't intend to crack any PIN or password anytime soon anyway

like image 119
Baba Avatar answered Oct 07 '22 20:10

Baba