DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Database Integration Tests With Spring Boot and Testcontainers
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Database Integration Tests With Spring Boot and Testcontainers
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  1. DZone
  2. Coding
  3. JavaScript
  4. How To Compare Strings In PHP

How To Compare Strings In PHP

Paul Underwood user avatar by
Paul Underwood
·
Jun. 25, 13 · Interview
Like (0)
Save
Tweet
Share
79.48K Views

Join the DZone community and get the full member experience.

Join For Free

During any sort of programming you will always get situations where you need to compare values with each other, if the values are boolean or integers then the comparison is simple. But if you want to compare strings or parts of strings then there can be more to the comparison such as case of the string you are comparing.

In this tutorial we are going to look at all the different ways you can compare strings in PHP using a number of built in PHP functions.

== operator

The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it returns true.

if('string1' == 'string1')
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}

This code will return that the strings match, but what if the strings were not in the same case it will not match. If all the letters in one string were in uppercase then this will return false and that the strings do not match.
if('string1' == 'STRING1')
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}

This means that we can't use the == operator when comparing strings from user inputs, even if the first letter is in uppercase it will still return false. So we need to use some other function to help compare the strings.

strcmp Function

Another way to compare strings is to use the PHP function strcmp, this is a binary safe string comparison function that will return a 0 if the strings match.

if(strcmp('string1', 'string1') == 0)
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}

This if statement will return true and echo that the strings match. But this function is case sensitive so if one of the strings has an uppercase letter then the function will not return 0.

strcasecmp Function

The previous examples will not allow you to compare different case strings, the following function will allow you to compare case insensitive strings.

if(strcasecmp('string1', 'string1') == 0)
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}
if(strcasecmp('string1', 'String1') == 0)
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}
if(strcasecmp('string1', 'STRING1') == 0)
{
     echo '
Strings match.
';
} else {
     echo '
Strings do not match.
';
}

All of these if statements will return that the strings match, which means that we can use this function when comparing strings that are input by the user.

Strings PHP

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Database Integration Tests With Spring Boot and Testcontainers
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: