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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:27+00:00 2026-05-24T02:39:27+00:00

I have the following piece of code: $item[‘price’] = 0; /* Code to get

  • 0

I have the following piece of code:

$item['price'] = 0;
/* Code to get item information goes in here */
if($item['price'] == 'e') {
    $item['price'] = -1;
}

It is intended to initialize the item price to 0 and then get information about it. If the price is informed as ‘e’ it means an exchange instead of a sell, which is stored in a database as a negative number.

There is also the possibility to leave the price as 0, either because the item is a bonus or because the price will be set in a later moment.

But, whenever the price is not set, which leaves it with the initial value of 0, the if loop indicated above evaluates as true and the price is set to -1. That is, it considers 0 as equal to ‘e’.

How can this be explained?

When the price is provided as 0 (after initialization), the behavior is erratic: sometimes the if evaluates as true, sometimes it evaluates as 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-05-24T02:39:28+00:00Added an answer on May 24, 2026 at 2:39 am

    You are doing == which sorts out the types for you.

    0 is an int, so in this case it is going to cast 'e' to an int. Which is not parsable as one and will become 0. A string '0e' would become 0 and would match!

    Use ===

    From PHP.net:

    Comparisons between strings and numbers using == and other non-strict
    comparison operators currently work by casting the string to a number,
    and subsequently performing a comparison on integers or floats. This
    results in many surprising comparison results, the most notable of
    which is that 0 == "foobar" returns true.

    However this behavior was changed in PHP 8.0:

    When comparing to a numeric string, PHP 8 uses a number comparison.
    Otherwise, it converts the number to a string and uses a string
    comparison.

    PHP 7

    0 == 'foobar' // true
    0 == '' // true
    4 == '4e' // true (4e is cast as a number and becomes 4)
    

    PHP 8 converts numbers to strings before making comparisons

    0 == 'foobar' // false
    0 == '' // false
    4 == '4e' // false ('4e' is considered non-numeric therefore 4 is cast as a string and becomes '4')
    

    This is a major change therefore it was implemented in a new major PHP version. This change breaks backward compatibility in scripts that depend on the old behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following piece of code in my jsp file which getSession information: <jsp:useBean
I have the following piece of code: self.ignore_dir_extensions = ['xcodeproj'] item = repr(extension.split('/')[0]) print
Here's what I want to do. I have the following piece of code: Private
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have the following piece of code pattern: void M1(string s, string v) {
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following piece of code in Visual C++ 2005 : : class
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
I have the following piece of code taken from the PHP manual on the
I have the following piece of code in C++. int arr[] = {1, 2,

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.