- [David] Hi, I'm David Powers, and welcome to this edition of PHP Tips and Tricks, designed to help you become a smarter, more productive PHP developer. Variables can be used to store all kinds of values. Strings, arrays, objects, and so on. But did you know you can also use a variable as a function? In this edition of PHP Tips and Tricks, I want to show you how to simplify a script when you don't know in advance which function will be needed. By the way, you can follow along with the exercise files provided with this video.
The real payoff of working with variables comes when you're dealing with values that aren't necessarily known in advance. For example, if you're manipulating a series of images, they could be of different MIME types. The PHP functions for handling images are specific to each MIME type. This is the PHP online manual for GD and image functions. If I scroll down, the functions for creating image resources all begin the same way. We've got imagecreatefromgif, imagecreatefromjpeg, and imagecreatefrompng.
And the functions for outputting the different MIME types follows the same pattern. If we scroll down a little bit further, we've got imagegif, imagejpeg, and down here imagepng. If you're dealing with a batch of images that are all of the same MIME type, this isn't a problem. But if they're mixed, you need to choose the right function each time. Let's switch to my editing program.
One solution is to use a switch statement like this, but you need two of them. One to create the image resource, and this other one down here to output the edited image. It works but it really is cumbersome. But the consistent naming pattern of these functions allows us to simplify the script with variable functions. In my testing site I've got a folder that contains four images of national flags.
The first one is a gif, then we've got two pngs, and the Stars and Stripes is a jpeg. As a simple demonstration of variable functions, I'm going to rotate each of these images by 90 degrees. I've got the script in this other file here, variable_functions.php. On line two I'm using a file system iterator from the standard PHP library to loop through the images directory. And inside the loop we get the MIME type from the current image using getimagesize.
Notice that I'm using a ray-D referencing, the technique that I demonstrated in the previous PHP Tips and Tricks. All image MIME types begin with image/. We need only the part after the /, so we extract that with substring and store it in a variable called mime. And I'm echoing mime on line eight. So let's test that by loading this file into a browser. And there's the result, we get gif, png, png, jpeg.
Now that I've got the MIME types, I can create variable functions from them. Let's quickly review how variable functions work. If my first image is a jpeg, the value of the MIME variable will be jpeg. To create a variable function, you simply assign the name of the function as a string to a variable. Here I'm using a double-quoted string with the mime variable embedded in it. So this has the effect of assigning imagecreatefromjpeg to a variable called createResource.
But if mime is png, createResource is imagecreatefrompng. You can then use the variable function just like any other function by adding a pair of parentheses after the variable name and pass it any arguments as with an ordinary function. So let's get back to the editing program. So I no longer need this line here, line eight. I'm going to comment that out and then un-comment the rest of my script down at the bottom also, un-comment that.
So we need two functions, one to create an image resource and the other to output the modified image. The first one is here on line 10, createResource, and the other one on the following line, outputImg. Both are assigned with a double-quoted string with the mime variable embedded in it. The first variable function creates an image resource on line 13. The image is then rotated and then the second variable function is used to output the image as the correct MIME type, using the same file name prefixed with rotated_.
Finally, both image resources are destroyed to free up memory and we simply echo "Done" to say that the script has completed running. So if we load this into a browser it tells us that we're done, and if we look at my testing site, there in my images folder are the four rotated images. So that's a practical example of using variable functions to simplify a script, rather than using multiple switch statements.
To create a variable function, you store the function name as a string in a variable. You can then treat the variable as a function by appending a pair of parentheses to the variable and passing it arguments in the normal way. In effect, the variable function acts as an alias for the name function. Well, that's it for this week's PHP Tips and Tricks. I hope you found it useful. Until next time, this is David Powers thanking you for watching.
Note: The exercise files are free to all members. The code is commented to enhance your learning, but you will need database connectivity for some files to run as intended.