Diamonte
Diamonte

Reputation: 413

PHP IF OR AND not working

I have problems to make IF OR statement properly, in this case it does not matter if I use || or "OR".

I have now: if($email == '' || (!filter_var($email, FILTER_VALIDATE_EMAIL)))

I get error PHP Fatal error: Call to undefined function \xa0() in

if I change to if($email == '' || !filter_var($email, FILTER_VALIDATE_EMAIL))

I get error PHP Parse error: syntax error, unexpected '!' in

Upvotes: 2

Views: 254

Answers (2)

smassey
smassey

Reputation: 5931

This error message Call to undefined function \xa0() in looks a lot like encoding types are the source of your problem. The \xA0 character is a bugger:

\xa0 is actually non-breaking space in Latin1 (ISO 8859-1), also
chr(160). You should replace it with a space. When .encode('utf-8'), it
will encode the unicode to utf-8, that means every unicode could be
represented by 1 to 4 bytes.Jun 12, 2012

from Python: Removing \xa0 from string?

The problem is you can't see it either because it's a special "whitespace" that was most probably inserted during a Ctrl+C / Ctrl+V from some strange IDE or website.

This should remove them all from your file and replace them with good whitespaces:

file_put_contents('your-script-cleaned.php', 
                  str_replace('\\x0a', 
                  ' ', 
                  file_get_contents('your-script-original.php')))

An IDE search & replace could do the trick just as well.

Upvotes: 6

Your code has a non-breaking space character (\u00A0) after the ||.

Additionally PHP is stupid enough to consider that byte a viable identifier, thus it thinks

<nbsp>(!filter_var($email, FILTER_VALIDATE_EMAIL))

is a function call, not unlike

myfunction(!filter_var($email, FILTER_VALIDATE_EMAIL))

whereas

<nbsp>!filter_var($email, FILTER_VALIDATE_EMAIL)

is a syntax error (as would be myfunction!filter_var($email, FILTER_VALIDATE_EMAIL)), as ! cannot be used as a binary operator).


Typing such non-breaking spaces happens fairly commonly in keyboard layouts wherein you must type the pipe character | with Alt Gr, such as the standard Finnish layout that I am using. This occurs because you still have the Alt Gr pressed down while hitting the space bar; on many layouts Alt Gr +                             will produce a non-breaking space. (Note that I myself used non-breaking space in my answer to make the space-bar key ;)


A good editor would be able to mark such an use a syntax error, or perhaps you can configure your editor to highlight such mistakes.

Upvotes: 0

Related Questions