Re: how can one variable equal 2 things??



On 02/07/2005 23:34, Marcus wrote:

[snip]

if anything, I would have thought testing if ['default'] == 1 would be a boolean true since the variable exists and is not NULL, but testing if ['default'] == 1 returns FALSE.

This is due to implicit type conversion. When you use the equality (==) rather than strict equality (===) and compare two different types, the values are converted into something that can be compared in some meaningful way.


If you compare a string to a number, an attempt is made to convert that string to a number first. If the string starts with valid number data, then that will be the used value, otherwise the comparison will be against zero (0).

The string 'default' doesn't have anything resembling a number at its start, so it will be converted to zero. You then perform a comparison against literal zero, hence the expression evaluates to true.

See the type comparison page[1], as well as the section on converting strings to numbers[2], in the manual.

Mike


[1] <URL:http://www.php.net/manual/en/types.comparisons.php>
[2] <URL:http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion>


--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.



Relevant Pages

  • Re: Unicode String Parameters in DLLs
    ... > NOT Unicode, so you might have heard some stuff about that. ... > One thing to watch out for is if your DLL routine puts a zero byte at the ... > string ); it does not terminate the string. ... >> We are converting our application to work with Unicode. ...
    (microsoft.public.word.vba.general)
  • Re: how can one variable equal 2 things??
    ... a boolean true since the variable exists and is not NULL, but testing if == 1 returns FALSE. ... This is due to implicit type conversion. ... If you compare a string to a number, an attempt is made to convert that string to a number first. ... If the string starts with valid number data, then that will be the used value, otherwise the comparison will be against zero. ...
    (comp.lang.php)
  • Code worked fine in VB6 but not .NET
    ... The code worked perfectly fine in VB6 for converting a decimal length to ... binary hex, but in .NET it only works when the iNum variable doesn't ... If zero is passed in then I lose everything in the ... lPosLen As Integer) As String ...
    (microsoft.public.dotnet.languages.csharp)
  • Chr(0) in .NET causes string to return blank
    ... The code worked perfectly fine in VB6 for converting a decimal length to ... binary hex, but in .NET it only works when the iNum variable doesn't ... If zero is passed in then I lose everything in the ... iPosLen As Integer) As String ...
    (microsoft.public.dotnet.framework)
  • Re: Code worked fine in VB6 but not .NET
    ... > The code worked perfectly fine in VB6 for converting a decimal length to ... If zero is passed in then I lose everything in the ... > Public Function SetBinaryLengthofMsg(ByRef lLen As Integer, ... > lPosLen As Integer) As String ...
    (microsoft.public.dotnet.languages.csharp)