php 将函数结果的首字母大写

php Making first letter of a function result uppercase

我有一个功能,returns我一个字。我希望这个词的首字母大写。

$parts = array ("ing", "er", "a", "on", "po", "i", 
    "re", "tion", "con");


function getWord ($parts)  {
    //getting number of array elements
    $result = count($parts)-1;

    $x = mt_rand(0, $result);
    $y = mt_rand(0, $result);
    $z = mt_rand(0, $result);

     $oneSyl = $parts[$x];
     $twoSyl = $parts[$x].$parts[$y];
     $threeSyl = $parts[$x].$parts[$y].$parts[$z];

    //creating an array of 1,2,3 syllable words
     $newWord = array ($oneSyl, $twoSyl, $threeSyl);

    // getting a number from 0 to 2 
     $randLength = mt_rand(0, 2);

    echo $newWord[$randLength];

    }

getWord($parts);

我试过ucfirst(),但它需要一个字符串值,而我只有一个函数。 如何将 ucfirst() 添加到函数中?或者我怎样才能得到第一个字符是大写的单词

谢谢。

只需要你这样做

echo ucfirst($newWord[$randLength]);
$parts = array ("ing", "er", "a", "on", "po", "i", 
    "re", "tion", "con");


function getWord ($parts)  {
    //getting number of array elements
    $result = count($parts)-1;

    $x = mt_rand(0, $result);
    $y = mt_rand(0, $result);
    $z = mt_rand(0, $result);

     $oneSyl = $parts[$x];
     $twoSyl = $parts[$x].$parts[$y];
     $threeSyl = $parts[$x].$parts[$y].$parts[$z];

    //creating an array of 1,2,3 syllable words
     $newWord = array ($oneSyl, $twoSyl, $threeSyl);

    // getting a number from 0 to 2 
     $randLength = mt_rand(0, 2);
//edited
    echo ucfirst($newWord[$randLength]);

    }

getWord($parts);

据我所知 you.You 可以添加一行:

$parts = array ("ing", "er", "a", "on", "po", "i", 
"re", "tion", "con");


function getWord ($parts)  {
//getting number of array elements
$result = count($parts)-1;

$x = mt_rand(0, $result);
$y = mt_rand(0, $result);
$z = mt_rand(0, $result);

 $oneSyl = $parts[$x];
 $twoSyl = $parts[$x].$parts[$y];
 $threeSyl = $parts[$x].$parts[$y].$parts[$z];

//creating an array of 1,2,3 syllable words
 $newWord = array ($oneSyl, $twoSyl, $threeSyl);

// getting a number from 0 to 2 
 $randLength = mt_rand(0, 2);

//Here is the modified line
echo ucfirst($newWord[$randLength]);

}

getWord($parts);