How is this possible? I am returning false, but it 'unset'

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

McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

How is this possible? I am returning false, but it 'unset'

Post by McManCSU »

I am seeing this very odd problem:

I have this code:

Code: Select all

echo 'count = '.$count.'<br>';
    
    if ($count > 1)
    {
	echo '************     Returning false<br><br>';	
	return false;
    }
Which returns to this:

Code: Select all

else
{
	$rtnVal= $func($val);
	echo 'RESULT3: ' . $rtnVal . ', func: '.$func.' and val: '.$val.'<br>';
}
I see this output:

Code: Select all

count = 2
************ Returning false

RESULT3: , func: isNotDuplicateEntry and val: 12345
Notice how the $result is 'unset' and not false!? This works for when I return true; the $result is '1'... But I am not seeing '0' for this case.

Ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I can not reproduce the error

Code: Select all

<?php
function foo($count) {
  echo 'count = '.$count.'<br>';
  if ($count > 1)
  {
    echo '************     Returning false<br><br>';
    return false;
  }

  return true;
}

function bar() {
  $val = 2;
  $func = 'foo';

  if (false) {
    //
  }
  else
  {
    $rtnVal= $func($val);
    echo 'RESULT3: ' . $rtnVal . ', func: '.$func.' and val: '.$val.'<br>';
    var_dump($rtnVal);
  }
}

bar();
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

Ya, I dont quite understand whats going on here either since it seems pretty straight forward.

Is there a way to force the return to be a boolean or something? Maybe a 'return (false);' would work...?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

McManCSU wrote:Is there a way to force the return to be a boolean or something? Maybe a 'return (false);' would work...?
I doubt it. It's your code wrapped up in two functions and it's working. You certainly did something wrong elsewhere.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Whats teh meaning of this line

Code: Select all

$rtnVal= $func($val);
$func() ?? or $var or func() ??
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

"echo false" prints just nothing, not the word "false".
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

Actually echo false should print 0 (likewise echo true prints 1). I am using 'func' as a function and 'var' as its argument.

EDIT: I take that back. I see this when I type this echo.

Code: Select all

echo '"'.false.'" "'.true.'"';
Prints

Code: Select all

"" "1"
Interesting I suppose. Maybe this isnt really a problem then?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

true means 1 and false means NULL not Zero .
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

neel_basu wrote:true means 1 and false means NULL not Zero .
Wrong. False has a value (of type "boolean"). NULL does not have a value.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i see what you are doing, but why you are doing it is beyond me. why are you wasting your time doing it this way anyways?

I mean, defining a variable to be the name of a function, to just call the variable later as the function name is very redundant and unnecessary to me.

Not to mention the next coder in line who sees you doing this will be like "What in the hell was this guy smoking when he wrote this??"

I know i would...
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

Ok I think then my problem is related to assignments like this:

Code: Select all

$a = false;
$b = false;

// Test 1
if ($a === false)
{
    echo ‘This will print’;
}

$a &= $b;
if ($a === false)
{
    echo ‘this will NOT print’;
}
From what I have gathered, I think this is due to &= being a bit-wise operation. So if I print the value $a after doing a &=, it prints '0' (vs a string-of-length 0)

It seems like the best way to retain 'boolean' nature of the variable is to do:

Code: Select all

$a = $a && $b;
Is there a better way (like using the &= operator) to do a quick and-equal assignment?

Thanks
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

I see what you are doing, but why you are doing it is beyond me. why are you wasting your time doing it this way anyways?

I mean, defining a variable to be the name of a function, to just call the variable later as the function name is very redundant and unnecessary to me.

Not to mention the next coder in line who sees you doing this will be like "What in the hell was this guy smoking when he wrote this??"

I know i would...
Thanks for your insight, but this is actually a great idea and a great method for what I am doing. There are too many details but I will give you an brief idea of why:

Think of a wizard, like an installation wizard with a bunch of steps. Instead of having to create some massive if statement that checks which step you are on and which step you are going to, or whether the next step even applies to you, etc., you can use a variable that is the function name. You can have the steps (each function) stored in an array and just increment on the array. Which ever step you are on is determined on the index of it. And since each step may not always apply, you want a quick and easy way to skip over it.

Its simple, easy and short. However, if you like huge if statements crammed into a 5 million line code base, be my guest and do things your way :) But then I would be the one asking what you were smoking...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

infolock wrote:i see what you are doing, but why you are doing it is beyond me. why are you wasting your time doing it this way anyways?

I mean, defining a variable to be the name of a function, to just call the variable later as the function name is very redundant and unnecessary to me.

Not to mention the next coder in line who sees you doing this will be like "What in the hell was this guy smoking when he wrote this??"

I know i would...
I do this sort of thing all the time. If you've ever written a system that allows a user to build something that isn't static (a form builder for example) having the ability to dynamically call functions is really helpful. It gets really interesting when your functions don't even have names.
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

... Nothing...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

McManCSU wrote:Its simple, easy and short. However, if you like huge if statements crammed into a 5 million line code base, be my guest and do things your way :) But then I would be the one asking what you were smoking...
Why not switches instead of ifs? And a 5 million line code base seems a bit large for PHP. Come to think of it, would that even parse? :twisted:
Post Reply