Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to Copy String in PHP ?
Next article icon

String comparison using == vs strcmp() in PHP

Last Updated : 30 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see the string comparison using the equal (==) operator & strcmp() Function in PHP, along with understanding their implementation through the example.

PHP == Operator: The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns a true value if both of the values are the same (It compares the only value of the variable, not data types) and returns a false value if both of the values are not the same. 

This should always be kept in mind that the present equality operator == is different from the assignment operator =. The assignment operator assigns the variable on the left to have a new value as the variable on right, while the equal operator == tests for equality and returns true or false as per the comparison results.

Example: This example describes the string comparison using the == operator. 

PHP




<?php
 
  // Declaration of strings
  $name1 = "Geeks";
  $name2 = "Geeks";
 
  // Use == operator
  if ($name1 == $name2) {
      echo 'Both strings are equal';
  }
  else {
      echo 'Both strings are not equal';
  }
?>
 
 

Output:

Both the strings are equal

PHP strcmp() Function: The strcmp() is an inbuilt function in PHP that is used to compare two strings. This function is case-sensitive which points that capital and small cases will be treated differently, during comparison. This function compares two strings and tells whether the first string is greater or smaller or equals the second string. This function is binary-safe string comparison.

Syntax:

strcmp( $string1, $string2 )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $string1: This parameter refers to the first string to be used in the comparison. It is a mandatory parameter.
  • $string2: This parameter refers to the second string to be used in the comparison. It is a mandatory parameter.

Return Values: The function returns a random integer value depending on the condition of the match, which is given by: 

  • Returns 0 if the strings are equal.
  • Returns a negative value (< 0), if $string2 is greater than $string1.
  • Returns a positive value (> 0) if $string1 is greater than $string2.

Example: This example illustrates the string comparison using the strcmp() function.

PHP




<?php
 
  // Declaration of strings
  $name1 = "Geeks";
  $name2 = "geeks";
 
  // Use strcmp() function
  if (strcmp($name1, $name2) !== 0) {
      echo 'Both strings are not equal';
  }
  else {
      echo 'Both strings are equal';
  }
?>
 
 

Output:

Both strings are not equal

Reference:

  • http://php.net/manual/en/language.operators.comparison.php
  • http://php.net/manual/en/function.strcmp.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Next Article
How to Copy String in PHP ?
author
sarthak_ishu11
Improve
Article Tags :
  • PHP
  • PHP Programs
  • Technical Scripter
  • Web Technologies
  • PHP-Questions

Similar Reads

  • How to Compare Two Strings without using strcmp() function in PHP ?
    Comparing two strings is a common operation in programming. In PHP, the strcmp() function is typically used to compare two strings. However, there may be situations where you need to compare strings without using this built-in function, such as for learning purposes or to meet specific constraints.
    4 min read
  • Generating Random String Using PHP
    Generating a random string involves creating a sequence of characters where each character is selected unpredictably from a defined set (e.g., letters, numbers, symbols). This process is used in programming to produce unique identifiers, passwords, tokens, or keys for security and randomness in appl
    2 min read
  • How to Copy String in PHP ?
    Copying a string is a basic operation that all the programming languages offer. In this article, we will explore different approaches to copying a string in PHP, including using the assignment operator, the strval() function, the substr() function and by implementing the strcpy() function. Table of
    3 min read
  • How to prepend a string in PHP?
    We have given two strings and the task is to prepend a string str1 with another string str2 in PHP. There is no specific function to prepend a string in PHP. To do this task, we have the following operators in PHP: Below are the methods to prepend a string in PHP Table of Content Using Concatenation
    2 min read
  • Split a String into an Array of Words in PHP
    In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array. PHP provides several methods to achieve this, making it easy to convert a string into an array of words: Table of Content Using ex
    3 min read
  • How to check if URL contain certain string using PHP?
    Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find
    4 min read
  • How to generate simple random password from a given string using PHP ?
    In this article, we will see how to generate a random password using the given string. We have given a string and the task is to generate a random password from it. Example: Input: abgADKL123 Output: abgADKL123 Input: geeksforgeeks Output: egksegsfroeke To achieve this, we use the following approach
    3 min read
  • Comparing Two Dates in PHP
    Working with dates is a common task in PHP, whether you're building event calendars, validating user input, setting expiration times, or simply checking if one date is earlier than another. PHP offers multiple ways to compare dates, from simple string comparisons to using the powerful DateTime class
    3 min read
  • How to replace a word inside a string in PHP ?
    Given a string containing some words the task is to replace all the occurrences of a word within the given string str in PHP. To do this task, we have the following methods in PHP: Table of Content Using str_replace() MethodUsing str_ireplace() Method Using preg_replace() MethodUsing strtr()Using pr
    4 min read
  • When to use static vs instantiated classes in PHP?
    Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (m
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences