php使函数结果的第一个字母大写

时间:2016-01-15 19:04:21

标签: php uppercase

我有一个函数可以给我一个字。我希望这个单词的第一个字母是大写的。

-p

我试过$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()添加到函数中?或者如何得到第一个字符为大写的单词

谢谢。

3 个答案:

答案 0 :(得分:1)

只是你需要这样做

echo ucfirst($newWord[$randLength]);

答案 1 :(得分:0)

$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);

答案 2 :(得分:0)

从我得到你。你可以添加一行:

$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);