Parse error: syntax error, Unexpected '$folder' (T_VARIABLE) in

Asked

Viewed 432 times

0

Guys I’m having this error, I read that this message appears when an error occurs in a previous line of code, what is strange to me is what is happening in the first line of the file.

Parse error: syntax error, Unexpected '$folder' (T_VARIABLE) in

<?php
 $pasta = $_SERVER["DOCUMENT_ROOT"]."/arquivos/";

 if(is_dir($pasta))
 {
  $diretorio = dir($pasta);

  while($arquivo = $diretorio->read())
  {
   if(($arquivo != '.') && ($arquivo != '..'))
   {
    unlink($pasta.$arquivo);
    echo 'Arquivo '.$arquivo.' foi apagado com sucesso. <br />';
   }
  }

  $diretorio->close();
 }
 else
 {
  echo 'A pasta não existe.';
 }
?>
  • How you executed this file?

  • Boy, I copied your code from here and made the same mistake, but when I rewrote it exactly the same, it worked

  • Yeah, that’s exactly what the other fellow said.

2 answers

3


Its spaces are neither spaces nor tabs, which is within this below (from the gray table below):

  •   

Behold:

  • The ASCII value of space (" ") is 32
  • The ASCII value of tab (" t") is 9
  • Already the ASCII value of this character you used as space is 8195

I mean, even though it looks like space, it’s not.

It is another character, but it is neither space nor tab, for sure you are using a text editor that is not used to write code, probably using Microsoft Word or Wordpad or the Textedit Mac OSX in "Advanced Document Mode"

So PHP won’t work and no other type script probably, so I recommend that rewrite manually your code using an appropriate text editor, such as one of these:

  • 1

    I didn’t know that this was happening using editors not specific to code, I already came across problems so that the code, after rewriting in an editor, worked, thanks for the information.

  • @Weessmith editors called richtext use own schemes, like Msword, Wordpad, etc. This is because they are not only text, they are proper encodings

  • I see, right here in Stack I’ve seen problems similar to his, agr I know a possible reason.

  • Dude I’m gonna rewrite, it makes sense what you said but the weird thing is that I he’s in the sublime I don’t know why it happened but I’ll rewrite it manually

  • Thank you very much for your reply.

  • for nothing dear @Marcosgoulartdeoliveira this your doubt is very interesting to the community, I’ve been wanting to talk about this kind of problem for a long time. So rewrite with one of the programs I quoted please comment here if it worked, I would be very grateful for your feedback ;)

  • Opa, sorry for the delay to answer I am new programming and as a user in the stack, before I only entered to see answers had never answered or asked before. But it did work. But it wasn’t exactly as you said I was using the sublime but I copied much of the code from a website when I rewrote it worked but from the beginning it was in the sublime,

  • @Marcosgoulartdeoliveira probably the author of such site formatted the post by word or other program so and when copying to his site got this apparent spaces, so you ended up paying the price hehehe, then https documentation://php.net has many examples ready, you should copy from there, for example: http://php.net/manual/en/function.dir.php and change your need :)

Show 3 more comments

1

I simply rewrote the code and it worked:

if(is_dir($pasta)){
    $diretorio = dir($pasta);
    while($arquivo=$diretorio->read()){
        if(($arquivo!='.')&&($arquivo!='..')){
            unlink($pasta.$arquivo);
            echo "Arquivo $arquivo foi apagado com sucesso.<br>";
        }
    }
    $diretorio->close();
}else{
    echo "A pasta não existe";
}

I believe that’s exactly what @Guilhermenascimento said now in his reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.