Tech

I’m using ChatGPT to help me fix my code faster, but at what cost?


ChatGPT on laptop

Photo by Frank Rumpenhorst / image alliance via Getty Images

Earlier this week, I showed you how ChatGPT Can Create Entire WordPress Plugins met a specific, if somewhat rudimentary, functional spec. Now, I’m continuing to test ChatGPT to see if it could be a useful tool — in this case, to get me some technical guidance.

Also: I asked ChatGPT to write a WordPress plugin. It did it in less than 5 minutes

As I’ve done in previous ChatGPT explorations, my posts are in lowercase or bold and the AI ​​responses are in italic. First, I’ll show you the questions and answers, and then I’ll discuss the implications.

Understanding programming language changes

My hosting provider is migrating all their websites from very outdated PHP 7.4 to PHP 8. PHP is a web programming language which WordPress and several other web management tools rely on. I have a couple of plugins that are not compatible with PHP 8 and I want to determine what aspects of their code I will have to find and fix.

Also: Can the Al Detector save us from ChatGPT? I tried 3 online tools to find out

Normally, I would dig into PHP.net website, do a bunch of Google searches, and dig through the comments in StackExchange to find the programming work I’ll need to do. But it’s a messy, time-consuming process. Instead, I asked a detailed, direct question about ChatGPT and got a very helpful answer, including illustrative code blocks.

I’m having an issue with a PHP extract function that gives me a fatal error when not passing an array. In PHP 7.4 I don’t get the error, but it seems to be in PHP 8. Is unpacking changed for PHP 8?

In PHP 8, the behavior of extract() function has been changed to be more strict. In previous versions of PHP, extract() The function will only return false if it is not passed to an array, but in PHP 8 it will throw a fatal error if it is not passed in an array. This change was made to improve the robustness of the functionality and to help prevent potential errors caused by unexpected input.

You can check if the variable passed to the function is an array using is_array() function before calling extract().

image-1

ChatGPT

You can also use array_key_exists() The function checks if a passed variable is an array or not.

image-2

ChatGPT

Alternatively, you can use operator to destructure arrays into variables.

picture-3

ChatGPT

Turns out, ChatGPT’s answer was extremely helpful and I fixed that in a few minutes. An important part of being able to fix an error is knowing where to look. ChatGPT has clarified how behavior has changed in function between PHP versions.

Since I’m trying to future-proof an entire plugin that I’m porting to a new version, I asked this as well:

Which 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 are likely to cause problems:

  • The extract() function, as mentioned in previous answer, now throws a fatal error if it is not passed an array.
  • The each() was removed in PHP 8.0. You can use for every or array_keys() And current() instead of.
  • The create_function() was removed in PHP 8.0. Instead, anonymous functions should be used.
  • The list() construction, when used with for everynow returns the key and value as separate variables, instead of a single array.
  • The __autoload() was removed in PHP 8.0. You should use spl_autoload_register() instead of.
  • The $php_errormsg variable has been removed in PHP 8.0. You should use error_get_last() replacement function.
  • Some bugs that were deprecated in previous PHP versions have now been removed in PHP 8.0. Specifically, the create_function(), each(), __autoload()And $php_errormsg be eliminated.

These are just a few examples of changes that can cause problems when migrating from PHP 7.4 to PHP 8. You should double-check your code and application before upgrading to make sure that everything works as it should. expectation.

Note that ChatGPT used the phrase “as mentioned in the previous answer.” It can keep track of the replies in the chat. That’s both “wow” and scary.

Also: ChatGPT productivity tips: Five ways to use chatbots to make your life easier

In the code I was modifying, it turned out that the original developer (who had been out of the picture for a long time) used the constructor a lot. I didn’t realize that was deprecated. But with the helpful list above, I just searched the code and found what needed updating and rewriting. Then I went ahead and asked ChatGPT to show me how to replace that functionality.

What is the best way to rewrite code using constructor in PHP? Can you give me an example using the constructor and then an example using the current matching mechanism?

