4.3 Accessing Individual Characters


The strlen( ) function returns the number of characters in a string:

$string = 'Hello, world'; $length = strlen($string);             // $length is 12

You can use array syntax (discussed in detail in Chapter 5) on a string, to address individual characters:

$string = 'Hello'; for ($i=0; $i < strlen($string); $i++) {   printf("The %dth character is %s\n", $i, $string[$i]); } The 0th character is H The 1th character is e The 2th character is l The 3th character is l The 4th character is o


Programming PHP
Programming PHP
ISBN: 1565926102
EAN: 2147483647
Year: 2007
Pages: 168

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net