[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
4. php expressions
5. php client side
6. php flow control
a. php conditionals
* b. php adv conditions
c. php iteration
d. php functions
e. php adv functions
f. php modularity
7. php manual


print version

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.

Directions for surfing with audio.

Advanced Conditionals

These 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, if ('PHP' > 5) will actually be compared as if (0 > 5). This can be a problem.

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 ('2' > '100') will evaluate to true because in the first position 2 is greater than 1. The other character positions are irrelevant since the first string only has one character.

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 Strings

If 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:

  • 0 (zero): the two values are equal
  • > 0 (greater than zero): value two is greater than value one
  • < 0 (less than zero): value one is greater than value two

Our basic set of string comparison functions are as follows:

strcmp(str1, str2);
Takes two arguments and compares them as strings. In order for any of these functions to work, both arguments must be values that can be cast to strings. The strings are compared in ASCII order.
strcasecmp(str1, str2);
Takes two arguments and compares them as strings. The strings are compared in ASCII order, but without regard to case sensitivity. All alphabetic characters are treated as if they were lower-case.
strnatcmp(str1, str2);
Takes two arguments and compares them as strings. The strings are compared in ASCII order, except for numbers, which are compared in natural order, which is to say, as numbers. This is true even if they occur in the middle of the string. Thus 'February 2' will evaluate to less than 'February 14'. Whereas on a normal string comparison it would be greater since 2 is greater than 1.
strnatcasecmp(str1, str2);
This combines the previous two functions and performs a case insensitive search with numbers sorted in natural order.
strncmp(str1, str2, length);
Takes three arguments, two strings to compare and the length to which to compare them. This allows you to perform a comparison on just the beginning portion of a string from index position zero to the specified length.
strncasecmp(str1, str2, length);
This is the same as the previous function except that it performs a case insensitive search.

Similarity

PHP also provides numerous ways of comparing strings to see whether they are similar. We can compare for textual similarity and for phonic similarity.

Phonic Similarity

Phonic 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:

  • soundex()
  • metaphone()

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, metaphone() tends to be more accurate because its algorithms for the rules of pronunciation are better.

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 Similarity

Textual similarity is a little more useful because it allows you to compare how similar two text strings are.

similar_text()

The similar_text() function takes two string and returns an index value that returns the number of characters that are the same, allowing for additions, subtraction, and repetition. It also takes an optional third parameter which returns a value that represents the percentage of similarity.

similar_text($a, $b[, $percent]);

$match = similar_text($a, $b, $mper);

levenshtein()

The levenshtein() function returns a weighted score representing the difference between two strings. It takes two strings and three optional arguments. The three optional arguments allow you to specify how much weight to assign to different types of difference. This allows you to specify how you want scores weighted with numeric values. Otherwise all scores are given equal weight.

Our difference types are:

  • insertions between string one and two
  • replacements between string one and two
  • deletions between strings one and two
levenshtein($a, $b[, $ins, $rep, $del])

$diff = levenshtein($a, $b, 100, 5, 1)

[top]