Earlier this week, I showed you how ChatGPT could create an entire WordPress plugin that met a specific, if somewhat rudimentary, functional spec. Now, I’m continuing my testing of ChatGPT to get a feel for whether it can be a helpful tool — in this case, to help me get some technical guidance.
As I’ve done in previous explorations of ChatGPT, my writing is in plain or bold text, and the AI’s responses are in italic type. First, I’ll show you the questions and answers, then I’ll discuss implications.
Understanding programming language changes
My hosting provider is moving all its sites from the very outdated PHP 7.4 to PHP 8. PHP is a web programming language that WordPress and a number of other web management tools rely upon. I had a couple of plugins that were not PHP 8 compatible, and I wanted to identify what aspects of their code I would have to find and fix.
Normally, I’d dig into the PHP.net site, do a bunch of Google searches, and dig around the comments in StackExchange to find out what programming work I would need to do. But that’s a bit of a messy, time-consuming process. Instead, I asked a detailed, direct question of ChatGPT and got back a very useful response, including blocks of illustrative code.
I’m having trouble with the PHP extract function giving a fatal error when not passed an array. In PHP 7.4, I did not get the error, but I seem to in PHP 8. Did extract change for PHP 8?
In PHP 8, the behavior of the extract() function has been changed to be more strict. In previous versions of PHP, the extract() function would simply return false if it was not passed an array, but in PHP 8, it will throw a fatal error if it is not passed an array. This change was made to improve the robustness of the function and to help prevent potential bugs caused by unexpected input.
You can check if the variable passed to the function is an array or not by using the is_array() function before calling extract().
You can also use the array_key_exists() function to check if a variable passed is an array or not.
Alternatively, you can use the … operator to destructure the array into variables.
As it turned out, ChatGPT’s answer was extremely helpful, and I had that bug fixed in a few minutes. A big part of being able to fix that bug was knowing where to look. ChatGPT clarified how the behavior changed in the function between versions of PHP.
Since I was trying to future-proof the entire plugin I was moving to the new version, I also asked this:
What PHP functions are likely to break when moving from PHP 7.4 to PHP 8?
When moving from PHP 7.4 to PHP 8, there are a few functions and features that may break or behave differently. Here are some of the most notable changes that could potentially cause issues:
- The extract() function, as mentioned in previous answer, now throws a fatal error if it is not passed an array.
- The each() function has been removed in PHP 8.0. You can use foreach or array_keys() and current() instead.
- The create_function() function has been removed in PHP 8.0. It is recommended to use anonymous functions instead.
- The list() construct, when used with foreach, now returns the key and value as separate variables, rather than a single array.
- The __autoload() function has been removed in PHP 8.0. You should use spl_autoload_register() instead.
- The $php_errormsg variable has been removed in PHP 8.0. You should use the error_get_last() function instead.
- Some of the deprecations in previous PHP versions are now removed in PHP 8.0. Specifically, the create_function(), each(), __autoload(), and $php_errormsg are removed.
These are just a few examples of the changes that may cause issues when moving from PHP 7.4 to PHP 8. It’s a good idea to thoroughly test your code and applications before upgrading to ensure that everything works as expected.
Note that ChatGPT used the phrase “as mentioned in the previous answer.” It’s able to keep track of answers in the conversation. That’s both “wow” and scary.
In the code I was revising, it turned out that the original developer (who is long out of the picture) used create_function a lot. I didn’t realize that had been deprecated. But with the useful list above, I just searched the code and found what needed to be updated and rewritten. I then went ahead and asked ChatGPT to show me ways to replace that function.
What’s a good way to rewrite code using create_function in PHP? Can you give me an example using create_function, and then one using the currently proper mechanism?
create_function is a deprecated function in PHP that was used to create anonymous functions. It is no longer recommended to use this function as it has been removed in PHP 8.0. Instead, anonymous functions, also known as closures, can be created using the function keyword.
Here is an example of using create_function:
And here is the equivalent code using an anonymous function:
Additionally, you can use the use keyword to pass variables into the closure.
In general, it is recommended to use anonymous functions instead of create_function as they have improved performance and better readability.
As it turns out, that was a very clear explanation and I was able to quickly rewrite the code appropriately. That plugin is now running in my hosting provider’s new PHP 8 environment without issue.
MacOS tech support
As I was researching my article about the dangers of Optimize Mac Storage, I also had some questions. As my article showed, I went in and did some testing to validate some of my concerns. But as a quick way of narrowing down my research, I asked ChatGPT this:
If you use the MacOS feature Optimize Mac Storage, will Time Machine make incomplete backups because some files are only represented as stubs on the local machine?
The “Optimize Mac Storage” feature in macOS uses iCloud to offload rarely used files from your Mac to the cloud, while keeping a “stub” file on your local machine to represent the original file. This can save space on your Mac’s hard drive.
When Time Machine is set up to make backups, it will back up all files, including those that have been offloaded using “Optimize Mac Storage.” However, the stub files that represent the offloaded files will be backed up in place of the original files.
This means that if you were to restore your Mac from a Time Machine backup that was made while “Optimize Mac Storage” was enabled, any offloaded files would not be included in the restore and would need to be downloaded from iCloud again.
It’s important to note that if you have a lot of files that are offloaded, your Time Machine backups can take up more space than if you don’t use Optimize Mac Storage because Time Machine will be backing up the stub files, which take up less space but Time Machine will treat as the original files.
So, in short, Time Machine will make incomplete backups if you use the “Optimize Mac Storage” feature, as it will only back up the stub files instead of the original files that have been offloaded to the cloud.
That was a correct answer.
Well, it certainly saved time. Normally, programming on the internet involves a dance between the coding and debugging environment, and a lot of incremental web searches where clues are tracked down and discussions and comments are read.
Don’t get me wrong. That process can be very informative, and there’s a lot of folklore buried in those comments you’ll never get from a chatbot. But it’s not an efficient use of time when you’re in a hurry or on a critical deadline. Asking those coding questions of ChatGPT proved to be very efficient.
As with anything AI, you have to be aware that there are gaps in the AI’s knowledge, and that you very well might get completely wrong answers. But using ChatGPT as a straightforward tool like any other tool in my toolbox proved quite productive.
I do wonder where it got its information, though. Is there some writer out there like me who wrote a deeply informative article about extract or create_function whose words are getting “spun” into a response by ChatGPT without credit or monetization? Is someone’s hard won knowledge being used as uncredited source material for those answers?
I do worry about that. At least with Google authors stand a chance of having the search engine send traffic to the article in return for sharing that information. But with a chatbot like ChatGPT, the original thread is lost. Sure, I got the productivity benefit of a quick answer. But did someone out there lose attribution for their work?
Stay tuned. This surge in generative AI is making big waves. Stick with me and ZDNET as we try to wrap our heads around what it means for all of us.
ChatGPT, when you answer a question, are you taking knowledge others have contributed and are you taking work and credit away from the original human sources?
As an AI language model created by OpenAI, I do not have personal opinions or thoughts. However, I can tell you that the information I use to generate my responses is based on the data I have been trained on, which consists of vast amounts of text from the internet. This text was written by many different people and sources, and I don’t have the ability to attribute credit to specific individuals for the information I present. My goal is to provide helpful and accurate information to assist users in finding the answers they’re looking for. It’s important to remember that my responses are generated based on patterns in the data I was trained on and may not always be completely accurate or up-to-date.