// today is 03 Jan 2009
$datemonth = (int) date("md");
if($datemonth == 0103){
echo "Match";
} else {
echo "Not a match";
}
I am receiving Not a match as result. Isn't 0103 equal to 103 when compared as integer? In this situation I can use if($datemonth == 103) for the intended behaviour. But why the logic is failing? A leading zero does not have any value in an integer, right?
When you begin a numeric literal with a leading zero, it means the number is in octal (base 8). You probably meant it to be a decimal (base 10) number. 0103 in octal is equal to 67 in decimal. Drop the leading zero and your code should work. See PHP documentation for more details on numeric literals.
Related
First expression:
displays 123 octal, is not recognized, if recognized it should be 83
Second Expression:
displays 291, here hex recognized, if not recognized it should be 123
Third Expression:
Displays 0
$y = 0+"0123";
echo $y;
echo '<br>';
$x = 0+"0x123";
echo $x;
echo '<br>';
$x = 0+"0b10101";
echo $x; // This displays 0
output:
123
291
0
See the documentation at http://php.net/manual/en/language.types.string.php#language.types.string.conversion
The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.
There is no mention of non-decimal support.
PHP casting of strings to numbers is limited (and probably with good reason). If you want to use different base numbers you need to specify them as numbers e.g.
$y = 0123;
echo $y; // 83
$x = 0x123;
echo $x; // 291
$x = 0b10101;
echo $x; // 21
Notice that they are not quoted.
If you want to explicitly convert strings you need to do the following:
echo octdec("0123"); // 83
echo hexdec("0x123"); // 291
echo bindec("0b10101"); // 21
The prefixes (e.g. 0 or 0x or 0b) are allowed but optional when using these functions.
Note: PHP used to support implicit casting of hex strings as numbers but as of 7.0 it does 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
?>
Why this FALSE condition is TRUE?
<?php
if(111111111111111119 == 111111111111111118)
{
echo 'Condition is TRUE!';
}
?>
Quote from:
http://php.net/manual/en/language.operators.comparison.php
$a == $b is TRUE if $a is equal to $b after type juggling
If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically
So because your strings are both numeric they are being converted to numbers first.
Then on some architectures numbers are so big that are overflowing maximum integer size and you are getting equal results.
PHP DOC
Converting to string
An integer or float is converted to a string representing the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (4.1E+6).
Converting to integer
If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!
My Guess you are using a 32 bits system so therefore
var_dump(111111111111111119,111111111111111118);
var_dump(111111111111111119 === 111111111111111118); // would be true on 32bit
Output
float 1.1111111111111E+17
float 1.1111111111111E+17
true
Simple Solution
if(bcsub("111111111111111119", "111111111111111118") == "0")
{
// 32 bit true
var_dump("Am Free");
}
since it's converted into a numeric value
if('111111111111111119' == '111111111111111118')
{
echo 'Condition is TRUE!';
} else {
echo 'Condition is FALSE!';
}
// on 64 bit: condition is FALSE! (tested on my mac)
I'd assume that on 32bit machine it'd be true. Even when i remove the quotes on my mac it's shows false.
if('a111111111111111119' == 'a111111111111111118')
{
echo 'Condition is TRUE!';
} else {
echo 'Condition is FALSE!';
}
// condition is FALSE!
I'm trying to validate a number by it's length. This number has to have 4 digits so it passes the validation. The problem is when this number has 0's to it's left, like 0035.
Right now I'm at this:
echo (strlen ((string) 0025 ));
Which gives a total of 2, but I want this to count the 0's to it's left, so it gives me a total of 4.
Clearly the cast of the integer to string is not working, how can i do this?
You can't do that way, a left zero means the number is octal and not decimal, you can use sprintf() to do that.
Example:
echo strlen(sprintf("%04d", 25));
Live Test:
http://codepad.viper-7.com/VQr7Xz
Comment Answer:
I don't want to add the 0s to the number, i want to detect if the
number has 0s. If the number received is 25, it's not a valid number.
If it is 0025 it is valid. What i want is to validate only numbers
with 4 digits. – Cláudio Ribeiro
Cláudio, numbers have infinite left zeros, although a user has explicitly type 2 or 3 left zeros there are more hidden left zeros, it's a math basic, this is why it's impossible to know how many left zeros the user has typed if you receive an integer variable. If the variable has a constant size and you want to know how many left zeros it has you can do this:
<?php
$int = 25;
echo 4 - strlen($int);
Live test: http://codepad.viper-7.com/fT2jSn
But if you the variable has variable length it must be a string type instead of a numeric type.
An example where the variable received is a string:
<?php
$strs = array("0025","000035","01","2");
foreach($strs as $str)
{
preg_match("/^0+/", $str, $matches);
echo strlen(#$matches[0]);
echo "<br>";
}
Live Test: http://codepad.viper-7.com/BTRTgR
That should work:
$str = "0025";
if( is_numeric($str) && strlen($str) == 4)
{
echo "pass";
}
If it's a number, not a string, the number doesn't have digits. It has a value. You can format that value into a string with 4 digits which is left padded with 0s. But to validate whether a number has 4 digits is nonsense, since the number value has no formatting. The value only becomes "4 digits" when you format it as base 10 number. Until then the value is a value which can be expressed in a multitude of bases and has a different number of "digits" in all of them.
You either want to format the number to a 0-padded 4 digit string, or you want to check whether the value is between 0 and 9999 (or 1000 and 9999 if it has to be exactly "4 digits").
if (0 <= $num && $num <= 9999) {
$numStr = sprintf('%04d', $num);
} else {
trigger_error('Number out of range');
}
$y = 013;
echo $y + 5; //this result in 16
I can not figure it out how its ans is 16? Can any one help?
because 013 isn't decimal (base 10). it's octal (base 8). the value in decimal is:
(0 * 8^2) + (1 * 8^1) + (3 * 8^0) = 0 + 8 + 3 = 11
which gives the correct (though unexpected, at least by you) result of 16 when added to 5.
moral of the story: don't prepend a number literal with 0 unless you know what it means
number with leading zero is octal number
like
$a = 0123; // octal number (equivalent to 83 decimal
Integers can be specified in decimal
(base 10), hexadecimal (base 16), or
octal (base 8) notation, optionally
preceded by a sign (- or +).
To use octal notation, precede the
number with a 0 (zero). To use
hexadecimal notation precede the
number with 0x.
$y = 013;
echo $y + 5;
013 is octal number all php integer numbers are octal .
show this link. first.
http://www.ascii.cl/conversion.htm