How-to articles, tricks, and solutions about PHP

How can I sanitize user input with PHP?

There are several ways to sanitize user input in PHP, depending on the type of data and how you plan to use it.

How can I select and upload multiple files with HTML and PHP, using HTTP POST?

You can use the following HTML code to create a form that allows users to select and upload multiple files.

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.

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.

How can I use PHP to dynamically publish an ical file to be read by Google Calendar?

Here's an example of how you can use the iCalcreator library to create an iCal file and output it for download:

How can I view/open a word document in my browser using with PHP or HTML

You can use the PHP function readfile() to read the contents of a Word document and then use the appropriate headers to prompt the browser to open or download the file.

How can I write a file in UTF-8 format?

To write a file in UTF-8 encoding with PHP, you can use the fopen, fwrite, and fclose functions.

How check if a String is a Valid XML with-out Displaying a Warning in PHP

In PHP, you can use the simplexml_load_string function to check if a string is a valid XML without displaying a warning.

How do I add PHP code/file to HTML(.html) files?

To add PHP code to an HTML file, you will need to use PHP tags.

How do I capture PHP output into a variable?

In PHP, you can use the output control functions to capture the output of a PHP script into a variable.

How do I compare two DateTime objects in PHP 5.2.8?

To compare two DateTime objects in PHP 5.2.8, you can use the DateTime::diff() method, which returns a DateInterval object representing the difference between the two dates.

How do I count unique visitors to my site?

There are several ways to count unique visitors to a website using PHP, but one common method is to use cookies.

How do I create a copy of an object in PHP?

In PHP, you can create a copy of an object using the clone keyword.

How do I display a MySQL error in PHP for a long query that depends on the user input?

To display a MySQL error in PHP for a long query that depends on user input, you can use the mysqli_error() function.

How do I generate a custom menu/sub-menu system using wp_get_nav_menu_items in WordPress?

To generate a custom menu/sub-menu system using wp_get_nav_menu_items in WordPress, you can follow these steps:

How do I get a YouTube video thumbnail from the YouTube API?

To get a thumbnail image for a YouTube video in PHP, you can use the file_get_contentsfunction to make a GET request to the videos.list method of the YouTube Data API.

How do I implement basic "Long Polling"?

Here's an example of how you can implement Long Polling in PHP:

How do I install Composer on a shared hosting?

To install Composer on a shared hosting, you can use the following steps:

How do I PHP-unserialize a jQuery-serialized form?

In PHP, you can use the unserialize function to unserialize a serialized string.

How do I print all POST results when a form is submitted?

To print all the POST results when a form is submitted with PHP, you can use the print_r($_POST) function.

How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?

In PHP, you can use the rmdir() function to remove a directory, and the unlink() function to remove a file.

How do I remove the last comma from a string using PHP?

To remove the last comma from a string in PHP, you can use the rtrim function.

How do I return a proper success/error message for JQuery .ajax() using PHP?

To return a proper success/error message for jQuery.ajax() using PHP, you can do the following:

How do I see the actual XML generated by PHP SOAP Client Class?

To see the actual XML generated by the PHP SOAP Client class, you can use the __getLastRequest() method.

How do I write to the console from a Laravel Controller?

You can use the Log facade to write to the console from a Laravel controller.