This is an old revision of the document!

PHP RFC: base_convert changes


the base_convert family of functions(base_convert, bin2hex, hex2bin etc) are very accepting with their input arguments, you can pass any string to to them and they give a best effort of converting it.

For example base_convert(“hello world”, 16, 10); will return 237 with no warnings. What this does internally is base_convert(“ed”, 16, 10);

Also negative numbers simply do not work, eg base_convert(“-ff”, 16, 10); will return 255.


II propose two changes to the base_convert family of functions.

Error on truncation

When passed arguments that base_convert will ignore, also give a warning to the user. There is an exception in place for the second char, if base is 16 x is allowed eg 0xff, base 2 allows b and base 8 allows o. This can be raised to a full exception for a later PHP version (eg PHP 8)

Allow negative arguments

Negative numbers should be allowed to be passed to the base_convert family of functions. for example base_convert('-ff', 16', 10); should return -255. Currently users need to make an exception in this case

Backward Incompatible Changes

Error on truncation

No BC breaks for a warning, in the case of an exception there will be a BC break - Would suggest PHP 8 for this.

Allow negative arguments

base_convert currently in the backend has a zend_ulong as its representation. We would need a zend_long. This will change the behavior of numbers in the range 9223372036854775807 - 18446744073709551615 but will allow negative numbers.

This currently breaks a fair amount of unit tests that I will need to update if this change is accepted. The unit tests are around extreme values

Proposed PHP Version(s)

PHP 7.4 for warnings PHP 8.0 for negative arguments and exceptions

RFC Impact



To Existing Extensions


To Opcache


New Constants


php.ini Defaults

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

Only the base_convert family of functions will be changed

Future Scope


Proposed Voting Choices

Include these so readers know where you are heading and can discuss the proposed voting options.

Patches and Tests


After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)


Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/base_convert_improvements.1557952935.txt.gz · Last modified: 2019/05/15 20:42 by exussum