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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:04:13+00:00 2026-06-07T22:04:13+00:00

How can this happen? var_dump(0==some string); // yields true, why? switch(0) { case a:

  • 0

How can this happen?

var_dump(0=="some string"); // yields true, why?

switch(0) {
  case "a":
    echo "a"; // <-- we get here, why?
    break;  

  case "b":
    echo "b";
    break;   

  default:
    echo "def";
    break;
} 

according to this

0=="some string"
0==(int)"some string"
0==0
true

this also would logical:

        0=="some string"
(string)0=="some string"
      "0"=="some string"
       false
  • 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-07T22:04:14+00:00Added an answer on June 7, 2026 at 10:04 pm

    Too Long; Didn’t Read

    To sum this up in this rather long post; an implicit conversion from one type to another takes place, if you don’t want this to happen use the more strict === or an explicit cast.

    Examples including both can be found further down in this post..


    Further reading about type-juggling can be found in the relevant section of the manual, located here.


    Will the world will end, is the PHP interpreter is lying to me?

    Not really, even though this might be unexpected at first glance it is actually a feature (type-juggling) of the language and it’s often very usable in other situations.

    When comparing objects of different types the PHP interpreter needs to find a common ground if they are of different types, naturally it will implicitly try to convert one to the other.

    In this case it will try to convert the string “string” into a numeral value.

    The below line is equivalent to what you have in your question:

    if (0 == intval ("string"))
    

    Hmm, wait.. erhm, what!?

    Since a conversion from “string” to a numeric value isn’t really possible the implicit conversion will yield 0 (as specified by the language).

    It’s the string which will be converted to an int since that is what the PHP
    committee decided upon.

    This is mainly because an implicit conversion that way will aid when doing math operations.

    And it’s much more common to expect that behavior than the other way
    around.

    We know that comparing 0 to 0 should compare true, and this is exactly what is happening. The below is equivalent to what you have in your question, though it’s more verbose to explain things further.

    function equal_ ($lhs, $rhs) {
      if (gettype ($lhs) == 'integer')
        $rhs = intval($rhs);
    
      /* ... */
    
      return $lhs === $rhs;
    }
    
    if (equal_ (0, "string")) {
    
    }
    

    But hey.. HEY, HOLD UP!

    “When I use my switch I’m not even calling any comparison-operator, what is going on with that example?”

    You don’t, but the internals of the interpreter will when executing your switch-statement.

    To get around this problem you will need to explicitly cast either the needle you are searching for, or the values used as labels so that they are of the same type.

    Your previous snippet could be written as the below to enable more strict comparison.

    switch((string)0) {
      case "a":
        echo "a";
        break;  
    
      case "b":
        echo "b";
        break;   
    
      default:
        echo "def";
        break;
    } 
    

    What if I don’t want to use this “feature”, what should I do?

    Since PHP4 there is a more strict comparison operator; ===.

    This will not allow an implicit conversion of one of the operands, instead it will first check to see if the operands are of the same type – if not; it will return false.

    Only if they are of the same type will it actually compare the two, the below function is in many ways equivalent of using ===.

    function strict_equal_ ($lhs, $rhs) {
      if (gettype ($lhs) != gettype ($rhs))
        return false;
    
      return $lhs == $rhs;
    }
    
    if (strict_equal_ (0, "string"))
      echo "True";
    else
      echo "False";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to get this working, what should happen is when the user
How can I make this happen: var name = otherObject.name; //string var o =
Can anyone tell me how to make this happen? It looks like a setting.
Basically, I'm having this happen: https://i.stack.imgur.com/el5zH.png I can scroll over to farther than I
I can't quite get this right.. my function looks like: create or replace function
How can I turn this behavoir off? It happens when you enter a close
I have this code ,now can anyone reply s what happens to the lock
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
Can this be done quickly in jQuery before I start looping through them? Or
Can this be done? It seems like this should be possible. In general, 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.