Students using Windows computers will notice quickly that their terminals are handling PHP's Interactive Shell differently than described in the curriculum. This is an unfortunate side-effect of Window's Terminal not having 'readline' functionality. You can achieve the same results as described in the curriculum, but instead of starting the interactive shell with the command: php -a you'll have to wrap your content like this: <?php [your content] ?> every time you want to use interactive mode.
As a result of this being a particularly slow process, it may be worth trying this feature out and moving back to your text editor to complete the following lessons.
Arithmetic and gluing strings together with operators is very useful, but what if we want to do something more complicated like build a program that translates sentences into pig latin? To do that you have to learn about functions.
A function is a word which defines an action. It takes some kind of input, and returns some kind of output. For example, the function
strlen takes a string as an input, and returns the length of the string as the output. And the function
round takes a number with a decimal as an input, and returns the number rounded to the nearest whole number as the output.
Let's try some simple functions out without having to build an entire website. Open your terminal (or command line in windows), type this command and then hit Enter:
Now we are in the PHP Interactive Shell. This is kind of like a test kitchen where we can just throw commands directly at the PHP Pastry Chef to see how they work. You should see something that looks like:
I will shorten this to just:
To leave the PHP shell, just type
exit and hit Enter.
Now, type this command and press Enter (everything on the first line after the
> echo strlen("Charlie Parker"); 14
The result is
14. As I mentioned before
strlen is a function which calculates the number of characters in a string, including spaces. We told it to calculate the number of letters in "Charlie Parker", so it gave us back the value
echo keyword is what prints that result into the terminal on the next line.
Let's try another function - this one is called
round. It takes a number as its input, then rounds it up or down to the nearest integer, and gives us that number back as output. Type this in:
> echo round(4.8); 5 > echo round(2.1); 2
We can also save the output of a function into a variable to use later, like this:
> $length = strlen("Bruce Wayne"); > echo $length; 11 > $rounded_integer = round(7.9); > echo $rounded_integer; 8
Let's look at an example that combines a few things we've learned: variables, functions, and operators. As you follow along, type each line (everything after the
>) and press Enter:
> $input_word = "stressed"; > $output_word = strrev($input_word); > echo $input_word . " reversed is " . $output_word; "stressed reversed is desserts"
Here is what it's doing, line by line:
$input_wordinto the function
strrevand assign its output to the variable
strrevtakes a string as input and reverses it to produce a new string as output.
Here are a few important terms to remember:
When you're reading your code, you should read a function call from right to left. You pass input to a function by putting arguments inside of the parenthesis to the right of the function's name. Then the function does its work and returns a value, which you can visualize as coming out of the function's left side. You would use an
= on the left of the function to assign that return value to a variable, as we did in our previous example. Or you can print the return value by putting the word
echo to the left of the function, as we did in our first example.
A function can also have multiple arguments. For example, the function
str_repeat takes a string and repeats it some number of times. So it needs 2 arguments - the string to repeat, and the number of repeats.
If your boss told you to make a bunch of photocopies of a document, what would you need to ask? You'd have to ask how many copies to make, and you'd need the document to copy. You are now behaving like a function (good work!) and those are your arguments. If you are not given either of those arguments, then you won't be able to do your job. In PHP, if you give a function more than the required number of arguments, the arguments will be read from left to right and extra ones will be ignored.
str_repeat looks like:
> echo str_repeat("I am a function! ", 3); I am a function! I am a function! I am a function!
Multiple arguments are separated by commas within the function's parenthesis. In this case the first argument is the string to repeat and the second is the number of repetitions.
Let's try it out in a website next. First, we'll make a form which lets the user decide what string they want repeated, and how many times to print it. We can call this file
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <title>String Repeating</title> </head> <body> <div class="container"> <h1>Hi! I'm a parrot website. I'll repeat back what you say!</h1> <form action="repeater.php"> <div class="form-group"> <label for="repeat_text">What would you like me to say?</label> <input id="repeat_text" name="repeat_text" class="form-control" type="text"> </div> <div class="form-group"> <label for="number_of_repeats">How many times should I say it?</label> <input id="number_of_repeats" name="number_of_repeats" class="form-control" type="text"> </div> <button type="submit" class="btn-success">Submit</button> </form> </div> </body> </html>
So, now let's create the PHP file to go with our form. We'll call it
repeater.php, to match the URL defined in the form's
<?php $repeat_text = $_GET["repeat_text"]; $number_of_repeats = $_GET["number_of_repeats"]; $repeated_text = str_repeat($repeat_text, $number_of_repeats); ?> <html> <head> <title>String Repeating</title> </head> <body> <h1>Here ya go!</h1> <p><?php echo $repeated_text; ?> </p> </body> </html>
Again, we are getting the values for each input field in our form by using their name attributes with
$_GET. Then we put those values into two variables,
$number_of_repeats, and then we use those two variables as arguments to the
str_repeat function. Finally, the output from the function goes into the variable
$repeated_text, which is displayed in the HTML.
It's time for you to play with some functions! Create a website with a form in it for the user to enter four
strings of text.
strtoupperto make it uppercase and print it to the screen. It takes a string as its only argument.
str_word_countto count the number of words in the second string. Then print that to the screen. It also takes a string as its only argument.
str_shuffleto shuffle the order of the characters in the third string. It also takes a string as its only argument.
striposto output the numbered position (called the index) of the first occurrence of the word "you" (or another word of your choice). It takes two arguments - the first one is the string we are searching in, and the second one is the string we are searching for. For example, if you type
stripos("How are you?", "you");It will give you the answer
8because the word "you" starts on the 8th character in the phrase (counting from 0, including spaces). "H" is at index 0, "o" is index 1, "w" is index 2, etc. Most everything in programming counts from 0 instead of 1. Don't worry, you'll get used to it.