The outcomes of programs we’ve been writing so far have been predetermined. Unless we manually change our code, it will produce the same results each time it’s run. But this isn’t very realistic. Programs often receive unexpected inputs or results which is why we need conditionals. Conditionals allow us to write flexible programs that handle this variability.

One common reason our programs need to be flexible is when they have user interaction. When we create a website, we don’t know exactly when a user will press a button or exactly what text they’ll input in a form. Writing programs that can handle unique user interaction is a big part of software development.

User interaction isn’t restricted to web development. We can enable user interaction in our terminal-based programs as well.

The built-in readline() function takes a string with which to prompt the user. It waits for the user to enter text into the terminal and returns that value as a string.

echo "Hi, I'm Aisle Nevertell. What's your name?\n"; $name = readline(">> "); echo "\nNice to meet you, $name";

The code above prints, Hi, I'm Aisle Nevertell. What's your name?. Then, it prints >> to the terminal to prompt the user to type and awaits their input which it will save in the $name variable. If the user entered Alex, for example, the program would next print Nice to meet you, Alex to the terminal.

By incorporating in conditionals, we can take different actions depending on the user input:

echo "\nWhat's your favorite color?\n"; $color = readline(">> "); if ($color === "green"){ echo "\nCool, that's my favorite too!"; } else { echo "\nOh, $color is nice, I guess…"; }

In the code above, we prompt the user to enter their favorite color. If they say our favorite color (green), we give one response, otherwise we give a different response.

Let’s practice!



In previous exercises that didn’t require user input, we ran your code for you. Now that you’re going to be using the terminal, you’ll also be running your own programs there. To run your program you’ll need to enter php src/index.php in the terminal.

We wrote the first line of a program in src/index.php. Run the program. You should see the text printed to the terminal.


Time to expand the program in src/index.php. Use the readline() function to prompt a user.

You can use whatever you want as the prompt string. We tend to use ">> " or "> " because we feel it gives the user a clear idea of where to type. But test things out and see what you like!

The user will be entering their first name. You should save this response in a variable.


Your program should handle three situations:

  • If the user’s name is greater than 8 characters, you should print "Hi, [THEIR NAME]. That's a long name."
  • If their name is between 4 and 8 characters (inclusive), you should print "Hi, [THEIR NAME].".
  • And if their name is 3 characters or fewer, you should print "Hi, [THEIR NAME]. That's a short name.".

Time to test your code. Run your code a couple times to make sure it’s working as expected.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?