php / php/doc-en

Non-strict comparation of object and scalar value

Open
#1,773 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Category: Engine Status: Needs Triage
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description

The following code:

<?php

class Foo
{
    public $bar = 1;
}

$o = new Foo();
$s = '1';
$b = true;
$i = 1;
$f = 1.0;

var_dump($o == $s);
var_dump($o == $b);
var_dump($o == $i);
var_dump($o == $f);

Resulted in this output:

bool(false)
bool(true)

Notice: Object of class Foo could not be converted to int in /in/mreGe on line 16
bool(true)

Notice: Object of class Foo could not be converted to float in /in/mreGe on line 17
bool(true)

But I expected this output instead:

bool(true)
bool(true)
bool(true)
bool(true)

or

Notice: Object of class Foo could not be converted to string in /in/mreGe on line 14
bool(false)

Notice: Object of class Foo could not be converted to bool in /in/mreGe on line 15
bool(false)

Notice: Object of class Foo could not be converted to int in /in/mreGe on line 16
bool(false)

Notice: Object of class Foo could not be converted to float in /in/mreGe on line 17
bool(false)

I can't find any logic in current behavior:

  • Why I can compare non-stringable object with string or bool, but can't compare with int/float?
  • Why I can compare non-stringable object with string without notice?
  • If object can't be converted to int/float why comparation return true?
PHP Version

PHP 8.0.22

Operating System

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the comparison snippet on the reported PHP 8.0.22 version and inspect the PHP documentation covering object comparisons and type juggling. The work is done when the differing results and conversion notices are explained clearly in the relevant documentation, or the issue is narrowed to a PHP runtime bug.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.