| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Advanced ConditionalsThese tutorials are about PHP and its use for server-side Web programming. Advanced Conditionals
When working with comparing strings on computers we sometimes have to deal with more complicated comparisons than a simple conditional. This is because computers expect to be working with numbers when comparing values. In PHP, when comparing a string to a number, the string will first be cast to a number. This means that all strings that don't begin with a numeric value will be cast to zero. For instance, Even when working with strings, computers will, unless told otherwise, compare the numeric values of the individual characters in the string until the condition in question is determined. This means that without an other information, most computers tend to sort strings into ASCII order. In ASCII, upper-case and lower-case letters have different values, so the sort is case sensitive. For instance 'BAT' will compare to be less than 'cat', even though B comes before C in the alphabet, because the B is in upper case. This is not always what we want.
Even worse if we compare numeric strings when we want to compare their numeric values, if we don't cast them to numbers first, we can get very odd results. For instance, the expression Normally comparisons normally are also limited to equality and difference. Which is to say, we can test for equality, inequality, and the direction of inequality (greater than or less than), and that is it. What happens if we want to test whether two things are similar? Or whether two words sound the same? This section looks as other ways to compare strings, both for non-ASCII sorts and for comparisons that test more than equality or direction of difference. Comparing StringsIf you want to make sure you are always comparing strings, rather than using comparison operators, you should use string comparison functions. The ensure that all arguments are cast to strings before comparing them and allow you to control the method in which the comparison occurs. All string comparisons take (At least two arguments and return a value based on comparing those arguments. The return value is always an integer that can be interpreted as follows:
Our basic set of string comparison functions are as follows:
SimilarityPHP also provides numerous ways of comparing strings to see whether they are similar. We can compare for textual similarity and for phonic similarity. Phonic SimilarityPhonic similarity first, since it is a little easier. Phonic similarity just means that things are compared based on whether they sound alike. To compare things by how they sound you can use one of two functions. Both assume English pronunciation. Given that PHP was first developed in Toronto, it is fair to assume that English means neutral American English (but don't hold me to that). The two functions are:
Both return strings that represent the pronunciation of a word. Note that the result strings themselves are not pronounceable, but are strings to represent the sound of the word in a standardized way. Of the two, You can use the result values in comparisons for equality.
if (metaphone($a) == metaphone($b) {
...
}
The only real purpose of these functions is to look for homophones, or words that sound alike. This is something that is primarily of use if you are planning on writing spell-checkers and other advanced text processing tools. Textual SimilarityTextual similarity is a little more useful because it allows you to compare how similar two text strings are.
|