Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7943149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:11:01+00:00 2026-06-04T00:11:01+00:00

I am comparing two string by strcmp(1,2) and i am getting -1 insted of

  • 0

I am comparing two string by strcmp(1,2) and i am getting “-1 ” insted of 1 my code is below

  <?php
    echo strcmp(1,2);
   ?>

output:-1

enter image description here

please let me know why i am getting -1 for false and how to handle it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:11:04+00:00Added an answer on June 4, 2026 at 12:11 am

    1 is less than 2, strcmp is defined as returning:

    Returns < 0 if str1 is less than str2; > 0 if str1 is greater than
    str2, and 0 if they are equal.

    Thus the behaviour is expected.


    Note the following caveat:

    If you rely on strcmp for safe string comparisons, both parameters
    must be strings, the result is otherwise extremely unpredictable. For
    instance you may get an unexpected 0, or return values of NULL, -2, 2,
    3 and -3.

    From a comment on the documentation page for strcmp.


    PHP is full of this sort of unexpected behaviour. In your situation, I would ensure both arguments were cast as strings to avoid any confusion:

    $first = 1;
    $second = 2;
    echo strcmp((string)$first, (string)$second);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a difference when comparing two string which are 0 and '0'
I am comparing two strings using following code string1.Contains(string2) but i am not getting
I need help getting two pieces of data, and comparing them. What I am
i will be comparing two values like this:\ value1>value2 i know that value2 is
i am facing a weird problem i am simply comparing two string values and
When comparing two strings how to avoid checking if a string is of different
I'm comparing two strings like so: <?php $Str1 = '111122223333444455556666'; $Str2 = '111122223333444455557777'; if($Str1
I'm doing some calculations of comparing two strings. In case I know they are
I've had some success comparing strings using the PHP levenshtein function. However, for two
I am stuck in comparing two unicode strings in PHP which both contain the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.