# Syntax has me baffled

Thread began 9/24/2009 11:18 pm by tom92909 | Last modified 9/25/2009 3:43 pm by Jason Byrnes | 1841 views | 6 replies |

## 9/24/2009 11:18 pm  |  #1 tom92909Beta Tester

### Syntax has me baffled

I need to run two numeric variables through a series "IF statements"...??? Unless there's a better way...

Here are my parameters... ( I do realize that what I'm typing in the code below is in NO WAY proper syntax, I'm just trying to ensure that I'm explaining myself clearly)

php:
``````IF \$x is even
AND \$y is even  ( do nothing)

IF \$x is odd
AND \$y is odd  ( do nothing)

IF \$x is even
AND \$y is odd  ( \$y = \$y+1)

IF \$x is odd
AND \$y is even  ( \$y = \$y+1)``````

I've run into a wall here and need a helping hand.

Thanks!

Tom

## 9/25/2009 12:06 am  |  #2 anonymous

Tom,

You could either do a modified if statement or a case and switch statement. I think since two of your if statements should result in the same action, you would be best with a modified if statement like this:

php:
``if ((\$x%2=0 AND \$y%2=0) OR (\$x%2!=0 AND \$y%2!=0)) {DO NOTHING  } elseif ((\$x%2=0 AND \$y%2!=0) OR (\$x%2!=0 AND \$y%2=0)) {GO CRAZY}``

## 9/25/2009 12:41 am  |  #3 tom92909Beta Tester

hmmm, I'm not getting a positive result with that either.

php:
``````<?php
\$min =5;
\$max =12;
if ((\$min %2 = 0 AND \$max %2 = 0) OR (\$min %2 != 0 AND \$max %2 != 0)) {
} elseif ((\$min %2 = 0 AND \$max %2 != 0) OR (\$min %2 !=0 AND \$max %2 = 0)) {
\$max = \$max+1;
}
echo "\$min";
echo "\$max";
?>``````

The above code produces the following error...

Parse error: syntax error, unexpected '=' on line 3

The following code will produce an echo result of "3 is odd"

php:
``````<?php
\$i=3;
if (\$i % 2) {
echo "\$i is odd";
} else {
echo "\$i is even";
}
?>``````

So I'm still baffled on syntax...

## 9/25/2009 3:02 am  |  #4 tom92909Beta Tester

PHP makes efficient work out of my problem.

php:
``````if(\$x%2 != \$y%2) {
\$y++;
}``````

Crazy simple solution. Cheers to PHP!

## 9/25/2009 12:09 pm  |  #5 Jason ByrnesWebAssist

The error you where getting was because og the way you where doing the comparisons.

in this line:

php:
``if ((\$min %2 = 0 AND \$max %2 = 0) OR (\$min %2 != 0 AND \$max %2 != 0)) {``

this code is setting the result:
\$min %2 = 0

to do an equals comparison in PHP, it should be:
\$min %2 == 0

so that line should be:

php:
``if ((\$min %2 == 0 AND \$max %2 == 0) OR (\$min %2 != 0 AND \$max %2 != 0)) {``

## 9/25/2009 2:23 pm  |  #6 tom92909Beta Tester

Jason,

Thanks for the clarification, that kinda helped, but I'm not really getting the output I wanted.

Let me explain again what I'm needing.

I have (2) dynamic numbers.
\$x = 5;
\$y = 14;

so my issue is how do I check for the following conditions...

IF \$x < 0 (\$x=0)
IF \$x is even
AND \$y is even ( do nothing)

IF \$x < 0 (\$x=0)
IF \$x is odd
AND \$y is odd ( do nothing)

IF \$x < 0 (\$x=0)
IF \$x is even
AND \$y is odd ( \$y++)

IF \$x < 0 (\$x=0)
IF \$x is odd
AND \$y is even ( \$y++)

I'm a bit lost how to write the statement to get the above.
I hope that I've explained myself better this time.

## 9/25/2009 3:43 pm  |  #7 Jason ByrnesWebAssist

try the following:

php:
``if(\$x < 0) \$x = 0; if ((\$x%2==0 AND \$y%2!=0) OR (\$x%2!=0 AND \$y%2==0)) \$y++;``

If that does not give the desired results, please explain what the outcome is and how it differs from what you are trying to accomplish.

## Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

## Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

## Everything else!

Close Window

Account or customer service questions?

Need technical support?

Content

Layout

Ease of use

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Window

## We were unable to retrieve the attached file

Close Window

### Attach and remove files

• Text Formatting
• Insert Element
• Block Formatting
• Cancel
Close Window

Close Window

#### This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --