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

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

Related

PHP write a number out of an array

I have a PHP problem.
I need to write a number from a sets of digits 0-9. Each set has 10 digits, each digit once.
I need to count the number of sets that I have to use to write the number.
For example number 10 is written from one set, but number 300 uses 2 sets because it has two zeros.
But, the problem is that 6 and 9 are considered the same. They can be rotated by 180 degrees.
Number 266 will be using one set, 369 also is using one set, but 5666 is using 2 sets.
I would be very grateful if you could somehow help me.
Here is how I have started and stuck up, have no more clue how to loop through it. Tried many things, nothing successful.
<?php
function countSet($num) {
$array = str_split($num);
$statarr = [0,1,2,3,4,5,6,7,8,9];
$a1 = $array; $a2 = $statarr;
$result = array_intersect($a1,$a2);
$count = array_count_values($result); }
?>
If you just need to know how many sets you need for a number, you can solve by counting the numbers. For the case of the 9, just replace every 9 by a 6 and divide the number of 6 by two. Something like this (sorry if there's any syntax error, I'm on mobile):
function countSet($input) {
// Convert the input into a string and replace every 9 by a 6. Then convert it to array
$numbers = str_split(str_replace("9", "6", strval($input)));
// Count occurrences for each number
$usedNumbers = array_count_values($numbers);
// If we have a 6, must divide it by 2 (6 represents 6 and 9
if (array_key_exists("6", $usedNumbers)) {
$usedNumbers["6"] = ceil($usedNumbers["6"] / 2);
}
// Now we just need to know the max number of occurrences for a single number as our result.
return max($usedNumbers);
}
See an online demo here

Octal Numbers - what I might be missing to check if the input number recieved is of an Octal Format or Not?

Just learnt the basics of converting a Decimal Number to an Octal Number. Now for the reverse, I seem to have got the fact that any number whose last digit ending is either 8 or 9, cannot be an octal number.
But, is there anything else that I would need to check or do to see if an input number is an Octal Number or not (apart from checking 8 or 9 in the last digit)?. - [basically, enquiring if I am missing a certain process]
Below is my code in PHP:
<?php
$iOctal = 1423;
echo "What is the Decimal Value of the octal number $iOctal?"."<br/>";
$rg_Decimal = str_split($iOctal);
//print_r($rg_Decimal);
if (end($rg_Decimal) == 8 || end($rg_Decimal) == 9){
echo "<b>Error:- </b>Unable to process your request as the input number format is not of the Octal Number Format. Please try again...";
}
if ($iOctal < 8 && $iOctal >= 0){
echo "The Decimal Value of the octal number $iOctal is $iOctal.";
}
else{
$iE = count($rg_Decimal);
--$iE;
$iDecimal = 0;
for ($iA = 0; $iA < sizeof($rg_Decimal); ++$iA){
$iDecimal += $rg_Decimal[$iA] * bcpow(8,$iE);
--$iE;
}
echo "The Decimal Value of the octal number $iOctal is <b>$iDecimal</b>";
}
?>
It just so happened that during the testing, I had used an online resource. When I had given a particular number, it said that the number format was not octal. But the number did not have an 8 or 9 ending.
Looking forward to your kind support.
You can use the builtin function octdect($number) from php.
An example from http://php.net/manual/en/function.octdec.php , with the same question:
<?php
function is_octal($x) {
return decoct(octdec($x)) == $x;
}
echo is_octal(077); // true
echo is_octal(195); // false
?>

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

Minus value using php [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);

How can I set default input value to 0.00 in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
Here you go :)
echo number_format($var,2);
If you want it to print specific no. of decimal points, use number_format.
$float_var = number_format($var, 2);
$var = number_format($number, 2, '.', '');
This forces 2 points after the decimal, sets the decimal as a period. You can also forego the last two as it defaults to it;
Note: The third value is your decimal separator, the fourth value is the thousandths separator.
$var = number_format($number, 2);
For direct output:
printf('%0.2f',$var);
Output into variable:
$outVar = sprintf('%0.2f',$var);
This statemant casts $var type to float and prints with 2 decimal signs
maybe you should check it first if the value is not set
if(!isset($variableName))
{
// then set
$variableName = "0.00"; // => string
//or like this
$variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;
result
0.00
if you are trying to format the value i sugest you to use meioMask pluging.
So you define your field as number and the pluging do the trick, even if you set "0" for the value

Categories