0

I am using PHP 7.0.2

At one place in the manual on integers, I saw the below statement :

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).

and at one place in the manual I saw below contradictory statement :

PHP does not support unsigned integers.

Due to these two sentences I got confused. At one place it's saying that an integer can optionally be preceded by a sign which means whenever I use any integer, preceding it with a sign(- or +) or not will be my choice.

And at the same time it's saying that PHP does not support unsigned integers.

So, does it say that whenever I use any integer without preceding it with a sign(+ or -) won't it be considered as a legal integer in PHP?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 3
    It may mean that you can't adjust the range by casting a number as unsigned. With MySQL, a signed range is -128 - 127, and an unsigned range is 0 - 255. With PHP, the ranges are finite and cannot be changed. – aynber Dec 29 '17 at 17:54
  • 2
    "[Unsigned integers](https://stackoverflow.com/tags/unsigned-integer/info)" refers to positive-only integers. PHP only supports "[signed integers](http://kias.dyndns.org/comath/13.html)" (= meaning `+` and `-` prefixes allowed, with the `+` being optional.) – mario Dec 29 '17 at 17:55
  • @mario : If such is the case then what's the sign preceding to the integer 0(zero) as 0(zero) is neither a positive nor a negative integer? – PHPLover Dec 29 '17 at 18:04
  • 1
    What does that have to do with your original question? Are we getting hung up on non-essential definitions by any chance? – mario Dec 29 '17 at 18:06
  • @mario : I want to make my doubt crystal-clear by understanding and knowing each and every small bit. – PHPLover Dec 29 '17 at 18:08
  • Possible duplicate of [Signed versus Unsigned Integers](//stackoverflow.com/q/247873) – mario Dec 29 '17 at 18:15
  • Think of the number as speed and direction along the X axis, 0 mens no motion . If numbers are to be put into two groups then all positive and negative will go in one group and zero alone will be in the second group. Also in PHP zero means FALSE and all positive and negative integers means TRUE in boolean context. – USER249 Dec 29 '17 at 18:47

3 Answers3

2

To clarify this, just as an integer and a float are different data types, an unsigned integer is actually a different data type to an integer. A standard integer is usually 32 bits (this can vary between different programming languages) and can be from approx -2 billion to +2 billion, while an unsigned integer can be from 0 to approx +4 billion. You can also have smaller and larger integers, which have lower and higher max values, and can also be unsigned, keeping the same number of distinct options but shifting to only positive.

This is relevant for things like database key fields, where an unsigned integer is ideal for autoincrement identity type fields, but will not be properly supported in php. It gets complicated, so if you're just using php don't stress over it and if you're using php with a database provider it's probably best to avoid using unsigned integers in your data as php will either break or start returning negative values different to the positive value that's actually stored.

It's the sort of annoyance that makes my eye twitch a little.

0

That simply means, that each integer is signed and you can't create unsigned integers. So if you want to use something bigger than PHP_INT_MAX, you have to use strings (or some library like bcmath) to come around this.

Philipp
  • 15,377
  • 4
  • 35
  • 52
  • If it means that each integer is signed and I can't create unsigned integers then what's the sign preceding to the integer 0(zero) as 0(zero) is neither a positive nor a negative integer? – PHPLover Dec 29 '17 at 18:04
  • 1
    PHP (and almost all other languages) store integers as two's complement. This means, that the most significant bit stores the sign. For zero all bit's are zero, means also the sign bit is zero - so zero is a positive number. Change the sign bit to 1 and leave everything else zero is equal to `PHP_INT_MIN` – Philipp Dec 29 '17 at 18:14
0

The + prefix to state a positive integer is optional, positive integers can be indicated by no sign or a +. Negative integers can only be indicated by a -.

Extending the examples from the integer guide.

$a = 123; // decimal number (123)
$a = +123; // decimal number (123)
$a = -123; // a negative decimal number (-123)
$a = 0123; // octal number (equivalent to 83 decimal)
$a = +0123; // octal number (equivalent to 83 decimal)
$a = -0123; // octal number (equivalent to -83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = +0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = -0x1A; // hexadecimal number (equivalent to -26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = +0b11111111; // binary number (equivalent to 255 decimal)
$a = -0b11111111; // binary number (equivalent to -255 decimal)
Kevin Stich
  • 773
  • 1
  • 8
  • 24