Page 1 of 3
How is this possible? I am returning false, but it 'unset'
Posted: Mon May 07, 2007 6:53 pm
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?
Posted: Mon May 07, 2007 7:53 pm
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();
Posted: Mon May 07, 2007 9:44 pm
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...?
Posted: Mon May 07, 2007 10:56 pm
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.
Posted: Tue May 08, 2007 12:53 am
by neel_basu
Whats teh meaning of this line
$func() ?? or $var or func() ??
Posted: Tue May 08, 2007 1:30 am
by stereofrog
"echo false" prints just nothing, not the word "false".
Posted: Tue May 08, 2007 9:44 am
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.
Prints
Interesting I suppose. Maybe this isnt really a problem then?
Posted: Tue May 08, 2007 10:05 am
by neel_basu
true means 1 and false means NULL not Zero .
Posted: Tue May 08, 2007 11:44 am
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.
Posted: Tue May 08, 2007 2:23 pm
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...
Posted: Tue May 08, 2007 5:10 pm
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:
Is there a better way (like using the &= operator) to do a quick and-equal assignment?
Thanks
Posted: Tue May 08, 2007 11:48 pm
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...
Posted: Wed May 09, 2007 5:13 am
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.
Posted: Wed May 09, 2007 10:49 am
by McManCSU
... Nothing...
Posted: Wed May 09, 2007 12:15 pm
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?
