Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Comparing Strings PHP
 
Article Index
Comparing Strings PHP
Page 2
Page 3

Comparing Strings

Determining the relationship between two strings is not as immediately obvious a feat as performing the same operation on two numbers. The main problem with strings is one of context. If you examine a string based on its binary form, the two words "Marco" and "marco" will be completely different because the byte value of the character "M" ishas to bedifferent from the value of "m". However, depending on your requirements, Marco and marco could be equivalent and should be treated as such.

The easiest way to compare two strings is to use the built-in PHP comparison operators. However, there are a few "gotchas" that you should be aware of. Consider, for example, the following expression:

echo (0 == '0');

Because one of the operators is an integer, the string "0" is converted to an integer value before the conversion is made, resulting in the output 1. Now, this may not look like much of a problem at first sight, but it can very easily become one when something like this happens:

echo (0 == 'Marco');

Because the string 'Marco' is converted to the integer value 0 when the expression is evaluated, the result of the comparison operation is still true, and the preceding code snippet outputs 1. Now, there's a good chance that you will never want something like this to happen to your code and, therefore, you should never use the simple comparison operators when dealing with strings unless you really know what you're doing.


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: PHP Select, PHP Examples, PHP Echo, PHP Object, PHP Substr, PHP, PHP Arrays, Arrays, Function, PHP Hosting.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Wallpaper 18
Statistic


Last Post

 
Top! Top!