silvester pinari silvester pinari - 1 year ago 75
PHP Question

I am writing a simple php code to check if number is odd or even

I am trying to write a SIMPLE php code to see if number is odd or even but i keep getting an error saying "expected statment" after the closing bracket on the if statement. can someone please explain what i need to do?


function odd_or_even($num) {

if ($num % 2 == 0);
print'Number is even';

} else {

print 'odd';


Answer Source

Posting as a community wiki.

As I stated in comments:

if ($num % 2 == 0);
                  ^ ends/terminates the statement.

As per what the manual states on line termination:

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.


Answer: Remove it.

Taken from comments:

"The else is unexpected because the conditional becomes a one liner that does nothing. The expected statment the OP mentions is a mis-quoted error message. – chris85"

What you posted should have thrown you the following error:

Parse error: syntax error, unexpected 'else' (T_ELSE)

But you failed to post that actual error message.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download