Show what you know - HELP



Can someone please give me some hints and explain me what I do wrong for that exercice (without giving the solution, as I want to understand and know what I do wrong).

My code:

	// Créez un tableau avec les membres de votre famille ou amis
    $tabAmis = array ();
    array_push ($tabAmis, "Ami1");
    array_push ($tabAmis, "Ami2");
    array_push ($tabAmis, "Ami3");
    array_push ($tabAmis, "Ami4");
    array_push ($tabAmis, "Ami5");
    array_push ($tabAmis, "ami6");

	// Triez le tableau pour avoir les noms en ordre alphabétiques
	// Aléatoirement, sélectionnez un gagnant !
    $answer = substr($tabAmis, rand(0, strlen($tabAmis)-1), 1);
	// Affichez le nom du gagnant en majuscule !
	print strtoupper($answer);
  • It is say to use count so I added it but I don't know what to do with it.
  • it is say to use sort alphabetically but I have no idea how to do that.
  • for the answer, I use the code in the previous exercices (where you have to pick one letter of your name and try to change it to make it work with that exercice).

I guess all is wrong. But really I am kind of really lost.

Can someone help me please, by giving me hint of what is wrong, and what I do wrong?


Any help, please ? :pray:🏼


The problem you are having here is that your code is incorrect. after ...sort($tabAmis) is incorrect. (you don't need the count($tabAmis) either but changing it won't do anything to your code). The problem is:

$answer = substr($tabAmis, rand(0, strlen($tabAmis)-1), 1);.

But don't panic instead of writing it like that. write it like this:

$answer = $tabAmis[rand(0, count($tabAmis) - 1)];
Hope this helps.


Thank you very much for the help and hints.

Would you please explain me then why you use [ ] for the $answer ? I am wondering in which case it should be used. Maybe just because there are already several ( ) ?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.