Syntax error, unexpected 'function' (T_STRING)

Bonjour,
Je voudrais créer un sondage avec php et jai énormément de mal, avec ce code, il m'affiche "Parse error: syntax error, unexpected 'name_function' (T_STRING) in /Applications/XAMPP/xamppfiles/htdocs/news.php on line 86"
Merci de votre aide!

<div class="content2">
<div class="form">
<form name="formulaire" method="get" action="<?=$_SERVER['PHP_SELF'];?>">
<fieldset>
<legend><em><strong>24-03-2017</strong></em></legend>
<p class="vote">
<img src="images/chats-sphynx.jpg" class="chatflottant" />
<img src="images/chaton-race-sphynx.jpg" class="chatflottant2" />
Voter pour les noms de chats<br/>
<input type="radio" name="choix" value="0" onclick="document.formulaire.submit()" /> Dinah<br/>
<input type="radio" name="choix" value="1" onclick="document.formulaire.submit()"/> Sassy<br/>
<input type="radio" name="choix" value="2" onclick="document.formulaire.submit()"/> Kirsten<br/>
<input type="radio" name="choix" value="3" onclick="document.formulaire.submit()"/> Iris<br/>
<input type="hidden" name="vote-fait" value="ok"/>

<?php
$vote['question'] = "Quel nom préférez-vous ?";
$vote['reponse'][0] = "Dinah";
$vote['reponse'][1] = "Sassy";
$vote['reponse'][2] = "Kirsten";
$vote['reponse'][3] = "Iris";
$vote['vote'][0] = "0";
$vote['vote'][2] = "0";
$vote['vote'][2] = "0";
$vote['vote'][2] = "0";

function afficherOptions()
{
global $vote;
$nbrElements = count($vote['reponse']);

if ( (!isset($_GET['vote-fait'])) || ($_GET['vote-fait'] != "ok") )
{
if ($nbrElements != count($vote['vote']) )
{
?>Problème : le sondage a été mal conçu.<?
}
else
{
?>
<form name="formulaire" method="get" action="<?=$_SERVER['PHP_SELF'];?>">
<input type="hidden" name="vote-fait" value="ok">
<b><?=$vote['question'];?></b><br>
<?
for ($i=0; $i < $nbrElements; $i++)
{
?><input type="radio" name="choix" value="<?=$i;?>" onClick="document.formulaire.submit();"><?
echo $vote['reponse'][$i]."<br>";
}
?>
</form>
<?
$vote_chaine = serialize($vote);
sauverDonnees($vote_chaine);
}
}
else
{
ajouterVote($_GET['choix']);
afficherResultats();
}
}

function afficherResultats()
{
global $vote;
$nbrElements = count($vote['reponse']);
$nbrVotes = array_sum($vote['vote']);

echo "<b>".$vote['question']."</b><br>";

for ($i=0; $i < $nbrElements; $i++)
{
?><?=round($vote['vote'][$i]*100/$nbrVotes, 2);?>% : <?=$vote['reponse'][$i];?> (<?=$vote['vote'][$i];?> vote(s) )<br><?
}
echo "Nombre de votes : $nbrVotes.";
}

$fichier = "donnees.txt"

function sauverDonnees($vote_chaine)
{
global $fichier;

if (!is_writable($fichier))
{
?>Ecriture impossible!<?
}
elseif (!$f = fopen($fichier,'w'))
{
?>Problème fichier!<?
}
elseif (!fwrite($f, $vote_chaine))
{
?>Problème lors de l'écriture.<?
}
else
{
fclose($f);
}
}

function ajouterVote($choix)
{
global $vote;
$vote['vote'][$choix] += 1;

$vote_chaine = serialize($vote);
sauverDonnees($vote_chaine);
}

