problem with multiple conditions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi, first of all, please bear with me, I don't really know a great deal
about php programming, as will be painfully obvious from the code
snippet I'm gonna post. But I need this problem solved, and I can't
find the information I need (or maybe I can't understand it). So here's
the thing:

What I have is a range of integers, and I need those whose digits
alternate between odd and even. Example: 123456789. For that purpose I
have this thingy here:

settype(\$zee, "string");
if ((\$zee[0] % 2 != 0) && (\$zee[1] % 2 == 0))
{
echo \$zee . "<br />";

}

Just to be precise: I want to have \$zee echoed, if the first digit is
odd and the second is even.

Now, this doesn't work, because apparently the second condition is
never true. I don't get why. Because if I change the "==" in the second
condition to "!=" (that is to say I want both digits to be odd), then
it works fine! Can someone help me fix this?

PS:
When the conditions are put like this:
if ((\$zee[0] & 1) && (\$zee[1] & 0))
the exact same thing happens: it works with two odd conditions, but not
with one odd, one even.

Re: problem with multiple conditions

On Sun, 26 Nov 2006 07:44:53 -0800, greedo wrote:

Your type conversion is a mortal sin and may cost you dearly when you meet
the Great Programmer. Repent, o sinner, change thy evil ways and do
something like this:

\$zere="345678";
\$zee=preg_split('//',\$zere,-1,PREG_SPLIT_NO_EMPTY);
if ((\$zee[0] % 2 != 0) && (\$zee[1] % 2 == 0))
{
echo "zere=\$zere\n";

}
else echo '\$zee[0]=',\$zee[0],"   ",'\$zee[1]=',\$zee[1],"\n";

does not hold water in the PHP world. Your "settype" trick would be neat,

--

Re: problem with multiple conditions

First of all thanks, I will try to figure out what you did there and
hopefully this'll do. May take me a moment, tho. :)

Re: problem with multiple conditions

Eh?

Of course it does.

http://www.php.net/manual/en/language.types.string.php#language.types.string.substr

It _does_ work.

Micha

Re: problem with multiple conditions

On Sun, 26 Nov 2006 21:54:36 +0100, Michael Fesser wrote:

PHP allows me to address particular characters of the string by using []
notation, but I remember having difficulties with numeric operations, when
using that.

With settype? No, it doesn't. You can't convert from an array to string
and back just like that. His code proves the opposite.

--

Re: problem with multiple conditions

.oO(greedo@starschiffchen.de)

Works here on PHP 5.2. What version do you use?

In this case the second condition will always be FALSE.
It should be

if ((\$zee[0] & 1) && !(\$zee[1] & 1)) {
...
}

Micha

Re: problem with multiple conditions

On Sun, 26 Nov 2006 21:54:36 +0100, Michael Fesser wrote:

No, it doesn't:
\$ /tmp/ttt
PHP Notice:  Array to string conversion in /tmp/ttt on line 4
\$
\$
\$ cat /tmp/ttt
#!/usr/local/bin/php
<?php
\$zee=array(3,4,5);
settype(\$zee, "string");
if ((\$zee[0] % 2 != 0) && (\$zee[1] % 2 == 0))
{
echo \$zee . "<br />";

}
?>

\$ /usr/local/bin/php -v
PHP 5.2.0 (cli) (built: Nov  3 2006 21:13:54)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

What he wants is to convert array to string for faster printing, sort like
the equivalence between char[] and *char in C language. He has an array of
integers and wants to print them as a string, provided certain numeric
conditions are met. That is why I broke a string into an array in my
snippet. In other words, he wants to be smart with "settype" function.

--

Re: problem with multiple conditions

OK, obviously the OP wasn't clear enough, because my understanding of
his problem was somewhat different.

He mentioned a "range of integers", but for me this doesn't necessarily
mean an array like above. It could also be simple integers like

\$zee = 123456789;

which is what I used in my test.

Micha

Re: problem with multiple conditions

On Mon, 27 Nov 2006 17:15:13 +0100, Michael Fesser wrote:

I agree, the original poster didn't provide the definition for \$zee, so
we can only speculate. Your guess is as good as mine.

--

Re: problem with multiple conditions

Michael Fesser wrote:

No, it doesn't work here in PHP 5.2.  It looks like it does in your case
maybe because you aren't displaying your errors?

Additionally, his test is perfectly correct.  (\$x % 2) will be zero for
even numbers and 1 for odd numbers.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: problem with multiple conditions

.oO(Jerry Stuckle)

I always log errors and warnings while developing, but maybe my
understanding of the OP's problem was just different. See my reply to