fork download
  1. <?php
  2.  
  3. function r(){
  4. $n=rand(65,91);
  5. // if $n == 91, then $n-91 returns 0 (e.g. false), then we use 32 (e.g. ord(' '))
  6. // if $n != 91, then $n-91 returns not 0 (e.g. true), so we use $n
  7. return chr($n-91?$n:32);
  8. }
  9.  
  10. function s($s){
  11. $c=0;
  12. $t='METHINKS IT IS LIKE A WEASEL';
  13. // The snippet "$s[$i]==$t[$i++]" returns true/false if $s[$i] == $t[$i]
  14. // and has the side effect of incrementing $i
  15. // $c++ is the same as $c+=TRUE
  16. for($i=0;$i<28;$c+=$s[$i]==$t[$i++]);
  17. return $c;
  18. }
  19.  
  20. for ($n = 65; $n <= 91; $n++) { var_dump(chr($n-91?$n:20)); }
  21. var_dump( s('ZZZZZZZZ IT IS LIKE A WEASEL') );
  22.  
  23. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "D"
string(1) "E"
string(1) "F"
string(1) "G"
string(1) "H"
string(1) "I"
string(1) "J"
string(1) "K"
string(1) "L"
string(1) "M"
string(1) "N"
string(1) "O"
string(1) "P"
string(1) "Q"
string(1) "R"
string(1) "S"
string(1) "T"
string(1) "U"
string(1) "V"
string(1) "W"
string(1) "X"
string(1) "Y"
string(1) "Z"
string(1) ""
int(20)