how do you pass a zero as a function argument?
how do you pass a zero as a function argument?
am 01.10.2007 20:44:21 von Jake Barnes
I've got a function that starts off like this:
function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
}
I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
4, etc). It finds in the database records starting with that letter or
number. It works fine except when I try to pass in a zero. The PHP
code acts as if I have not passed in a function argument, so
$whichLetter becomes "a", which is the default value (as you can
see).
So how do I get PHP to see that I'm really passing in a zero, and not
just some false value?
Re: how do you pass a zero as a function argument?
am 01.10.2007 20:50:40 von Shelly
"lawrence k" wrote in message
news:1191264261.832485.249450@g4g2000hsf.googlegroups.com...
> I've got a function that starts off like this:
>
> function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
>
> }
>
>
> I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
> 4, etc). It finds in the database records starting with that letter or
> number. It works fine except when I try to pass in a zero. The PHP
> code acts as if I have not passed in a function argument, so
> $whichLetter becomes "a", which is the default value (as you can
> see).
>
> So how do I get PHP to see that I'm really passing in a zero, and not
> just some false value?
>
Are you calling it with alphabeticalListOfAlbumsByLetter("0") or by
alphabeticalListOfAlbumsByLetter(0) ? If it works for "1", it should work
for "0". After all, what is being passed in is really only the ascii value
and the letters "1" and "0" both have ascii values.
Shelly
Re: how do you pass a zero as a function argument?
am 01.10.2007 20:51:36 von zeldorblat
On Oct 1, 2:44 pm, lawrence k wrote:
> I've got a function that starts off like this:
>
> function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
>
> }
>
> I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
> 4, etc). It finds in the database records starting with that letter or
> number. It works fine except when I try to pass in a zero. The PHP
> code acts as if I have not passed in a function argument, so
> $whichLetter becomes "a", which is the default value (as you can
> see).
>
> So how do I get PHP to see that I'm really passing in a zero, and not
> just some false value?
The problem is likely elsewhere in your code. The following correctly
outputs "0" for me:
function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
echo $whichLetter;
}
alphabeticalListOfAlbumsByLetter(0);
?>
Re: how do you pass a zero as a function argument?
am 02.10.2007 19:07:51 von John Murtari
lawrence k writes:
> I've got a function that starts off like this:
>
> function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
>
> }
>
>
> I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
> 4, etc). It finds in the database records starting with that letter or
> number. It works fine except when I try to pass in a zero. The PHP
> code acts as if I have not passed in a function argument, so
> $whichLetter becomes "a", which is the default value (as you can
> see).
>
> So how do I get PHP to see that I'm really passing in a zero, and not
> just some false value?
THere are a few ways to do this, you may want to try the
REALLY EQUAL operator, "===". An example:
function isItZero($x) {
if ($x) {
echo "non zero param $x\n";
} else if (!$x && $x === 0) {
echo "int zero $x\n";
} else if (!$x && $x === '0') {
echo "char zero $x\n";
} else {
echo "Blank param\n";
}
}
isItZero('a');
isItZero("");
isItZero(0);
isItZero('0');
---- WILL OUTPUT
non zero param a
Blank param
int zero 0
char zero 0
--
John
____________________________________________________________ _______
John Murtari Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211) "TheBook.Com" (TM)
http://thebook.com/
Re: how do you pass a zero as a function argument?
am 02.10.2007 19:51:37 von luiheidsgoeroe
On Mon, 01 Oct 2007 20:44:21 +0200, lawrence k =
=
wrote:
> I've got a function that starts off like this:
>
> function alphabeticalListOfAlbumsByLetter($whichLetter=3D"a") {
>
> }
>
>
> I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
> 4, etc). It finds in the database records starting with that letter or=
> number. It works fine except when I try to pass in a zero. The PHP
> code acts as if I have not passed in a function argument, so
> $whichLetter becomes "a", which is the default value (as you can
> see).
>
> So how do I get PHP to see that I'm really passing in a zero, and not
> just some false value?
As you need a string, I'd say: cast it to one, and check it's length.
//if you always just need a single character:
if(strlen($whichLetter)==1){
//do your thing
}
//or if it just needs to be 1 or more:
if(strlen($whichLetter)>=3D1){
//do your thing
}
-- =
Rik Wasmus
Re: how do you pass a zero as a function argument?
am 02.10.2007 20:35:43 von Steve
> function isItZero($x) {
> if ($x) {
> echo "non zero param $x\n";
> } else if (!$x && $x === 0) {
> echo "int zero $x\n";
> } else if (!$x && $x === '0') {
> echo "char zero $x\n";
> } else {
> echo "Blank param\n";
> }
> }
php.net documentation...see: switch