Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulund/5894523 to your computer and use it in GitHub Desktop.
Save paulund/5894523 to your computer and use it in GitHub Desktop.
A number of ways to compare string with PHP, used in the post http://www.paulund.co.uk/how-to-compare-strings-in-php
<?php
// Using the == operator, Strings do not match is printed
if('string1' == 'STRING1')
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
<?php
// Using the == operator, Strings match is printed
if('string1' == 'string1')
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
<?php
// Both strings will match
if(strcasecmp('string1', 'string1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
// Both strings will match even with different case
if(strcasecmp('string1', 'String1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
// Both strings will match even with different case
if(strcasecmp('string1', 'STRING1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
<?php
// strcmp function, Strings match is printed
if(strcmp('string1', 'string1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment