Discovering What Has Been Removed from the Core
Learn about the functions that have been removed or deprecated in PHP 8.
Examining functions removed in PHP 8
There are a number of functions in the PHP language that have only been retained thus far in order to maintain backward compatibility. However, maintenance of such functions drains resources away from core language development. Further, for the most part, such functions have been superseded by better programming constructs. Accordingly, there has been a slow process whereby such commands have been slowly dropped from the language as evidence has mounted that they are no longer being used.
The table shown next summarizes the functions that have been removed in PHP 8 and what to use in their place:
PHP 8 Removed Functions and Their Replacements
Removed Function | Suggested Replacement |
|
|
|
|
|
|
|
|
| none other than running an OS command |
|
|
| none: the magic quotes feature itself has been removed from PHP |
|
|
|
|
|
|
|
|
In the next sections, we cover a few of the more important removed functions and give suggestions on how to refactor our code to achieve the same results. Let’s start by examining each().
Working with each() method
each() was introduced in PHP 4 as a way of walking through an array, producing key/value pairs upon each iteration. The syntax and usage of each() is extremely simple and is oriented toward procedural usage. We’ll show a short code example that demonstrates each() usage as follows:
Let’s get into the code.
Lines 27–31: We first open a connection to a data file containing city data from the GeoNames project.
Lines 32–39: We then use the
fgetcsv()function to pull a row of data into$lineand pack latitude and longitude information into a$dataarray. Note in the code snippet that we filter out rows of data on cities with a population of less than$target(in this case, less than 10 million).Lines 40–47: We then close the file handle and sort the array by city name. To present the output, we use
each()to walk through the array, producing key/value pairs, where the city is the key and latitude and longitude is the value.
This code example won’t work in PHP 8, however, because each() has been removed. The best practice ...