I have this code:
$mob_numbers= array(02345674, 12345675, 22345676, 32345677);
echo ($mob_numbers[0]);
I wanted to print the first element of array
but the output of that is:
641980
Why does this happen?
I have this code:
$mob_numbers= array(02345674, 12345675, 22345676, 32345677);
echo ($mob_numbers[0]);
I wanted to print the first element of array
but the output of that is:
641980
Why does this happen?
As said by @ lost The php manual > mentions this behavior
$ a = 0123; // octal number (equivalent to 83 in decimal)
So the best way to print the value of integers that have a 0 on the left is:
$mob_numbers = array("02345674", "12345675", "22345676", "32345677");
echo (int)$mob_numbers[0]; //Retorno 2345674
(int)
converts the string to integer, this ensures that the number stays as an integer, as you can see in the article #
You can achieve the same result without converting the values from your array
to string
, if you use the sprintf
to save the formatted value you want to get.
In this case, the option '%o'
of the function causes "The argument is treated as an integer, and shown as an octal number" (excerpt taken from the PHP Manual).
If you want to get the zeros, you have to use '%08o'
. This will return the value filled with leading zeros if there are less than 8
numbers in its integer.
Example:
$mob_numbers= array(02345674, 12345675, 22345676, 32345677);
echo sprintf('%08o', $mob_numbers[0]); // preenche com zero quando não há 8 números
echo sprintf('%o', $mob_numbers[0]); // formata o valor sem o zero
You can work with these numbers as string
.
Just add quotes:
$mob_numbers= array("02345674", "12345675", "22345676", "32345677");
echo ($mob_numbers[0]);