Christian Kruse: Zufallsbuchstabe

Beitrag lesen

你好 AllesMeins,

mt_srand((double)microtime()*1000000);

Besser: mt_srand((double)(microtime() ^ posix_getpid()));

$zeichen = "abcdefghijklmnopqrstuvwxyz".
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
buchstabe = $zeichen[mt_rand(0,strlen($zeichen))];

Besser, weil definiert:

$buchstabe = $zeichen{mt_rand(0,strlen($zeichen))];

Der Zugriff auf einen String via [] ist nicht definiert und kann sich
jederzeit ändern:

=========================
Note: The behaviour of an automatic conversion to array is currently undefined.

<?php
$a = "1";    // $a is a string
$a[0] = "f";  // What about string offsets? What happens?
?>

Since PHP (for historical reasons) supports indexing into strings via offsets using the same syntax as array indexing, the example above leads to a problem: should $a become an array with its first element being "f", or should "f" become the first character of the string $a?

The current versions of PHP interpret the second assignment as a string offset identification, so $a becomes "f", the result of this automatic conversion however should be considered undefined. PHP 4 introduced the new curly bracket syntax to access characters in string, use this syntax instead of the one presented above:

<?php
$a    = "abc"; // $a is a string
$a{1} = "f";  // $a is now "afc"
?>

See the section titled String access by character for more information.

再见,
 克里斯蒂安

--
Neues Block: „ähm Jeena kennt Wayne?“
Savage Circus: Dreamland Manor
Der Verstand ist der Hausherr, der Koerper sein Gast.
http://wwwtech.de/