From the course: PHP for WordPress

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

Writing your own functions

Writing your own functions

From the course: PHP for WordPress

Writing your own functions

- [Instructor] Now that you know a bit about functions, let's walk through writing our own, something that can actually work in a real program instead of just in the abstract. The function that we're going to write is going to detect if a string is a palindrome. A palindrome is a string that reads the same backwards and forwards. This will allow us to accomplish a few objectives: create a Boolean function, pass an argument to that function, and use some built-in PHP functions. Let's get started. So we'll start by using the function keyword, and since this is going to be a Boolean function, we're going to call it is_palindrome and it's going to accept one argument and we will call that argument string. We'll add our curly braces and then we'll do the check. Now, the simplest way to do a check for a palindrome is to reverse the string. And luckily, there is a built-in PHP function to do that. So let's create a variable called pal_check and we'll do the comparison right in line. So we'll…

Contents