How to get the location from an IP address with PHP?

Sometimes, we want to get the location from an IP address with PHP.

In this article, we’ll look at how to get the location from an IP address with PHP.

How to get the location from an IP address with PHP?

To get the location from an IP address with PHP, we can use a web API.

For instance, we write

$location = file_get_contents(
    "http://freegeoip.net/json/" . $_SERVER["REMOTE_ADDR"]
);
print_r($location);

to call file_get_contents to make a get request to "http://freegeoip.net/json/" . $_SERVER["REMOTE_ADDR"].

$_SERVER["REMOTE_ADDR"] has the IP address of the client that made the request.

Then we get the $location response array.

It includes the country, city, zip code, latitude, and longitude data.

Conclusion

To get the location from an IP address with PHP, we can use a web API.

How to get the last ID inserted with PDO and PHP?

Sometimes, we want to get the last ID inserted with PDO and PHP.

In this article, we’ll look at how to get the last ID inserted with PDO and PHP.

How to get the last ID inserted with PDO and PHP?

To get the last ID inserted with PDO and PHP, we can use the lastInsertId method.

For instance, we write

$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();

to create a prepared statement object with $db->prepare called with a insert statement SQL string.

Then we call execute to run the statement.

Next, we call $db->lastInsertId to return the ID of the last inserted entry.

Conclusion

To get the last ID inserted with PDO and PHP, we can use the lastInsertId method.

How to call a function from a string stored in a variable with PHP?

Sometimes, we want to call a function from a string stored in a variable with PHP.

In this article, we’ll look at how to call a function from a string stored in a variable with PHP.

How to call a function from a string stored in a variable with PHP?

To call a function from a string stored in a variable with PHP, we can use a variable as the function name.

For instance, wewrite

$functionName()

to call the $functionName function.

$functionName is a string with the name of the function.

We can also use the call_user_func function to do the same thing by writing

call_user_func($functionName)

Conclusion

To call a function from a string stored in a variable with PHP, we can use a variable as the function name.

How to get the current date and time in PHP?

Sometimnes, we want to get the current date and time in PHP.

In this artricle, we’ll look at how to get the current date and time in PHP.

How to get the current date and time in PHP?

To get the current date and time in PHP, we can use the date function.

For instance, we write

$date = date('Y-m-d H:i:s');

to call date with a format string to return a date string in the given format.

Y is the year. m is the month. d is the day.

H is the hour. i is the minute. s is the second.

This will return current date time at the server’s local time zone.

We can change ther time zone with date_default_timezone_set.

For instance, we write

date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s');

to call date_default_timezone_set to set the time zone.

Then we call date to get the current date and time at the current time zone.

Conclusion

To get the current date and time in PHP, we can use the date function.

How to fix Fatal error: Call to undefined function mysqli_connect() with PHP?

Sometimes, we want to fix Fatal error: Call to undefined function mysqli_connect() with PHP.

In this article, we’ll look at how to fix Fatal error: Call to undefined function mysqli_connect() with PHP.

How to fix Fatal error: Call to undefined function mysqli_connect() with PHP?

To fix Fatal error: Call to undefined function mysqli_connect() with PHP, we’ve install and enable the mysqli package.

To install it, we run

sudo apt install php-mysqli

on Ubuntu or its derivatives.

On Windows, we enable the mysqli extension by putting in

extension=php_mysqli.dll 

in php.ini.

Conclusion

To fix Fatal error: Call to undefined function mysqli_connect() with PHP, we’ve install and enable the mysqli package.

How to get a hex dump of a string in PHP?

Sometimes, we want to get a hex dump of a string in PHP.

In this article, we’ll look at how to get a hex dump of a string in PHP.

How to get a hex dump of a string in PHP?

To get a hex dump of a string in PHP, we can use the bin2hex function.

For instance, we write

echo bin2hex($string);

to call bin2hex with $string to return the hex dump of the string $string.

Conclusion

To get a hex dump of a string in PHP, we can use the bin2hex function.

How to solve PHP error ‘Notice: Array to string conversion in…’?

Sometimes, we want to solve PHP error ‘Notice: Array to string conversion in…’.

In this article, we’ll look at how to solve PHP error ‘Notice: Array to string conversion in…’.

How to solve PHP error ‘Notice: Array to string conversion in…’?

To solve PHP error ‘Notice: Array to string conversion in…’, we should make sure we aren’t trying to use print or echo on an array.

For instance, instead of using echo or print, we write

$stuff = [1, 2, 3];
print_r($stuff);

$stuff = [3, 4, 5];
var_dump($stuff);

to call print_r or var_dump with an array variable to print its contents.

Trying to print the contents of an array with print or echo will result in an error with PHP 7.x./

Conclusion

To solve PHP error ‘Notice: Array to string conversion in…’, we should make sure we aren’t trying to use print or echo on an array.

How to get the ID of the last updated row in MySQL?

Sometimes, we want to get the ID of the last updated row in MySQL.

In this article, we’ll look at how to get the ID of the last updated row in MySQL.

How to get the ID of the last updated row in MySQL?

To get the ID of the last updated row in MySQL, we can define a variable that has the ID of the entry we’re updating.

For instance, we write

SET @uids := null;
UPDATE footable
   SET foo = 'bar'
 WHERE fooid > 5
   AND (SELECT @uids := CONCAT_WS(',', fooid, @uids));
SELECT @uids;

to define the @uids variable that has the ID of the rows we’re updating.

We assign the value for @uids after the AND.

We get all the rows we’re updating with

SELECT @uids := CONCAT_WS(',', fooid, @uids)

@uids is a string with the IDs of the rows we’re updating separated by commas.

Conclusion

To get the ID of the last updated row in MySQL, we can define a variable that has the ID of the entry we’re updating.

How to sort files by date in PHP?

Sometimes, we want to sort files by date in PHP.

In this article, we’ll look at how to sort files by date in PHP.

How to sort files by date in PHP?

To sort files by date in PHP, we can use the filemtime and usort functions.

For instance, we write

$files = glob("path/to/files/*.swf");
usort($files, function ($a, $b) {
    return filemtime($b) - filemtime($a);
});

to get the files with the swf extension with glob in the path/to/files folder.

Then we call usort to sort the $files by the file date with a function that gets the time of each file and compare them.

We get the file date with filemtime and then sort them in descending order with

filemtime($b) - filemtime($a)

The $files array is then sorted in place.

Conclusion

To sort files by date in PHP, we can use the filemtime and usort functions.

How to sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP?

Sometimes, we want to sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP.

In this article, we’ll look at how to sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP.

How to sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP?

To sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP, we use the strtotime and usort functions.

For instance, we write

function date_compare($a, $b)
{
    $t1 = strtotime($a["datetime"]);
    $t2 = strtotime($b["datetime"]);
    return $t1 - $t2;
}
usort($array, "date_compare");

to call usort with $array and 'date_compare' to sort the $array in place with the date_compare function as the comparator function.

In date_compare, we convert the 'datetime' values of $a and $b to timestamps with strtotime.

Then we subtract the timestamps to compare them.

Conclusion

To sort a multidimensional array by elements containing Y-m-d H:i:s date with PHP, we use the strtotime and usort functions.