function creerSondage()
{
global $fichier, $vote;

$vote['question'] = "Quel nom préférez-vous ?";
$vote['reponse'][] = "Dinah";
$vote['reponse'][] = "Sassy";
$vote['reponse'][] = "Kirsten";
$vote['reponse'][] = "Iris";
$vote['vote'][] = "0";
$vote['vote'][] = "0";
$vote['vote'][] = "0";
$vote['vote'][] = "0";

if ( count($vote['reponse']) != count($vote['vote']) )
{
?>Problème : le sondage a été mal conçu.<?
}
else
{
$vote_chaine = serialize($vote);
sauverDonnees($vote_chaine);
}
}

afficherSondage():
function afficherSondage()
{
global $fichier, $vote;
clearstatcache();

if ( (!file_exists($fichier)) || (filesize($fichier) < 1) )
{
creerSondage();
}
else
{
$vote = unserialize(file_get_contents($fichier));
}
afficherOptions();
}
?>

<?php afficherSondage(); ?>

1 réponse

  1. Bon déjà il faut utiliser les balises code pour que ça soit lisible. Plus d'infos sur la balise code.

    Ensuite la ligne 86 correspond à quelle ligne dans ton code?
    1. Mince je suis désolée, en plus je ne peux plus le changer, parce que je suis inscrite maintenant et la ligne 86 c'est function sauverDonnees($vote_chaine) juste après $fichier = "donnees.txt" (désolée encore)

      <div class="content2"> 
      <div class="form"> 
      <form name="formulaire" method="get" action="<?=$_SERVER['PHP_SELF'];?>"> 
      <fieldset> 
      <legend><em><strong>24-03-2017</strong></em></legend> 
      <p class="vote"> 
      <img src="images/chats-sphynx.jpg" class="chatflottant" /> 
      <img src="images/chaton-race-sphynx.jpg" class="chatflottant2" /> 
      Voter pour les noms de chats<br/> 
      <input type="radio" name="choix" value="0" onclick="document.formulaire.submit()" /> Dinah<br/> 
      <input type="radio" name="choix" value="1" onclick="document.formulaire.submit()"/> Sassy<br/> 
      <input type="radio" name="choix" value="2" onclick="document.formulaire.submit()"/> Kirsten<br/> 
      <input type="radio" name="choix" value="3" onclick="document.formulaire.submit()"/> Iris<br/> 
      <input type="hidden" name="vote-fait" value="ok"/> 
      
      <?php 
      $vote['question'] = "Quel nom préférez-vous ?"; 
      $vote['reponse'][0] = "Dinah"; 
      $vote['reponse'][1] = "Sassy"; 
      $vote['reponse'][2] = "Kirsten"; 
      $vote['reponse'][3] = "Iris"; 
      $vote['vote'][0] = "0"; 
      $vote['vote'][2] = "0"; 
      $vote['vote'][2] = "0"; 
      $vote['vote'][2] = "0"; 
      
      function afficherOptions() 
      { 
      global $vote; 
      $nbrElements = count($vote['reponse']); 
      
      if ( (!isset($_GET['vote-fait'])) || ($_GET['vote-fait'] != "ok") ) 
      { 
      if ($nbrElements != count($vote['vote']) ) 
      { 
      ?>Problème : le sondage a été mal conçu.<? 
      } 
      else 
      { 
      ?> 
      <form name="formulaire" method="get" action="<?=$_SERVER['PHP_SELF'];?>"> 
      <input type="hidden" name="vote-fait" value="ok"> 
      <b><?=$vote['question'];?></b><br> 
      <? 
      for ($i=0; $i < $nbrElements; $i++) 
      { 
      ?><input type="radio" name="choix" value="<?=$i;?>" onClick="document.formulaire.submit();"><? 
      echo $vote['reponse'][$i]."<br>"; 
      } 
      ?> 
      </form> 
      <? 
      $vote_chaine = serialize($vote); 
      sauverDonnees($vote_chaine); 
      } 
      } 
      else 
      { 
      ajouterVote($_GET['choix']); 
      afficherResultats(); 
      } 
      } 
      
      function afficherResultats() 
      { 
      global $vote; 
      $nbrElements = count($vote['reponse']); 
      $nbrVotes = array_sum($vote['vote']); 
      
      echo "<b>".$vote['question']."</b><br>"; 
      
      for ($i=0; $i < $nbrElements; $i++) 
      { 
      ?><?=round($vote['vote'][$i]*100/$nbrVotes, 2);?>% : <?=$vote['reponse'][$i];?> (<?=$vote['vote'][$i];?> vote(s) )<br><? 
      } 
      echo "Nombre de votes : $nbrVotes."; 
      } 
      
      $fichier = "donnees.txt" 
      
      function sauverDonnees($vote_chaine) 
      { 
      global $fichier; 
      
      if (!is_writable($fichier)) 
      { 
      ?>Ecriture impossible!<? 
      } 
      elseif (!$f = fopen($fichier,'w')) 
      { 
      ?>Problème fichier!<? 
      } 
      elseif (!fwrite($f, $vote_chaine)) 
      { 
      ?>Problème lors de l'écriture.<? 
      } 
      else 
      { 
      fclose($f); 
      } 
      } 
      
      function ajouterVote($choix) 
      { 
      global $vote; 
      $vote['vote'][$choix] += 1; 
      
      $vote_chaine = serialize($vote); 
      sauverDonnees($vote_chaine); 
      } 
      
      function creerSondage() 
      { 
      global $fichier, $vote; 
      
      $vote['question'] = "Quel nom préférez-vous ?"; 
      $vote['reponse'][] = "Dinah"; 
      $vote['reponse'][] = "Sassy"; 
      $vote['reponse'][] = "Kirsten"; 
      $vote['reponse'][] = "Iris"; 
      $vote['vote'][] = "0"; 
      $vote['vote'][] = "0"; 
      $vote['vote'][] = "0"; 
      $vote['vote'][] = "0"; 
      
      if ( count($vote['reponse']) != count($vote['vote']) ) 
      { 
      ?>Problème : le sondage a été mal conçu.<? 
      } 
      else 
      { 
      $vote_chaine = serialize($vote); 
      sauverDonnees($vote_chaine); 
      } 
      } 
      
      afficherSondage(): 
      function afficherSondage() 
      { 
      global $fichier, $vote; 
      clearstatcache(); 
      
      if ( (!file_exists($fichier)) || (filesize($fichier) < 1) ) 
      { 
      creerSondage(); 
      } 
      else 
      { 
      $vote = unserialize(file_get_contents($fichier)); 
      } 
      afficherOptions(); 
      } 
      ?> 
      
      <?php afficherSondage(); ?>
    2. @Cassie003Ok c'est simple, c'est juste un oubli de ; à la ligne juste au dessus, donc après $fichier = "donnees.txt"
    3. @Utilisateur anonymeEt ensuite il y a un soucis ici:

      afficherSondage():
      function afficherSondage()


      Utilisation de : au lieu de ;
    4. @Utilisateur anonymeMerci beaucoup!!! déjà ma page est revenue mais maintenant j'ai essayé de voter et il me dit "Notice: Undefined offset: 1 in /Applications/XAMPP/xamppfiles/htdocs/news.php on line 79 qui est
      <?=round($vote['vote'][$i]*100/$nbrVotes, 2);?>% : <?=$vote['reponse'][$i];?> (<?=$vote['vote'][$i];?> vote(s) )<br><?
    5. @Cassie003Ça vient de ça:

      $vote['vote'][0] = "0"; 
      $vote['vote'][2] = "0"; 
      $vote['vote'][2] = "0"; 
      $vote['vote'][2] = "0"; 


      Il y a trois fois l'index 2, alors qu'il suffit de faire ceci:

      $vote['vote'][] = 0; 
      $vote['vote'][] = 0; 
      $vote['vote'][] = 0; 
      $vote['vote'][] = 0; 


      Pour mettre des index automatiquement, voir une façon plus optimisée.

      $vote['vote'] = array_fill(0, 4, 0);


      Doc de array_fill