create_function is a deprecated function in PHP used to create anonymous functions. This function should not be used anymore as it was removed in PHP 8.0. Instead, anonymous functions, also known as closures, can be created using function key word.

Here is an example of using create_function:

image4

ChatGPT

And here is the equivalent code using anonymous function:

image5

ChatGPT

Alternatively, you can use the use keyword to pass variables into the closure.

image6

ChatGPT

In general, anonymous functions should be used instead of create_function as they have improved performance and better readability.

As it turned out, it 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 Technical Support

When I was researching my article about the dangers of Optimize Mac Storage, I also have some questions. As my article pointed out, I went in and did some testing to validate some of my concerns. But to quickly narrow down my research, I asked ChatGPT this:

If you use MacOS’s Optimized Space feature, will Time Machine perform incomplete backups since some files are represented only 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 create a backup, it backs up all files, including those that have been offloaded using “Optimize Mac Space”. However, stub files that represent offloaded files will be backed up in place of the original files.

This means that if you restore your Mac from a Time Machine backup created while “Optimize Mac Space” is enabled, any offloaded files will not be included in the restore process and will not be included in the recovery process. needs to be re-downloaded from iCloud.

It’s important to note that if you have a lot of files offloaded, your Time Machine backups may take up more space than if you didn’t use Optimize Mac Storage because Time Machine backs up the original files. open, takes up less space. space but Time Machine will treat it as the original file.

So, in short, Time Machine will create incomplete backups if you use the “Optimize Mac space” feature, as it will only back up stub files instead of the original files uploaded to the cloud.

That is a correct answer.

Implications

Yes, it certainly saves time. Usually, programming on the internet involves a dance between coding and debugging environments, and lots of incremental web searches where clues are followed and discussions and comments are read. .

Do not misunderstand me. That process can be very informative, and there is a lot of folklore hidden in the comments that you would never get from a chatbot. But that’s not an efficient use of time when you’re in a hurry or are on a deadline. ChatGPT’s asking those encrypted questions proved to be very effective.

Also: ChatGPT and Bard: Are we looking for answers in the wrong place?

As with anything about AI, you have to be aware that there are gaps in AI knowledge and you will most likely get completely wrong answers. But using ChatGPT as a simple tool like any other in my toolbox proved to be quite effective.

However, I wonder where it gets its information from. Has any writer like me written an informative article about decompression or function generation whose words are becoming”spun into yarn” into ChatGPT responses with no credit or monetization? Is someone’s hard-won knowledge being used as unaccredited source material for those replies?

I worry about that. At least with Google, authors have a chance to have the search engine send traffic to the article in exchange for sharing that information. But with a chatbot like ChatGPT, the original flow is lost. Sure, I got the productivity benefit of a quick answer. But is there someone out there who has lost credit for their work?

Also: How to start using ChatGPT

Keep stable. This spike in general AI is making big waves. Stick with me and ZDNET as we try to understand what it means for all of us.

ChatGPT, when you answer a question, are you using the knowledge others have contributed and are you taking away work and credit from the original human sources?

As an AI language model created by OpenAI, I have no personal views or thoughts. However, I can tell you that the information I used to create my answers was based on data I was trained on, including a large amount of text from the internet. This document was written by many different people and sources and I am not able to credit specific individuals for the information I present. My goal is to provide useful and accurate information to assist users in finding the answers they are looking for. It is important to remember that my responses are generated based on patterns in the data I have been trained with and may not always be completely accurate or up to date.

So yes. That thing. “Large amount of text from the internet.” Move with everyone. Nothing to see here.


You can follow my daily project updates on social media. Remember to follow me on Twitter at @DavidGewirtzon Facebook at Facebook.com/DavidGewirtzon Instagram at Instagram.com/DavidGewirtzand on YouTube at YouTube.com/DavidGewirtzTV.

news7g

News7g: Update the world's latest breaking news online of the day, breaking news, politics, society today, international mainstream news .Updated news 24/7: Entertainment, Sports...at the World everyday world. Hot news, images, video clips that are updated quickly and reliably

Related Articles

Back to top button