PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

snippet - PHP - str_replace() all variations
14 May, 2022
Collection of all variants of str_replace function like functionality. Examples providing all kinds of solutions for your needs. From simple string replacement to regular expressions.
snippet - PHP: Zendesk api attach file to ticket
19 Apr, 2022
You can't directly add a file to a ticket, but y can upload a file and attach it to a ticket comment. The attachment appears in the ticket comment. You can only attach the uploaded file to a ticket...
snippet - PHP: OneSky api get translations
21 Dec, 2021
Example where we download via api and parse translations. In multiple language export there is only one format "I18NEXT_MULTILINGUAL_JSON". That format does cut phrase ids using "." as a separator....
snippet - PHP: Imagick rotate and merge
1 Dec, 2021
Rotate and merge image using data from css transform. In this code example the input data comes from javascript where visually image is rotated using css transform property. CSS transform and Imagick...
snippet - PHP: Imagick resize / scale image
1 Dec, 2021
Change the size of an image by resize or scale an image. The resizeImage method is an inbuilt function in PHP which is used to scale an image to the desired dimensions.
snippet - PHP: Imagick transparent background
30 Nov, 2021
In order to work with transparent images, you have to define image as transparent. Do that immediately after creating an instance, by setting the object's default background color with...
snippet - PHP: Imagick read image files from URL
24 Nov, 2021
The code example shows how to read an image from url to Imagick. Imagick doesn't take in url directly, but it has a method for reading file as binary content. So we just need to get the content form...
snippet - MQL5 for GeSHi
1 Oct, 2021
MQL5 language file for GeSHi (Generic Syntax Highlighter). I used C++ langague file to base this on, since MQL is based on C++. I don't think it's the best possible highlight and ideally would use...
snippet - PHP error: class 'DateTimeZone' not found
1 Mar, 2021
DateTimeZone class is built into php. If you get error that it's missing, then it's namespace problem.
written tutorial - PHP connect to Azure DB
11 Feb, 2020
This snippet shows you how to connect to Azure database for you to run some scripts for an example. In this example we use ODBC class not through PDO. You need to download two packages from your...
snippet - Trim Array Whitespace in PHP
23 Oct, 2019
This snippet shows you how to trim whitespaces from each array value in PHP. Expects all array values to be strings.
snippet - Encode, decode and output JSON in PHP
11 Sep, 2019
This snippet contains a small class that does most of the JSON operations that you might ever need. Static functions for encoding, decoding and outputting json from PHP. Ensure that you get the right...
snippet - PHP: Check string for a specific word
6 Aug, 2019
The fastest string parsing is using existing functions, in this case you can use the strpos() which is used to find the occurrence of one string inside another one. When you need a stripos() for a...
snippet - PHP Output Buffering
24 Jul, 2019
Output buffering catches all echo-s, print-s and other output to stdOut. Then you can call a function on script shutdown to output all of the content. You can buffer output and stop scripts from...
written tutorial - Set Up Laravel, PHP 7.3, Nginx, and MySQL with Docker
11 May, 2019
Another way to setup Laravel expecting that you have git and composer installed on your machine. Presented in configuration file examples for fastest reading/setup.
snippet - Show all errors for development in PHP
26 Mar, 2019

