I am making a PHP login/register form and to confirm whether someone is actually human and not a bot I am generating a random value that has to be written in a certain way. For example:
Please write the following number in an increasing manner: 34745 The user should write 34457 and only then will the confirmation work.
The thing is I know how to use the rand() function, that's how I generate the number. But the problem is I do not know how to make PHP sort the generated number's numerical values (for ex.: 64348 -> 34468). How do I do this? I hope there is a single function for that, as I've seen numerous ways to sort arrays and since they have indexes I only suppose that it should be possible to sort a number's values.
<?php
$number = str_split("647214");
sort($number);
$number = implode($number);
echo $number;
?>
I don't think it has single function, but you can make this.
$num = 34745;
$chars = preg_split('/ /', $num, -1, PREG_SPLIT_OFFSET_CAPTURE);
sort($chars);
$res = implode("", $chars);
echo $res;
I didn't tested this, but I hope it works!
Instead of using an integer to hold the complete number you could create an array of size n (n is the number of digits). Iterate over the array and use rand() to create random numbers from 0-9 in each position of the array. Now you have an array which can be sorted by using standard sort functions.