4

I have this line in PHP:

$bom != b"\xEF\xBB\xBF" 

When I run it, I get the error:

Parse error: syntax error, unexpected T_NS_SEPARATOR in
C:\xampp\htdocs\MediaAlbumWeb\Utils\Utils.php on line 218

What is the T_NS_SEPARATOR in php and why is it unexpected?

8
  • 1
    That line has no parse errors. Paste your entire code if possible or atleast few lines before line 218.
    – codaddict
    Commented Jun 7, 2011 at 9:20
  • please show some more code relevant to this.
    – Gaurav
    Commented Jun 7, 2011 at 9:20
  • 1
    just a wild guess: try removing the b before the string. the error message hints at namespaces though
    – knittl
    Commented Jun 7, 2011 at 9:21
  • @codaddict Actually, there is a parse error: it's the b, which PHP sees as a constant, followed by a string. The only that'd ever work if there was a concatenation in between ('.'). Nonetheless, I just think the "b" should be left out altogether. Commented Jun 7, 2011 at 9:24
  • 1
    @codaddict, what the... I've never seen this before, but I just tested it on my machine and it actually seems to work. Do you have a link to the documentation? Commented Jun 7, 2011 at 9:28

2 Answers 2

7

You likely have an unclosed single or double quote above that line in your code.

What is the b that's outside of the quotes?

If it's a comparison, it could be something like:

if($bom != "b\xEF\xBB\xBF")
{
 //code
}

Simple code to reproduce this error in PHP:

<?php
$arg = "'T';                      //this unclosed double quote is perfectly fine.

$vehicle = ( $arg == 'B' ? 'bus' : 'not a bus');

print $vehicle . "\n";            //error is thrown on this line.  

?>

Run this, it prints an error:

PHP Parse error:  syntax error, unexpected T_NS_SEPARATOR in 
/var/www/sandbox/eric/code/php/run08/a.php on line 6
1
  • // determine if the file has a utf-8 bom and skip it if it does $bom = fread($fp, 3); if ($bom != b"\xEF\xBB\xBF") rewind($fp); Commented Jun 7, 2011 at 9:32
0

You do a lot of Python, by any chance? b"string" is not a valid way to write your string in PHP, though it is in Python. If you just want the bytes, then you can write the string out as:

echo "\xEF\xBB\xBF";

That works. If you want to check for inequality:

if( $bom != "\xEF\xBB\xBF" ) {
}

What are you checking for anyway? For a Byte Order Mark? And if so: why, exactly?

4
  • 1
    My bad: it seems b"\xEF\xBB\xBF"; is valid syntax in PHP, although I've never known this. Commented Jun 7, 2011 at 9:29
  • It is checking for bom utf format or not. How can I solve this? Commented Jun 7, 2011 at 9:30
  • Riaz; it seems that the syntax is correct. Would you mind showing the rest of the code, there seems to be an issue with that. Commented Jun 7, 2011 at 9:32
  • 1
    // determine if the file has a utf-8 bom and skip it if it does $bom = fread($fp, 3); if ($bom != b"\xEF\xBB\xBF") rewind($fp); Commented Jun 7, 2011 at 10:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.