"Syntax error, Unexpected 'if' (T_IF)", even without apparent error

Asked

Viewed 1,205 times

3

You’re making that mistake:

Parse error: syntax error, Unexpected 'if' (T_IF) in C: Program Files Easyphp-Devserver-17 eds-www Hugg sys historico.php on line 3

I am a long time looking for the solution but can not find. The error is on this line:

if(isset($_POST['mensagem'])){

The code:

header("Content-Type: application/json;charset=utf-8");
    if(isset($_POST['mensagem'])){
        include_once "../defines.php";
        require_once('../classes/BD.class.php');
        BD::conn();

        $mensagens = array();
        $id_conversa = (int)$_POST['conversacom'];
        $online = (int)$_POST['online'];

        $pegaConversas = BD::conn()->prepare("SELECT * FROM `mensagens` WHERE (`id_de` = ? AND `id_para` = ?) OR (`id_de` = ? AND `id_para` = ?) ORDER BY `id` DESC LIMIT 10");
        $pegaConversas->execute(array($online, $id_conversa, $id_conversa, $online));

        while($row = $pegaConversas->fetch()){
            $fotoUser = '';
            if($online == $row['id_de']){
                $janela_de = $row['id_para'];

            }elseif($online == $row['id_para']){
                $janela_de = $row['id_de'];

                $pegaFoto = BD::conn()->prepare("SELECT `foto` FROM `usuarios` WHERE `id` = '".$row['id_de']."'");
                $pegaFoto->execute();

                while ($usr = $pegaFoto->fetch()){
                    $fotoUser = ($usr['foto'] == '') ? 'default.jpg' : $usr['foto'];
                }
            }

            $emotions = array(':P', ':$', ':|', ':O', '<3', ';*', ':%', ':@', ':D', ';D', ':A', ':"(', ':(', ':Z');
            $imgs = array(
                '<img src="smiles/tongue.png" width="16" height="16" border="0"/>',
                '<img src="smiles/redface.png" width="16" height="16" border="0"/>',
                '<img src="smiles/nada.png" width="16" height="16" border="0"/>',
                '<img src="smiles/espanto.png" width="16" height="16" border="0"/>',
                '<img src="smiles/blowkiss.png" width="16" height="16" border="0"/>',
                '<img src="smiles/kiss.png" width="16" height="16" border="0"/>',
                '<img src="smiles/zangado.png" width="16" height="16" border="0"/>',
                '<img src="smiles/mad.png" width="16" height="16" border="0"/>',
                '<img src="smiles/rindo.png" width="16" height="16" border="0"/>',
                '<img src="smiles/rindo2.png" width="16" height="16" border="0"/>',
                '<img src="smiles/anjo.png" width="16" height="16" border="0"/>',
                '<img src="smiles/choro.png" width="16" height="16" border="0"/>',
                '<img src="smiles/triste.png" width="16" height="16" border="0"/>',
                '<img src="smiles/dormindo.png" width="16" height="16" border="0"/>'
            );
            $msg = str_replace($emotions, $imgs, $row['mensagem']);
            $mensagens[] = array(
                'id' => $row['id'],
                'mensagem' => utf8_encode($msg),
                'fotoUser' => $fotoUser,
                'id_de' => $row['id_de'],
                'id_para' => $row['id_para'],
                'janela_de' => $janela_de
            );
        }
        die(json_encode($mensagens));
    }

I posted it complete here.

1 answer

6


Almost every code editor has an option to show spaces and line breaks, if you enable the option in your editor during editing (at least when this type of problem arises), it will probably find these things easier.

You can see by copying from yours Pastebin and pasting into a code editor that you have an invisible (null) special character at the end of the header line.

<?php
header("Content-Type: application/json;charset=utf-8");
                                                       ^^ --- AQUI
    if(isset($_POST['mensagem'])){
        include_once "../defines.

View the file opened in a hexadecimal viewer:

Hexdump mostrando caractere 0x00 no fim da linha

I used this excellent free tool: https://mh-nexus.de/en/hxd/


One of the simplest solutions for your case is to cursor before the ;, delete until you join the two lines and you get this way:

header("Content-Type: application/json;charset=utf-8")f(isset($_POST['mensagem'])){
               É pra deletar até colar tudo mesmo ---^^ 

(all this to make sure that you deleted the invisible characters).

Once you’ve done that, give ENTER between the lines again, reset the ; on the top line and the i of if that the problem must be remedied.

Browser other questions tagged

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