Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

comparing strings

Featured Replies

Are these two functions identical or is one more secure/reliable that the other...

 

if (strcmp($string1,$string2) === '0') {
echo 'match';
} else {
echo 'no match';
}

 

and

 

if ($string1 === $string2) {
echo 'match';
} else {
echo 'no match';
}

Well first and foremost, your original code will never output "match". strcmp returns an integer, so '0' will never match when the strings do. You need to remove the quotes from around it

 

As for them being identical, yes they will work just the same if you make my edit above. Note that you also don't need the third = in your first example either, since it's always going to return an integer

Edited by Jay Gilford

If you're wanting to compare them and work out if they're identical to each other yes. strcmp does more than just compare if they are identical, it actually tests if the first string is a higher/lower value to the second string

  • Author

I actually just did a test and there is a slight difference...

 

If $string1 = false

and $string 2 = ""

 

if ($string1 === $string2) {
echo 'match';
} else {
echo 'no match';
}

echo '<br />';

if (strcmp($string1,$string2) === 0) {
echo 'match';
} else {
echo 'no match';
}

 

The first example outputs 'no match' while the second example outputs 'match', that is with the second example being === or ==

  • Author

If you're wanting to compare them and work out if they're identical to each other yes. strcmp does more than just compare if they are identical, it actually tests if the first string is a higher/lower value to the second string

Ok that's cleared my question up, I read the php manual but it didn't say anything about using the first example compared to strcmp.

Why are you using a boolean value for a string? strcmp is expecting two string input. it will assume the false as an empty string

Edited by Jay Gilford

  • Author

Why are you using a boolean value for a string? strcmp is expecting two string input. it will assume the false as an empty string

I was just checking various combinations to see if there were any differences.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.