Change language

Comparing strings using == vs strcmp () in PHP

| |
Example :  
// Declaring strings $name1 = "Geeks" ; $name2 = "Geeks" ;  
// Use the == operator if ( $name1 == $name2 ) { echo ’Both strings are equal’ ; } else { echo ’ Both strings are not equal’ ; }
?>
Exit:
Both strings are equal 
strcmp() function
Strcmp() - it is a built-in function in PHP that is used to compare two strings. This function is case sensitive, which indicates that uppercase and small cases will be handled differently during comparison. This function compares two strings and reports whether the first string is greater than or less than or equal to the second.Syntax:
strcmp ($string1, $string2) 
Parameters:This function takes two parameters as above and described below:
  • $string1:this the parameter refers to the first line to be used in the comparison. This parameter is required.
  • $string2:This parameter refers to the second string to be used in the comparison. This parameter is required.

Returned values:
the function returns a random integer value depending on the match condition, which is specified as:
  • 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 :  
// Declaring strings $name1 = "Geeks" ; $name2 = "engineer" ;  
// Using the strcmp() function if ( strcmp ( $name1 , $name2 )! == 0) { echo ’Both strings are not equal’ ; } else { echo ’ Both strings are equal’ ; }
?>
Exit:
Both strings are not equal 
Link :
  • http://php.net/manual/en/language.operators.comparison.php
  • http://php.net/manual/en/function.strcmp.php

Comparing strings using == vs strcmp () in PHP cmp: Questions

Comparing strings using == vs strcmp () in PHP PHP: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method