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 6127421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:30:22+00:00 2026-05-23T16:30:22+00:00

I have a problem baffling me terribly. I noticed this before but didn’t give

  • 0

I have a problem baffling me terribly. I noticed this before but didn’t give it any heed until today.
I was trying to write my own check for integer strings. I know of is_numeric() but it does not suffice since it counts float as numeric not only integers and is_int() which does not work on string numbers.

I did something similar to this


$var1 = 'string';
$var2 = '123'; 

var_dump( (int)$var1 == $var1);// boolean true 
var_dump((int)$var2 == $var2);// boolean true

 var_dump((int)$var1);//int 0
 var_dump($var1);//string 'string' (length=6)

As expected the second var dump outputs true since I expect with php’s loose comparison that the string and integer versions be equal.

However with the first, I don’t get why this is so. I have tried casting to bool and it still gives me the same result.

I have tried assigning the cast var to a new variablr and comparing the two, still the same result

Is this something I am doing wrong or it is a php bug?

***Note
I am not comparing types here. I’m actually trying to take advantage of the fact that int 0 is not equal to string 'string'.

I wrote my integer check differently so I don’t really need alternatives for that.

***Edit
I did some extra checking and it turns out that 0 == 'string' is true as well. How is that possible?

***Edit 2
There are multiple correct answers below to the question. Thanks to everyone who answered.

  • 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-05-23T16:30:23+00:00Added an answer on May 23, 2026 at 4:30 pm

    It’s not a bug, it’s a feature. Any string can be casted to an integer, but the cast will return 0 if the string doesn’t start with an integer value. Also, when comparing an integer and a string, the string is casted to an integer and then the check is done against the two integers. Because of that rule, about just any random string is "equal" to zero. (To bypass this behavior, you should use strcmp, as it performs an explicit string comparison by casting anything passed to a string.)

    To make sure I’m dealing with an integer, I would use is_numeric first, then convert the string to an int, and verify that the stringified int corresponds to the input value.

    if (is_numeric($value) && strcmp((int)$value, $value) == 0)
    {
        // $value is an integer value represented as a string
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so this problem is baffling me. I have a custom class that all
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
simple problem baffling me... i have a function: function spitHTML() { $html = '
Hello StackOverflow community, I have run into a problem that quite frankly is baffling
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem I have never met before. I was clearing yesterday clients db
I have problem, when converting Windows application from .NET2.0 to .NET3.5. I do this
I have encountered a somewhat baffling problem with the simple task of filling an
I have problem with this method in NLog library: NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(object state) It consume too
I have problem with passing variables through views. But, first some code // i

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.