strange behavior of php in implicit convert - php

strangely the following code return true!
if ('1'==1 && '014'==016)
echo 1;
and output is
1
can anyone tell me why '014' is equal to 016? and how to solve this problem? and if it is possible avoid to do a explicit convert because the data type all the time is vary.
i'm using PHP Version 5.3.8-ZS5.5.0

In PHP, using an integer value with a leading zero leads PHP to assume it's an octal number. Octal 016 is equal to decimal 14.
Using a string such as '014' is implicitly cast to a decimal 14 when used in a comparison with another integer.
See http://php.net/manual/language.types.integer.php

you need to convert string values to integer first, like this:
if(intval('1')==1 && intval('014')==16)
echo 1;
see intval()

Try this one.
<?php
if (ltrim((int)'1',0) == ltrim(1,0) && ltrim((int)'014',0)== ltrim('016', '0'))
echo 1;

Related

inval return wrong number if the number start with zero

I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.

Why does this happen in php when we multiple string by number it always gives zero(0)?

Suppose we have a string $str = "a"; and number $num = 2;
$str = 'a';
$num = 2;
echo $str*$num;
Output:
0
When performing arithmetic operations on a string operand, PHP will try to convert the string to a number.
It does this by looking for digits at the beginning of the string and will try to convert them into a value. If there are no digits, the value will be zero.
(There's an edge case for strings containing e or E (scientific notation), but that's not relevant here.)
Good Question.
Same i did ask to my teacher when i was in collage,
The answer is.
String * int= infinity; //According to scientific calculator answer is infinity.
but we need to continue our so program it provide 0.
it is made by code by default answer.
Simply said the string will be converted to an integer with a value of 0. This will include most of the cases when only alphabetic values are used. If you try to add a integer value at the beginning of the string it would in theory become a integer of that value.
I would recommend to read Why PHP Strings Equal Zero or Comparison Operators
Maybe you are looking for str_repeat, instead doing looping for that, its a default value that php serve to you, or you need to cast A into integer . When you try to do calculation for data that is not in Integer/float data type. Usually PHP auto-typecast the variables. In some cases it wont. Then we have to type cast it manually

shifting(<<,>>) operator doesnot work well in php

i am trying to do simple program using php logical operator, when i try to invert the binary value using ~ operator it giving some symbol, instead of actual inverted value also when i try to shift the bits using using <<,>> it doesn't give expected value,help me?
here is the simple coding
<?php
$n=10;
$bin=decbin($n);
echo $bin;
//for onces complement
$com_value=~$bin; //this statement prints some IIIIIIIIIIIIIIII symbol instead of inverted value
echo $com_value;
$shift_value=$bin<<1;
echo $shift_value; //this printing 2020 as result for binary number of ten 1010
?>
help me to get correct value,i have to complete one assignment using that, thanks in advance
decbin returns a string, i.e. a sequence of 0 and 1 characters, with the binary representation of $n. This is not correct as you want to execute ~ and << on the binary value, which is simply $n. Although $n "looks decimal" in the source code, it is stored as 0's and 1's in your memory.
Try to remove that conversion into a string and use $n directly.

PHP Bug with Conditional Operator?

$i=0;
$j='00:15';
if ($i==$j)
echo "equal";
else
echo "not equal";
displays equal?
00:15 is cast into an integer first and then compared with $i. Since intval() (which does the casting) stops at the first non-numeric character, 00:15 is cast into zero, which is equal to zero. No bug here, this is how the == operator works.
If you want to avoid casting, use === instead.
Using the == the types will be converted before they are compared. In this case, both will be converted to an integer, and when you convert a non-numeric string to an integer, it will probably result in 0.
If you use the === operator instead, you will get the result that you expect.
string and int compares as int's, So $j converts to 0 here because starts with 00
Not bizarre at all - when comparing string with integer, the first one is being cast to integer then compared. Use === for type checking and you'll see the real result
Turns out the === is problematic
for example 10===10 will be false if one is integer and one is string even though they are identical as can happen from forms posting.
the solution i used was:
if ("$i"=="$j")
this way it just checks if they're identical regardless of type

issue in reversly printing a number in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does a leading zero do for a php int?
I am just a beginner in php. I tried to write program for print the given number as reverse. For example if i use 123 as input the result should be 321. I tried the following code. It works fine. But if the given input is 0123 the output is 38. I couldn't correct it. How can i correct my code? here is my code.
<?php
$n=123;
$b=0;
while($n>=1)
{
$b=$b*10+$n%10;
$n=$n/10;
}
echo $b;
?>
When you add a 0 to the beginning of your number, PHP treats the number as octal.
Octal 123 is decimal 83, which correctly changes to 38.
Just use the dynamic nature of PHP not much caring about the difference between numbers and strings:
echo strrev(123);
http://www.php.net/manual/en/function.strrev.php
Yes, it's because the numbers that are starting with 0 are considered in base 8, so 0123 is in fact 83 in base 10.
So, your algorithm is correct for integer numbers - if you want the revert of 0123 to be 3210, you could simply revert it as a string and you can simply use the strrev function
I think your math is the problem. You should have parenthesis around the multiplier and mod.
<?php
$num=6541020;
$revnum=0;
do{
$revnum=($revnum *10)+($num % 10);
$num=(int)($num / 10 );
}while($num>0);
echo $revnum;
?>
http://www.weberdev.com/get_example.php3?ExampleID=4879
you could use something like this:
<?php
$rprint = function ($nr) use (&$rprint)
{
echo $nr%10;
if($nr > 10)
{
$rprint($nr/10);
}
};
$rprint(12364);
?>
Edit 1:
OR you could use strings instead of numbers. As I suppose it was already suggested.
(I was a bit out of the subjects)
the integer with the leading zero is always treated as octal in php. So just make it as a string and use the buitin php function strrev()

Categories