Usually in default errors are disabled for production. Sometimes you have error 500 for an example and you need to see the error message. For an example in Drupal 7 you can put this in settings.php...
snippet - PHP: Send json with post request
26 Nov, 2018
Instead of curl I like to use http request function from a long and stable framework PHP: http request function, alternative to curl or file_get_contents. Because I find curl to be tedious. Using...
snippet - Magento 2: Module is not listing
18 Oct, 2018
When you copy a custom module to app/code directory. Magento does not pick it up automatically. You need to run this.
snippet - Number to words in PHP
24 Jul, 2018
Helper function for converting numbers to words. There is same function available in PHPs NumberFormatter class, but using it is not always possible, since it's not enabled by default.
snippet - PHP: http request function, alternative to curl or file_get_contents
12 Mar, 2018
Handles any HTTP request correctly. This is a flexible and powerful HTTP client implementation of GET, POST, PUT or any other HTTP requests. The reason I built this was due to use of url arguments...
snippet - PHP: First x characters of a string
2 Nov, 2017
Get first n amount of characters from a string. You could use substr for that, but you will get empty results when there are special characters. Thus multibyte string method is the right way to go.
snippet - PHP: Instagram to RSS feed
2 Aug, 2017
Instagram gives out json feed. You can use this little php snippet to create rss feed from the given json. The script leverages paging, which enables you to control the items count in the output feed.
snippet - PHP: Encode Url for Curl
23 May, 2017
This snippet encodes urls for curl. If you use special letters like äõöü in url, then you get a bit different url parsing from browsers than curl in PHP. You would think that you could use php's own...
snippet - PHP: Fix url query parameters starting with
19 May, 2017
Snippet shows you how to fix url query parameters starting with "...
snippet - PHP: Check url for valid response code 200
9 Jan, 2017
Check the HTTP Status Code of the website header using PHP. Check that url is giving valid response code of 2xx. You can modify this for different purposes. For detecting 404 not found only or...
snippet - Soap request with NTLM authentication
6 Dec, 2016
Usually when you have errors with soap it is because private Soap requests require some kind of authentication. One of the most common ways is to use NTML authentication, because Microsoft. PHP soap...
snippet - PHP: Download large file from url without loading it to memory
5 Dec, 2016
This snippet shows you how to download a file from url without loading it to memory. When you use file_get_contents() and file_set_contents() you load the content to memory and then to file. The...
snippet - Filename from path in PHP
22 Nov, 2016
Snippet shows you how to get filename from a path in PHP. Filename and extension separately. Can be used for extending filenames with timestamp or anything.
snippet - Select a random file from directory in PHP
22 Nov, 2016
Select a random file from directory. This can be useful if you want to display a random image on your website. For an example a background image randomly from a directory in your files.
snippet - Get body tag using PHP Dom Document
22 Sep, 2016
How to use a DOM parser and get the contents of the body.
snippet - Validate AJAX requests in Zend 1
26 Aug, 2016
How to validate AJAX requests in Zend Framework. There is a function in request class, to use instead of comparing the $_SERVER['HTTP_X_REQUESTED_WITH'] directly. This can be beneficial for security...
snippet - Directo class
17 May, 2016
Class for fetching and sending data between API. This class includes pulling data from Directo and posting xml with php array to xml generation.
snippet - PHP: Trim Array
5 May, 2016
This snippet shows you how to trim an array with single line of code. This can be achieved by using "array_map".
snippet - SimplePie Error: timed out after 10001 milliseconds
8 Feb, 2016
SimplePie is set to 10 seconds timeout in default. So if the feed has problem with speed then you can increase the time. The exact error that I got was "cURL error 28: Operation timed out after 10001...
snippet - PHP: Days since today or days between two dates
13 Jan, 2016
How to find out the amount of between two dates. For an example how many days have passed since a defined date.
snippet - Get every other member of array in PHP
11 Jan, 2016
Getting every other member of array in PHP. Examples of getting even or odd members of an array, using foreach loop and "modulo operation". The modulo operation (division by %) finds the remainder...
snippet - Enable Phar in PHP 7
10 Jan, 2016
Phar is included in PHP 7 amongst other updates. It's no longer external module. You have to turn off the "readonly" parameter. Since it disables Phar write support by default.
snippet - Cache json from curl request in PHP
5 Jan, 2016
When you create a simple request without any special REST client, you can do it just by using curl. When you need to do curl request, it's not a good practise to do it on every page load. So the...
snippet - Explode multiple delimiters PHP
4 Jan, 2016
To explode multiple delimiters PHP you have to use regular expression. Add your delimiters in the parentheses separated by pipe. You might have to escape your characters if they have meaning in PCRE...
snippet - PHP: Detect delimiter of CSV file
9 Dec, 2015
Using PHP to read csv file is a common thing to do. Sadly there is no solid standard on delimiters, depends on fully from where you get your csv file. Usually simple csv exports are a bit different...
snippet - Select next as well in PhpStorm
6 Oct, 2015
Select next, is a functionality well needed in everyday programming. For an example, if you have a function that defines a variable and the variable is used multiple times. You would like to select...
snippet - PHP: Replace all whitespaces with a single space
8 Sep, 2015
You can replace all whitespaces (space, new lines, etc.) with a single space using regular expression find and replace.
written tutorial - Set PhpStorm to always show line numbers
4 Sep, 2015
PhpStorm by default does not display line numbers. Working with version control or when debugging stuff, you usually get line number of the code where the error appears. So it would make sense that...
snippet - PHP: Get values by key in multidimensional array
5 Mar, 2015
How to get values by key name in multidimensional array. You have to iterate it recursively. This is the first time I can use Iterator classes of PHP and I already can think multiple problems this...
written tutorial - PHP: Memory management
22 Feb, 2015
If you are using larger files like bigger data files or images, memory might become an issue. Even if not, it's always good practice to use optimal amount of memory. Clearing memory is just a part of...
written tutorial - Php pcre regex or operator tutorial
2 Nov, 2014
PCRE regular expression operator OR. In regular expressions "or" is known as "alternatives". Create a subgroup using parenthesis.
snippet - PHP - Replace using regex
29 Oct, 2014
Search and replace using regular expression in PHP using preg_replace function. It is very powerful way to search and replace. I suggest using some online regular expression tool to test out your...
snippet - Validate UTF-8 strings in PHP
7 May, 2014
This function checks whether a string is valid UTF-8. You can ensure that the string you operate is a valid UTF-8 string. preg_match fails on strings containing invalid UTF-8 byte sequences. It does...
snippet - Convert text or HTML to plain-text in PHP
25 Nov, 2013
Encodes special characters in a plain-text string for display as HTML. Validates strings as UTF-8, if not UTF-8 it returns an empty string. It prevents CSS attacks on IE 6.
written tutorial - Install web server (Apache, PHP, MariaDB) on Linux Debian Wheezy
20 Nov, 2013
This tutorial shows how to prepare a Debian Wheezy web server. This is just a simple setup of basic web server. I myself did it to set up a development environment for myself.
snippet - Check if text contains cyrillic characters in PHP
5 Nov, 2013
Check if text contains cyrillic characters in PHP. This example shows you how to do exactly that.
snippet - Get words in string as array in PHP
16 Sep, 2013
How to split words in string to an array using regular expressions. You can use this for search indexing for an example.
written tutorial - Read or parse feeds with PHP
7 Aug, 2013
This tutorial shows how to parse RSS feeds with PHP. From the libraries and methods that I have tested and used SimplePie is by far the best PHP RSS parser. It supports most common RSS modules out of...
snippet - Convert file size in bytes to nice (human-readable) format in PHP.
6 Aug, 2013
This snippet shows you how to convert file size in bytes to nice (human-readable) format in PHP. If the size is less than 1 MB, show the size in KB, if it's between 1 MB - 1 GB show it in MB, etc.
snippet - Get img tag image url from html using regex in PHP
25 Jul, 2013
Snippet about getting img tag image url from html using regex in PHP. Example below.
snippet - Convert time to other time zone in PHP
7 May, 2013
Convert time and date from one time zone to another in PHP. Don't ever manage time adding or subtracting seconds, there is more to datetime than meets the eye.
snippet - Remove HTML elements using DOMDocument in PHP
19 Apr, 2013
This snippet shows how to remove HTML elements using DOMDocument in PHP. The problem is that you can't do it in the same loop.
written tutorial - Using DOMDocument to parse HTML in PHP
15 Apr, 2013
This snippet is about parsing HTML with PHP. It's hard to parse HTML, because people write incorrect HTML syntax very often. This code forces DOMDocument to read and later write HTML. So your changes...
snippet - PHP - get extension from file name
21 Mar, 2013
Get extension from file name.
snippet - Remove width and height form img tag using PHP
29 Nov, 2012
Remove width and height attributes from img tag using PHP. Really plain and simple replace width and height attributes with nothing.
snippet - PHP range where key and value is same
2 Oct, 2012
Creating range array where key and the value is the same number. I am using array_combine function for that. I give two parameters which are the same array created by range.
snippet - PHP - strip defined tags, strip_tags reverse
9 Aug, 2012
Strip tags in PHP strips all tags, except allowed tags. This function strips tags that you define. Also you can define if you want to strip content as well.
snippet - Strip tag with class, id or anything else in PHP
23 Jul, 2012
This function strips tags by any identifier without regexp, and without heavy XML parsing. As long as the tag looks exactly the same in source-code it works.
snippet - PHP Date or Time Range
12 Jul, 2012
Iterate through date or time range using PHP.
snippet - Get MSSQL Field Data Type in PHP
29 May, 2012
Example how to get field types in MSSQL. You can use it on any query, even procedures.
snippet - Detect character encoding in PHP
26 Apr, 2012
Detect character encoding in PHP and convert string to UTF-8.
snippet - Get all MSSQL tables using PHP
17 Feb, 2012
How to get all tables names that are in a certain table.
snippet - Check if URL is external or internal in PHP
11 Oct, 2011
Check if URL is external or internal in PHP. We are going to use parse_url PHP native function. Function parse_url parses a URL and returns an associative array containing any of the various...
snippet - Get PHP script execution time
22 Aug, 2011
How to test execution time of any part of the code in PHP. Basically you have to start counting microseconds before you code execution and stop after. This snippet shows how to do the counting.
snippet - Get files content type in PHP
8 Jul, 2011
This snippet gets content type of a file form external url. This snippet uses curl to download the file and get the content type.
snippet - Get Years in PHP
3 May, 2011
How to get current, last and coming year in PHP. Simple examples of how you can get different years. Currently the years are calculated from the server time.
written tutorial - PHP: Add Zeros Before Number
20 Feb, 2011
This tutorial shows how to add or append zeros before numbers. You might need this if you want to make folders named by numbers and you want them to be in right order when browsing the folder with...
written tutorial - Installing Yii Framework
17 Dec, 2010
How to install and set up Yii framework application to your hosting.
written tutorial - Add Text to Image in PHP
15 Dec, 2010
This tutorial shows how you can add text to images with PHP. You can use it to make banners with dynamic content for an example.
written tutorial - How to echo or print arrays in PHP
6 Dec, 2010
Examples of parsing array values on screen. Some examples for beginners.
written tutorial - Storing Images in MySQL With PHP
12 Nov, 2010
Easy to understand tutorial on how to upload, store and display images in mysql database.
written tutorial - Create Treeview Using Recursion in PHP
9 Oct, 2010
An easy example of recursion. PHP treeview can be used to create hierarchical lists like menus including sub-menus, category trees, taxonomy trees or site maps. This tutorial shows you how to...
written tutorial - PHP Global Variables
15 Jul, 2010
Tutorial about global variables in PHP. How to use global, superglobal in PHP.
written tutorial - Handling Timestamps in PHP
25 Jun, 2010
This tutorial is about using and converting timestamps in PHP, witch are common formats of storing some timeperiod in database.
written tutorial - Displaying random value from array in PHP
13 Jun, 2010
This beginners tutorial shows and explains how to get random value from array in PHP.