0

I have the following code, and php -l myfile.php prints out the following:

PHP Parse error:  syntax error, unexpected '{' in test.php on line 13

Parse error: syntax error, unexpected '{' in test.php on line 13

Errors parsing myfile.php

For testing i've copied the function into on own file to make sure, the error is really in this function. I've triple checked the code (and if I missed some stupid error, please apologize).

Code:

<?php
function getNewChannelValue($value, $bit, $flip, $alpha) {
        $max = 255;
        if ($alpha) {
                $max = 127;
        }
        if ($flip) {
                if ($bit && ($value % 2 == 0)) {
                        $value++;
                } else if (!$bit && ($value % 2 != 0)) {
                        $value++;
                }
        } else { //This is line 13
                if ($bit && ($value % 2 == 0)) {
                        $value--;
                } else if (!$bit && ($value % 2 != 0)) {
                        $value--;
                }
        }
        if ($value > $max && $bit) {
                $value = $max;
        }
        if ($value > $max && !$bit) {
                $value = $max - 1;
        }
        if ($value < 0 && $bit) {
                $value = 1;
        }
        if ($value < 0 && !$bit) {
                $value = 0;
        }
        return $value;
}
?>
5
  • 2
    There is no syntax error with the code in the question. Most likely that means typing an unprintable character instead of whitespace. Commented Apr 19, 2014 at 13:33
  • 1
    This is really weird... just remove the space between else and { , it worked for me. Yeah , initially it had a syntax error. Commented Apr 19, 2014 at 13:34
  • I'm unable to replicate your results. php -l notes no syntax errors. Commented Apr 19, 2014 at 13:34
  • 1
    Removing the Space helped! I don't understand why, but... o.O @Tom Walters i had the error on two different OS with different php versions, OS X v 5.5 and Raspbian 5.4.4-14+deb7u8... Commented Apr 19, 2014 at 13:37
  • 1
    For that many ifs,a switch would be better. Commented Apr 19, 2014 at 13:38

1 Answer 1

1

Ok, I think I finally found the error: I opened the file with a hex editor and I discovered a non ASCII-char at this place... Removing the char and replacing it with 0x20 (Space) helped... Seems that nano or something else made a mistake during writing. AD7six was right.

But this doesn't explain why Shankar Damodaran had the same issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.