From the course: Learning PHP
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Writing custom 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. We're going to use one of my favorite examples from when I was in school and that is detecting if a string is a palindrome, that 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 built in PHP functions. Let's get started. So we'll write our opening PHP tag, and then we will name the function. We'll use the keyword function and we'll call this is palindrome. Since this is going to be a Boolean function, it's always good to name those functions the question they're answering. So this is answering is the string we're passing a palindrome? And then it's going to accept some string, which we will call string. Now the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
What are functions?5m 11s
-
(Locked)
Using built-in functions4m 40s
-
(Locked)
Writing custom functions7m 10s
-
(Locked)
Union typing variables2m
-
(Locked)
Optional or named arguments5m 23s
-
(Locked)
Anonymous functions4m 32s
-
(Locked)
What are objects and classes?3m 21s
-
(Locked)
Defining a simple person class (update for PHP 8.0)3m 19s
-
(Locked)
Using the person class3m 25s
-
(Locked)
Challenge: Sort an array of objects46s
-
(Locked)
Solution: Sort an array of objects3m 26s
-
-
-
-
-
-