How to use the rand() function on a string?


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

```

Replace this line with your code.

<do not remove the three backticks above>

#2

We will need the three tools you have learned in this lesson.

substr
strlen
rand

Given a name,

$name = "Wee Gillis";

Let’s say we want the fifth letter, bearing in mind zero-indexing of strings…

$char = substr($name, 4, 1);    // -> G

Now replace the 4 with the random code to get a letter from your name.

rand(0, strlen($name) - 1)

#3

2 posts were split to a new topic: Doing the right calculation I still get the error


#4

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