PHP print names in upper case

Asked

Viewed 90 times

0

I’m a beginner in programming, I’ve been studying the PHP manual to better understand how the language works.

I have to do the following exercise: Print students' names in upper case in order of enrollment.

This is my code:

$nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);
asort($nome_alunos);
strtoupper($nome_alunos);
foreach ($nome_alunos as $key => $value) {
  echo "$key = $value\n";

I can’t capitalize on the names using strtoupper() that are inside the array, I was only able to put in order, I also read the manual of PHP and did not find a function that helps me beyond the one described above. Can someone help me?

2 answers

4


You are trying to convert the $student name array, but you must convert every $key within the loop

<?php
$nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);
asort($nome_alunos);

foreach ($nome_alunos as $key => $value) {
   $nome_aluno = strtoupper($key);
   echo "$nome_aluno = $value<br>";
}
?>
  • Thanks for the explanation was of great help to understand my mistake.

2

You need to use the function strtoupper() at each value. Below is the code to do this.

<?php
    $nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);

    asort($nome_alunos);
    foreach ($nome_alunos as $key => $value)
        echo strtoupper($key)." = $value<br>";
?>

If you want to leave only the first uppercase letter, you can do it as follows:

echo ucwords(strtolower($key)). " = value<br>";
  • Phelipe worked right, thanks for the help!.

Browser other questions tagged

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