How-to articles, tricks, and solutions about PHP

Get latitude and longitude automatically using php, API

To get latitude and longitude automatically using PHP, you can use an API (Application Programming Interface) such as Google Maps API, OpenCage Geocoding API, or GeoCode.io API.

How to add http:// if it doesn't exist in the URL

You can use the following code snippet to add "http://" to the beginning of a URL if it doesn't already exist:

How to list files and folder in a dir (PHP)

In PHP, you can use the scandir() function to list the files and directories in a directory.

PHP fopen() Error: failed to open stream: Permission denied

This error message is indicating that the PHP script is trying to open a file using the fopen() function, but the script does not have the necessary permissions to access the file.

Difference between 2 dates in seconds

In PHP, you can use the built-in function strtotime() to convert a date string to a Unix timestamp, which is the number of seconds since the Unix epoch (January 1, 1970).

PHP: Read Specific Line From File

In PHP, you can read a specific line from a file using the file() function, which reads the entire file into an array, with each line of the file as an element in the array.

What does this mean? "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM"

This error message is indicating that there is a problem with the syntax of the code, specifically a parse error, and that it was unexpected to find the token "T_PAAMAYIM_NEKUDOTAYIM" in that particular place in the code.

How to get the server path to the web directory in Symfony2 from inside the controller?

In Symfony2, you can use the $this->get('kernel')->getRootDir() method inside a controller to get the server path to the web directory.

How to Automatically Start a Download in PHP?

To automatically start a download in PHP, you can use the header() function to send the appropriate headers to the browser indicating that a file download should begin.

Compiling php with curl, where is curl installed?

The location of the curl installation depends on the operating system and the method used to install it.

PHP returning JSON to JQUERY AJAX CALL

To return JSON from a PHP script to a jQuery AJAX call, you can use the json_encode() function in PHP to convert an array or object into a JSON string.

How to search text using php if ($text contains "World")

You can use the strpos() function in PHP to search for a specific string within another string.

Max size of URL parameters in _GET

In PHP, the maximum size of the parameters in a GET request is determined by the server's configuration.

posting hidden value

In PHP, you can post a hidden value by including it in a hidden input field within a HTML form.

How to fix Warning Illegal string offset in PHP

The "Illegal string offset" warning in PHP occurs when you try to access an offset of a string variable as if it were an array.

How can I trim all strings in an Array?

You can use the array_map() function to trim all strings in an array in PHP.

Type hinting in PHP 7 - array of objects

In PHP 7, type hinting can be used to specify the expected data type of a function or method parameter.

How to find average from array in php?

You can use the array_sum function to get the sum of all the values in an array, and then divide that sum by the number of elements in the array to get the average.

How can I display the users profile pic using the facebook graph api?

To display a user's profile picture using the Facebook Graph API in PHP, you can make a GET request to the /{user-id}/picture endpoint.

How can I use PHP to check if a directory is empty?

You can use the scandir() function to get an array of files in a directory, and then check the length of the array.

Laravel: How to get last N entries from DB

You can use the latest method on a query builder instance to get the last N entries from the database.

How to get the full URL of a Drupal page?

In Drupal, you can use the url() function to get the full URL of a page.

XSS filtering function in PHP

Here is an example of a basic XSS filtering function in PHP:

How to send Emoji with Telegram Bot API?

To send an emoji using the Telegram Bot API in PHP, you can use the sendMessage method and include the emoji in the text of the message.