...
/Discovering What Has Been Removed from the Core
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:
<?php// see: https://download.geonames.org/export/dump//*The main 'geoname' table has the following fields :---------------------------------------------------geonameid : integer id of record in geonames databasename : name of geographical point (utf8) varchar(200)asciiname : name of geographical point in plain ascii characters, varchar(200)alternatenames : alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000)latitude : latitude in decimal degrees (wgs84)longitude : longitude in decimal degrees (wgs84)feature class : see http://www.geonames.org/export/codes.html, char(1)feature code : see http://www.geonames.org/export/codes.html, varchar(10)country code : ISO-3166 2-letter country code, 2 characterscc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 200 charactersadmin1 code : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)admin2 code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)admin3 code : code for third level administrative division, varchar(20)admin4 code : code for fourth level administrative division, varchar(20)population : bigint (8 byte int)elevation : in meters, integerdem : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat.timezone : the iana timezone id (see file timeZone.txt) varchar(40)modification date : date of last modification in yyyy-MM-dd format*/// scans geonames database cities with > 1M in population$data_src = __DIR__ . '/sample_data/cities15000_min.txt';$fh = fopen($data_src, 'r');$pattern = "%30s : %20s\n";$target = 10000000;$data = [];while ($line = fgetcsv($fh, '', "\t")) {$popNum = $line[14] ?? 0;if ($popNum > $target) {// add lat/lon info to city$city = $line[1] ?? 'Unknown';$data[$city] = $line[4]. ',' . $line[5];}}fclose($fh);ksort($data);printf($pattern, 'City', 'Latitude/Longitude');printf($pattern, '----', '--------------------');while ([$city, $latLon] = each($data)) {$city = str_pad($city, 30, ' ', STR_PAD_LEFT);printf($pattern, $city, $latLon);}?>
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$line
and pack latitude and longitude information into a$data
array. 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 ...