Changes

Jump to: navigation, search

Working with Strings and Text in PHP

1,451 bytes added, 16:04, 4 June 2007
Comparing Strings in PHP
== Comparing Strings in PHP ==
 
In developing web applications it is extremely common to need to compare two strings. PHP provides a number of functions that make it easy to perfrom a number of different string comparison tasks:
 
* '''strcmp()''' - Accepts two strings as arguments, performs a case-sensitive comparison and returns a value depending on the match.
 
* '''strcasecmp''' - Accepts two strings as arguments, performs a case-insensitive comparison and returns a value depending on the match.
 
* '''strncmp()''' - Accepts three arguments - the two strings to be compared and the number of characters to be included in the comparison. Performs a case-sensitive comparison of specified number of characters from each string and returns a value depending on the result of the match.
 
* '''strncasecmp()''' - Accepts three arguments - the two strings to be compared and the number of characters to be included in the comparison. Performs a case-insensitive comparison of specified number of characters from each string and returns a value depending on the result of the match.
 
=== String Comparison Functions Return Value ===
 
The string comparison function perform an ASCII based comparison of each character. If the ASCII codes of the two strings match then the functions return 0. If the first string has a ASCII value less than the second a negative number is returned. If it is greater a positive number is returned.
 
== Accessing and Modifiying Characters in String ==

Navigation menu