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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:14:49+00:00 2026-06-16T04:14:49+00:00

I am way confused with a comparison error. The way I coded this comparison,

  • 0

I am way confused with a comparison “error”. The way I coded this comparison, I want to make sure that if the user inputs any value other than 0 or 1 (Or no value), the result should be 0:

session_start();
function test( $status = 0 ) {
        if( !isset($_SESSION['status']) ) { $_SESSION['status'] = 0; }
        else {
            switch( $status ) {
                case 0: $_SESSION['status'] = $status;
                break;

                case 1: $_SESSION['status'] = $status;
                break;

                default:
                $_SESSION['status'] = 0; 
                $status = 0;
            }
        }
        echo 'Variable value: ' . $status;
        echo ' | Session value: ' . $_SESSION['status'] . "<br/>";
}
test();
test(0);
test(1);
test(999);
test('ready');

HOWEVER, it breaks at test('ready'); BECAUSE it outputs Variable value: ready | Session value: ready RATHER THAN Variable value: 0 | Session value: 0. It should continue to work well (and go for case o:) even if it is comparing numbers against a string.

BTW: The result is the same even if I replace switch for if( $status ===

=== EDIT: 12/19/2012 ===
Thanks to @bryan-b and @jatochnietdan comments and answers: -Because a string (when compaired with numbers [if( 0 == ‘string’)] ) is compared as 0

That helped me figure out the problem and learned that, unlike in other languages, rather than automatically returning false in a comparison, since they are of different data-types; php compares string variables as if their value is 0 when compared against numbers.THAT’S SOMETHING TO WATCH OUT FOR.

This is the corrected (working) code:

session_start();
function test( $status = 0 ) {
        if( !isset($_SESSION['status']) ) { $_SESSION['status'] = 0; }
        else {
            switch( $status ) {
                case 1: $_SESSION['status'] = $status;
                break;

                default:
                $_SESSION['status'] = 0; 
                $status = 0;
            }
        }
        echo 'Variable value: ' . $status;
        echo ' | Session value: ' . $_SESSION['status'] . "<br/>";
}
test();
test(0);
test(1);
test(999);
test('ready');

Thank you @bryan-b and @jatochnietdan!!!

PS. I wish I could vote 2 answers.

  • 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-16T04:14:51+00:00Added an answer on June 16, 2026 at 4:14 am

    I think the problem here is that php is casting the string “ready” to an integer, so the case comparison works. From the php docs (http://php.net/manual/en/function.intval.php):

    Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.

    I’ve added an echo into the (working) function showing what php is doing when casting the string to an int for the switch comparisons. You need to be checking the exact value and type of the passed in status.

    function test($status = 0){
        if(!isset($_SESSION['status']) || $status !== 1){ 
            $_SESSION['status'] = 0; 
        }
        else {
            $_SESSION['status'] = 1;
        }
    
        echo 'Variable value: ' . $status;
        echo ' | Variable value (int): ' . (int)$status;
        echo ' | Session value: ' . $_SESSION['status'] . "<br/>";
    }
    test();
    test(0);
    test(1);
    test(999);
    test('ready');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't believe I'm still confused about this but, any way, lets finally nail
I already asked this question, but in a way that confused people. So what
I'm confused about on how to use this array in a way that is
What is the best way(s) to keep the User from getting confused by this
This looks simple but I am confused: The way I create a vector of
I'm confused. Consider this code working the way I expect: >>> foo = u'Émilie
confused how to achieve this maybe with FULL OUTER or a UNION. I want
I'm confused if this is even the right way to do. Please correct me.
I'm a bit confused. Is there any simple way to set proper audio devices
I'm a bit confused why I can't initialize a getter this way: @synthesize stack

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.