473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with multiple conditions

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.

Nov 26 '06 #1
10 2656
On Sun, 26 Nov 2006 07:44:53 -0800, greedo wrote:
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.
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";
BTW, your assumption about C-ish equivalent between strings and arrays
does not hold water in the PHP world. Your "settype" trick would be neat,
had it worked.

--
http://www.mladen-gogala.com

Nov 26 '06 #2
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. :)

Mladen Gogala wrote:
On Sun, 26 Nov 2006 07:44:53 -0800, greedo wrote:
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.

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";
BTW, your assumption about C-ish equivalent between strings and arrays
does not hold water in the PHP world. Your "settype" trick would be neat,
had it worked.

--
http://www.mladen-gogala.com
Nov 26 '06 #3
..oO(gr****@starschiffchen.de)
>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.
Works here on PHP 5.2. What version do you use?
>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.
In this case the second condition will always be FALSE.
It should be

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

Micha
Nov 26 '06 #4
..oO(Mladen Gogala)
>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);
Eh?
>BTW, your assumption about C-ish equivalent between strings and arrays
does not hold water in the PHP world.
Of course it does.

http://www.php.net/manual/en/languag....string.substr
>Your "settype" trick would be neat,
had it worked.
It _does_ work.

Micha
Nov 26 '06 #5
On Sun, 26 Nov 2006 21:54:36 +0100, Michael Fesser wrote:
Eh?
>>BTW, your assumption about C-ish equivalent between strings and arrays
does not hold water in the PHP world.

Of course it does.

http://www.php.net/manual/en/languag....string.substr
PHP allows me to address particular characters of the string by using []
notation, but I remember having difficulties with numeric operations, when
using that.
>
>>Your "settype" trick would be neat,
had it worked.

It _does_ work.
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.
--
http://www.mladen-gogala.com

Nov 27 '06 #6
On Sun, 26 Nov 2006 21:54:36 +0100, Michael Fesser wrote:
Works here on PHP 5.2. What version do you use?
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.
--
http://www.mladen-gogala.com

Nov 27 '06 #7
Michael Fesser wrote:
.oO(gr****@starschiffchen.de)

>>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.


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

>>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.


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

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

Micha
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.
js*******@attglobal.net
==================
Nov 27 '06 #8
..oO(Mladen Gogala)
>On Sun, 26 Nov 2006 21:54:36 +0100, Michael Fesser wrote:
>Works here on PHP 5.2. What version do you use?

No, it doesn't:
$ /tmp/ttt
PHP Notice: Array to string conversion in /tmp/ttt on line 4
[...]
OK, obviously the OP wasn't clear enough, because my understanding of
his problem was somewhat different.
>$ cat /tmp/ttt
#!/usr/local/bin/php
<?php
$zee=array(3,4,5);
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
Nov 27 '06 #9
..oO(Jerry Stuckle)
>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?
I always log errors and warnings while developing, but maybe my
understanding of the OP's problem was just different. See my reply to
Mladen.

Micha
Nov 27 '06 #10
On Mon, 27 Nov 2006 17:15:13 +0100, Michael Fesser wrote:
>
OK, obviously the OP wasn't clear enough, because my understanding of
his problem was somewhat different.
I agree, the original poster didn't provide the definition for $zee, so
we can only speculate. Your guess is as good as mine.

--
http://www.mladen-gogala.com

Nov 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: M Wells | last post by:
Hi All, Just wondering if anyone can tell me if you can test for multiple conditions as part of an "IF" statement in T-SQL in SQL Server 2000? ie something like: IF @merr = 1 or @merr=2...
10
by: JMorrell | last post by:
First post to this community so am not sure if this is the correct place. Here goes. I have a MS Access db that keeps track of employees sick and annual leave balances. In it, I have a report,...
1
by: antao | last post by:
I'm keeping in the database a log of all the sessions for my application. I'm trying to write a stored procedure that returns all the sessions that; the login contains a certain string, loggedin...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
39
by: Marcin Zmyslowski | last post by:
Hello all! I have the following problem with MS Access 2003 permissions. I have two users. One is admin and the second one is user who has full permissions to enter modify and read data. I...
6
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or...
4
by: Prabhat | last post by:
Hi Friends, Can I have one page "prod.asp" with 2 forms (form1 with ONE Submit Button and form2 with 2 submit buttons) submiting to same page "prod.asp" again? Thanks Prabhat
7
by: apattin | last post by:
Hi experts, I am having a hard time coding a trigger to handle multiple validation conditions in DB2 UDB V8.1.9.. I thought that a single trigger might be better than one-trigger-per-condition,...
3
by: kavithapotnuru | last post by:
Hi All, Can anyone help me how to give multiple conditions in a while loop in perl. For example i have a while loop for which the value of a variable is A or B it should not enter in to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.