rfc:comparison_inconsistency
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:comparison_inconsistency [2014/01/11 02:53] – yohgaki | rfc:comparison_inconsistency [2014/02/05 03:03] (current) – removed yohgaki | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Existing comparison and conversion behaviors to discuss/ | ||
- | * Version: 0.1 | ||
- | * Date: 2014-01-08 | ||
- | * Author: Yasuo Ohgaki < | ||
- | * Status: Draft (or Under Discussion or Accepted or Declined) | ||
- | * First Published at: http:// | ||
- | |||
- | This RFC is to discuss comparison and conversion inconsistencies in PHP. | ||
- | |||
- | ===== Introduction ===== | ||
- | |||
- | There are number of in comparison and conversion inconsistencies. | ||
- | |||
- | For example, | ||
- | * https:// | ||
- | |||
- | There are number issues of like this. | ||
- | |||
- | Purpose of this RFC is fix inconsistency where it's feasible, otherwise document then fully if it's not documented already. | ||
- | |||
- | ===== Inconsistency ===== | ||
- | ==== Conversion/ | ||
- | |||
- | Type juggling only works for INTEGER or HEX like strings. | ||
- | |||
- | === HEX === | ||
- | Code | ||
- | var_dump(0x0A); | ||
- | var_dump(" | ||
- | var_dump((int)" | ||
- | var_dump((float)" | ||
- | var_dump(intval(" | ||
- | var_dump(floatval(" | ||
- | |||
- | Output | ||
- | int(10) | ||
- | string(4) " | ||
- | int(0) | ||
- | float(0) | ||
- | int(0) | ||
- | float(0) | ||
- | |||
- | Code | ||
- | if (0x0A == ' | ||
- | echo "0x0A == ' | ||
- | } | ||
- | if (0x0A == " | ||
- | echo '0x0A == " | ||
- | } | ||
- | |||
- | Output | ||
- | 0x0A == ' | ||
- | 0x0A == " | ||
- | |||
- | |||
- | === Octal === | ||
- | |||
- | Code | ||
- | var_dump(010); | ||
- | var_dump(" | ||
- | var_dump((int)" | ||
- | var_dump((float)" | ||
- | var_dump(intval(" | ||
- | var_dump(floatval(" | ||
- | |||
- | Output | ||
- | int(8) | ||
- | string(3) " | ||
- | int(10) | ||
- | float(10) | ||
- | int(10) | ||
- | float(10) | ||
- | |||
- | CODE | ||
- | if (010 == ' | ||
- | echo "010 == ' | ||
- | } | ||
- | if (010 == " | ||
- | echo '010 == " | ||
- | } | ||
- | |||
- | OUTPUT | ||
- | (NONE) | ||
- | |||
- | |||
- | === BINARY === | ||
- | |||
- | Code | ||
- | var_dump(0b110); | ||
- | var_dump(" | ||
- | var_dump((int)" | ||
- | var_dump((float)" | ||
- | var_dump(intval(" | ||
- | var_dump(floatval(" | ||
- | |||
- | Output | ||
- | int(6) | ||
- | string(5) " | ||
- | int(0) | ||
- | float(0) | ||
- | int(0) | ||
- | float(0) | ||
- | |||
- | CODE | ||
- | if (0b010 == ' | ||
- | echo "0b010 == ' | ||
- | } | ||
- | if (0b010 == " | ||
- | echo '010 == " | ||
- | } | ||
- | |||
- | OUTPUT | ||
- | (NONE) | ||
- | |||
- | === Array of Chars === | ||
- | |||
- | Null string is not handled as ARRAY. | ||
- | |||
- | https:// | ||
- | |||
- | Test script: | ||
- | $a = ''; | ||
- | $a[10] = ' | ||
- | echo $a; // " | ||
- | | ||
- | $b = ' '; // non empty string | ||
- | $b[10] = ' | ||
- | echo $b; // " | ||
- | |||
- | Expected result: | ||
- | " | ||
- | " | ||
- | |||
- | Actual result: | ||
- | " | ||
- | " | ||
- | |||
- | ==== String Integer conversion ==== | ||
- | |||
- | PHP converts " | ||
- | |||
- | <?php | ||
- | | ||
- | // this is the problem, which we'd expect | ||
- | // to return false, but which returns true: | ||
- | echo (2 == ' | ||
- | | ||
- | // this is probably what's happening: | ||
- | echo (2 == intval(' | ||
- | | ||
- | // this is what probably should happen: | ||
- | echo (strval(2) != ' | ||
- | | ||
- | ?> | ||
- | |||
- | https:// | ||
- | |||
- | ==== NAN/INF of float ==== | ||
- | |||
- | NAN/INF issue. | ||
- | |||
- | $f = NAN; | ||
- | var_dump(++$f); | ||
- | var_dump((float) NAN); // float NAN | ||
- | var_dump((int) NAN); // int -2147483648 -> what? | ||
- | var_dump((bool) NAN); // bool true -> makes sense | ||
- | | ||
- | $f = INF; | ||
- | var_dump(++$f); | ||
- | var_dump((float) INF); // float INF | ||
- | var_dump((int) INF); // int 0 -> what? | ||
- | var_dump((bool) INF); // bool true -> so why int 0? | ||
- | var_dump((int) (bool) INF); // int 1 | ||
- | |||
- | E_WARNING for these invalid/ | ||
- | |||
- | This could be mitigated by GMP float support. | ||
- | |||
- | |||
- | ==== Object Array conversion of numeric property/ | ||
- | |||
- | Object/ | ||
- | https:// | ||
- | |||
- | $ php -v | ||
- | PHP 5.5.7 (cli) (built: Dec 11 2013 07: | ||
- | Copyright (c) 1997-2013 The PHP Group | ||
- | Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies | ||
- | | ||
- | $ php -r '$obj = new StdClass; $obj-> | ||
- | object(stdClass)# | ||
- | [" | ||
- | int(234) | ||
- | } | ||
- | int(567) | ||
- | |||
- | |||
- | Notice: Undefined offset: 12 in Command line code on line 1 | ||
- | array(1) { | ||
- | [" | ||
- | int(234) | ||
- | } | ||
- | NULL <= SHOULD BE int(234) | ||
- | |||
- | |||
- | |||
- | |||
- | ===== Function/ | ||
- | |||
- | ==== is_numeric ==== | ||
- | |||
- | https:// | ||
- | |||
- | |||
- | ==== base_convert ==== | ||
- | |||
- | https:// | ||
- | |||
- | |||
- | ==== min() function ==== | ||
- | |||
- | https:// | ||
- | |||
- | This is not a bug. If one of operand is BOOL(or NULL), both operands are converted to BOOL and evaluated as BOOL. It may be good idea that document this behavior in min() manual. | ||
- | |||
- | ==== Return value of wrong internal function/ | ||
- | |||
- | If not all, almost all functions return NULL when required function parameter is missing or wrong type. However, almost all functions return FALSE when they have errors. | ||
- | |||
- | The manual has document for this behavior | ||
- | http:// | ||
- | |||
- | Note: If the parameters given to a function are not what it expects, such as passing an array | ||
- | where a string is expected, the return value of the function is undefined. In this case it will | ||
- | likely return NULL but this is just a convention, and cannot be relied upon. | ||
- | |||
- | This behavior could be cause of bug in scripts. For instance, | ||
- | |||
- | if (FALSE === some_func($wrong_parameter)) { | ||
- | // Error happend! | ||
- | } else { | ||
- | // OK to go | ||
- | } | ||
- | |||
- | Users should not rely on return value as it may return NULL for wrong parameters. Users should rely on error/ | ||
- | |||
- | It may be good to add use of error/ | ||
- | http:// | ||
- | |||
- | There are bug reports that complain return value inconsistency. The document could be improved with more explanations. | ||
- | |||
- | **Related Bug Reports** | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | |||
- | Bug reports are not verified carefully. Removing wrong one, adding proper one is appreciated. | ||
- | |||
- | ===== Developer Guideline ===== | ||
- | |||
- | * Internal function/ | ||
- | * Internal function/ | ||
- | * Internal function/ | ||
- | |||
- | ===== User Guideline ===== | ||
- | |||
- | * User should not rely return value only for failure condition, but should rely error/ | ||
- | |||
- | ===== Proposal ===== | ||
- | |||
- | Not yet. | ||
- | |||
- | ===== Backward Incompatible Changes ===== | ||
- | |||
- | Not yet. | ||
- | |||
- | ===== Proposed PHP Version(s) ===== | ||
- | |||
- | PHP 6.0 probably. | ||
- | |||
- | ===== Impact to Existing Extensions ===== | ||
- | |||
- | Not yet. | ||
- | |||
- | ===== New Constants ===== | ||
- | |||
- | Not yet. | ||
- | |||
- | ===== php.ini Defaults ===== | ||
- | |||
- | If there are any php.ini settings then list: | ||
- | * hardcoded default values | ||
- | * php.ini-development values | ||
- | * php.ini-production values | ||
- | |||
- | Not yet. | ||
- | |||
- | ===== Open Issues ===== | ||
- | |||
- | Make sure there are no open issues when the vote starts! | ||
- | |||
- | ===== Unaffected PHP Functionality ===== | ||
- | |||
- | List existing areas/ | ||
- | |||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | ||
- | |||
- | ===== Future Scope ===== | ||
- | |||
- | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | ||
- | |||
- | ===== Proposed Voting Choices ===== | ||
- | |||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | ||
- | |||
- | ===== Patches and Tests ===== | ||
- | |||
- | Links to any external patches and tests go here. | ||
- | |||
- | If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. | ||
- | |||
- | Make it clear if the patch is intended to be the final patch, or is just a prototype. | ||
- | |||
- | ===== Implementation ===== | ||
- | |||
- | After the project is implemented, | ||
- | - the version(s) it was merged to | ||
- | - a link to the git commit(s) | ||
- | - a link to the PHP manual entry for the feature | ||
- | |||
- | ===== References ===== | ||
- | |||
- | Links to external references, discussions or RFCs | ||
- | |||
- | ===== Rejected Features ===== | ||
- | |||
- | Keep this updated with features that were discussed on the mail lists. | ||
- | |||
- | ===== ChangeLog ===== | ||
- | |||
- | * 2013/10/31 - Initial version. |
rfc/comparison_inconsistency.1389408789.txt.gz · Last modified: 2017/09/22 13:28 (external edit)