142Part IPHP: The BasicsThe strpos()function is one of (Web server setup)

142Part IPHP: The BasicsThe strpos()function is one of those cases where PHP s type-looseness can be problematic. If no match can be found, the function returns a false value; if the very first character is a match, the function returns 0(because the indexing count starts with 0 rather than 1). Both of thesevalues look false if used in a Boolean test. One way to distinguish them is to use the identitycomparison operator (===, introduced as of PHP4), which is true only if its arguments are thesame and of the same type you can use it to test if the returned value is 0(or is FALSE) with- out risk of confusion with other values that might be the same after type coercion. If you areusing PHP3, you need to do explicit type testing with, for example, is_integer(). The strpos()function can also be used to search for a substring rather than a single charac- ter, simply by giving it a multicharacter string rather than a single-character string. You canalso supply an extra integer argument specifying the position to begin searching forward from. Searching in reverse is also possible, using the strrpos()function. (Note the extra r, whichyou can think of as standing for reverse.) This function takes a string to search and a single- character string to locate, and it returns the last position of occurrence of the second argu- ment in the first argument. (Unlike with strpos(), the string searched for must have onlyone character.) If we use this function on our example sentence, we find a different position: $twister = Peter Piper picked a peck of pickled peppers ; printf( Location of p is . strrpos($twister, p ) .
); Specifically, we find the third pin peppers: Location of p is 40CautionAre strings immutable? In some programming languages (such as C), it is common to manipulate strings by directlychanging them that is, storing new characters into the middle of an existing string, replacingold characters. Other languages (like Java) try to keep the programmer out of certain kinds oftrouble by making string classes that are immutable(or unchangeable) you can make newstrings by creating modified copies of old ones, but once you have made a string, you are notallowed to change it by directly changing the characters that make it up. Where does PHP fit in? As it turns out, PHP strings can be changed, but the most common practice seems to be to treat strings as immutable. Strings can be changed by treating them as character arrays and assigning directly into them, likethis: $my_string = abcdefg ; $my_string[5] = X ; print($my_string .
); which will give the browser output: abcdeXgThis modification method seems to be undocumented, however, and shows up nowhere in theonline manual, even though the corresponding extraction method (now updated to use curlybraces) is highlighted. Also, almost all PHP string-manipulation functions return modified copiesof their string arguments rather than making direct changes, which seems to indicate that this isthe style that the PHP designers prefer. Our advice is not to use this direct-modification methodto change strings, unless you know what you are doing and there is some large benefit in termsof memory savings.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply