Finding a character at a specific position of a string

前端 未结 5 1677
眼角桃花
眼角桃花 2021-01-05 14:28

Since I am still new to PHP, I am looking for a way to find out how to get a specific character from a string.

Example:

$word = \"master\";
$length =         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 14:53

    Try this simply:

    $word = "master";
    $length = strlen($word);
    $random = rand(0,$length-1);
    
    if($word[$random] == 's'){
     echo $word[$random]; 
    }
    

    Here I used 0 because $word[0] is m so that we need to subtract one from strlen($word) for getting last character r

提交回复
热议问题