Variable recognition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Variable recognition

Post by mjseaden »

Hi

I can't work out what is causing this strange problem with PHP 'incorrectly' determining the contents of a variable. In the run whos output I quote, $type = 0. However, PHP doesn't seem to recognise this. Here is the code:

Code: Select all

if( $page == 1 )
{
if( $loc == 'ALL_LOCS' )
{
    if( $type == 'ALL_TYPES' )
    {
        echo 'PHP thinks that '.$type.' == ALL_TYPES!!';
    }
    elseif( $type != 'ALL_TYPES' )
    {
and here is the output, directly cut and pasted:

Code: Select all

PHP thinks that 0 == ALL_TYPES!!
Incidently, if I change the check for 'ALL_TYPES' to 'ABSOLUTE_GIMBOID' for example, I get exactly the same problem - it then seems to think that 0 == 'ABSOLUTE_GIMBOID'.

Can anyone shed any light on this problem? Why is it thinking that 0 is equal to any string I place in the if statement?

Many thanks

Mark
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

were does $type come from?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

The $type variable is passed to a function stored in an 'include'd PHP script. The reason I have not looked very hard at the way it is parsed is because when echo'd, the variables do not match. $type is parsed as 0 to the function and appears as zero when echo'd.

I'm really confused by this problem - it appears that I can determine whether the variables are equal - but PHP/the computer can't!

Mark
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i'm sorry, but i can't reproduce what you are experiencing...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

did the same used

Code: Select all

<?php
$page =1;
$loc = 'ALL_LOCS';

$type ='ALL_TYPES';

if( $page == 1 )
{
if( $loc == 'ALL_LOCS' )
{
    if( $type == 'ALL_TYPES' )
    {
        echo 'PHP thinks that '.$type.' == ALL_TYPES!!';
    }
    elseif( $type != 'ALL_TYPES' )
    {
        echo 'type '.$type.' != ALL_TYPES';
    }
}

}
    	
 ?>
but it works just fine for me.

you can try http://www.php.net/manual/en/function.gettype.php gettype() to see if it comes out as anything else.

There isn't a global variable some where else that might becausing something weird to be happening?
Post Reply