php integer value is not showing as aspected [duplicate] - php

This question already has answers here:
what is 0050 and why echo 0050 result 40 [duplicate]
(2 answers)
Closed 9 years ago.
While coding I got an unexpected result.
I'm unsure how to ask this question so I'll put in my code and result:
$variable = 012;
$variable2 = 12;
$variable3 = '012';
When I echo out the variables:
Expected result:
$variable: 12
$variable2: 12
$variable3: 012
Result:
$variable: 10
$variable2: 12
$variable3: 012
What is happening here?

See the documentation of the integers of php
Then you see the following:
To use octal notation, precede the number with a 0
So octal 12 is decimal 10
Little bit more info of the docs
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)

$variable is Octal
$variable2 is int
$variable3 is string

Related

why (int)08 is equal to 0, not 8? [duplicate]

This question already has an answer here:
Strange behaviour with numbers that have a leading zero [duplicate]
(1 answer)
Closed 5 years ago.
echo (int)01; //1
echo (int)02; //2
echo (int)03; //3
echo (int)04; //4
echo (int)05; //5
echo (int)06; //6
echo (int)07; //7
echo (int)08; //0
echo (int)09; //0
echo (int)010; //8
echo (int)011; //9
echo (int)012; //10
echo (int)013; //11
(int) was doing right from 01 to 07. But after that it goes wrong. What's the reason of it??
Perhaps 08 is expected to be octal number, like 0x is hexadecimal.
If a number starts with 0 it is consider an octal number
and since these numbers range from 0 to 7 only. You get a 0 since 8%8 = 0
Reference: http://php.net/manual/en/language.types.integer.php
A side note on number systems
Have you wondered why the next number to 9 is 10? and why in the binary system the sequence is 0, 1, 10, 11... ? And why the octal system allows only values from 0 to 7?
It is because number systems usually increment values based on modulo logic on the base.
For example take the binary number system. Since it is base 2, it can only contain values 0, 1 since 0%2 = 0, 1%2 = 1, but 2%2 is again 0
So whenever a greater number comes, say 3. Its value in binary is (increment by one in the preceding place) (put the modulo in the existing place)
So the value of 3 in the binary system is (0+1) (3%2) = 11
Though this is not the exact logic, just putting it here for a beginner reference

PHP substring not working for intergers [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);

How to add zeros to the left of a number [duplicate]

This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
I have a piece of code for converting a Decimal number into base 3
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo $base_three;
So the number that it echos is 101 and it has 3 digits.
but I what it to echos is 000101 so that it has 6 digits.
Converting Decimal into base 3 with always 6 digits even though it has only 3 or 4 useful digits, is my goal! how can I solve it ?
try this
echo str_pad($base_three, 6, "0", STR_PAD_LEFT);
You can use sprintf to ensure that it always has a total of 6 digits, with leading zeroes:
$base_three = 101;
$padded = sprintf("%06s", $base_three);
echo $padded;
Convert to a string and pad with 0's.
$test = str_pad($base_three, 6, '0', STR_PAD_LEFT);
echo $test;
http://php.net/manual/en/function.str-pad.php
You can use sprintf to make sure you always output 6 digits, whatever number you have:
$number = 010;
sprintf("%06d", $number);
so the complete piece of code would be:
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo sprintf("%06d", $base_three);
or
printf("%06d", $base_three);
printf formats the variable and echos it, sprintf() doesn't echo but returns it
(s)printf can do a lot more, see http://www.php.net/manual/en/function.sprintf.php

Unexpected bitwise operation result [duplicate]

This question already has answers here:
What's the function of the ~ bitwise operator (Tilde) [duplicate]
(3 answers)
Closed 9 years ago.
Consider:
php > $a = 12; // 1100
php > echo ~$a;
-13
I would expect the inverse of 1100 to be either 0011 (direct) or 11110011 (an entire byte). That would give a result to either 3 or 243. Whence cometh -13?
Again, for good measure, another unexpected result of the same type and explanation:
php > $b = 6; // 0110
php > echo ~$b;
-7
Why -7?
Look at this code:
<?php
$val = 6;
print "$val = ".decbin($val);
print "\n";
$val = ~$val;
print "$val = ".decbin($val);
It prints
6 = 110
-7 = 11111111111111111111111111111001
At first you have 110. As my php uses 32 bits, after inverting all the bits, we get this huge number. As the 1-st bit is 1, php interprets it as a negative value, stored, using two's-complement representation. To find out, the modulus of the negative value, stored in this notation, we
invert the digits:
110
add one to the result:
111
which gives us 7
So, the value is -7
http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
Why -7?
6 is 00000000000000000000000000000110, so ~6 is ~00000000000000000000000000000110, and that's equal to 11111111111111111111111111111001. Because a signed datatype is used, the first bit indicates whether the number is positive or negative (positive = 0 and negative = 1). Because it is Two's complement, you should convert the binary number to decimal using this way:
Invert the binary number. You get 00000000000000000000000000000110
Convert 00000000000000000000000000000110 (a positive binary number) to a decimal number. You get 6
Add 6 up with one: you get 7
Make it negative: you get -7

Unfamiliar usage of '%' in PHP script [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I found the following line of code in a PHP script and have never seen anything like it before:
$a = ($ba%10)
What does this do?
Its is PHP's Arithmetic Operators
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
Click PHP.NET for more information!
It tells you the remainder of a division calculation. So 25%8 would be 1. If $ba = 101 then $ba%10 would equal 1.
% is the modulus operator, it gives you the remainder of integer division.
e.g. 87 % 10 = 7

Categories