Work with ZIP-archives in PHP 5.2+

Starting with PHP 5.2 you can use useful library ZipArchive for easy working with ZIP archives.
(If your PHP version les 5.2 you’ll get error: class ziparchive not found)

Steps for easy working with ZipArchive

$zip = new ZipArchive;

Read ZIP archive:


Create new ZIP archive (just add ZipArchive::CREATE):

$zip->open('{file_location_path}', ZipArchive::CREATE);

Example of extracting archive on your server:

$zip = new ZipArchive;
if ($zip->open('{file_location_path}') === true){
	echo 'Archive file is not found';

Example of creating archive on your server:
Continue reading Work with ZIP-archives in PHP 5.2+

What difference between extension, zend_extension, zend_extension_ts in PHP.ini (extension VS zend_extension)?

If you see in php.ini something like:

zend_extension = php_xdebug-2.5.1-7.1-vc14.dll
;zend_extension_ts = php_xdebug-2.0.4.dll ;for PHP<=5.2

extension – it’s native PHP extension
zend_extension – it’s Zend extension for PHP(mainly using for profiling/debug).
Both extension kinds share lots of stuff. The difference between both types is mainly in hooks they register into the Engine.
Remember that, despite it is very uncommon, an extension can be both a PHP extension and a Zend extension at the same time. Xdebug is a good example.

zend_extension_ts – it’s old(for PHP <5.3) threads safe Zend extension (not actual)
Detailed scheme of loading extension and zend_extension:

More detailed info you can read here:

Install Xdebug on Windows (PHP)

Simple steps for install XDebug on your Windows machine.
0. Write on test.php this code:


1. Copy all informmation from phpinfo page.
2. Open and insert phpinfo to textarea
3. Click “Analyse my phpinfo() output” and follow to provided instructions
Something like:

  • Download php_xdebug-2.5.1-7.1-vc14.dll
  • Move the downloaded file to E:\xampp\php\ext
  • Update E:\xampp\php\php.ini and change the line zend_extension = E:\xampp\php\ext\php_xdebug-2.5.1-7.1-vc14.dll
  • Restart the webserver
$obj1 = new stdClass();
$obj1->name = 'A';
$obj1->date = date('Y-m-d H:i:s', 1458754362);

Happy debugging.

PHP. Save information into text/log file. Bacis examples.

Simple example for save information to file on server by PHP

$content = '[' . date('Y-m-d H:i:s') . ']: test info log' . PHP_EOL . '------------' . PHP_EOL;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/save_test.log", "a+");
fwrite($fp, $content);

Error log using example(logs $content [array|string|object] to the log file):

$obj = new StdClass();
$content = [$obj, [3, 51, '4k']];
error_log('[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . print_r($content, TRUE) . PHP_EOL, 3, $_SERVER['DOCUMENT_ROOT'] . "/save_test.log");

Also you can use file_put_contents method:
Continue reading PHP. Save information into text/log file. Bacis examples.

Native PHP Sort array of objects by property

Usually, it is very often necessary to sort the data in an array. But when in the array we have objects it happened little bit difficult.
Fast solution for sorting array objects by property name.
Bacis solution (PHP 5.3+) :

function sortArrayWithObjects($array, $property)
    usort($array, function ($a, $b) use ($property) {
        return (($a->$property == $b->$property) ? 0 : (($a->$property < $b->$property) ? -1 : 1));
        //[short version] return strcmp($a->$property, $b->$property);
    return $array;

$obj1 = new stdClass();
$obj2 = clone $obj1;
$obj3 = clone $obj2;
$obj1->name = 'B';
$obj2->name = 'C';
$obj3->name = 'A';
$data = [$obj1, $obj2, $obj3];
$result = sortArrayWithObjects($data, 'name');
echo '<pre>';
echo '</pre>';

Continue reading Native PHP Sort array of objects by property

PHPExcel not working with PHP 7.+ – ZipArchive not found

PHPExcel ZipArchive not found

If you catch this error (and you use PHP 7 version) you need:
1. update PHPExcel to 1.8.+
2. try to use this preparing method:



How to get timestamp of start to end (current) day period on PHP?

$dtNow = new \DateTime();
$beginOfDay = clone $dtNow;

$endOfDay = clone $beginOfDay;
$endOfDay->modify('1 second ago');

$start_timestamp = $beginOfDay->getTimestamp();
$end_timestamp = $endOfDay->getTimestamp();

echo 'Start: '.$start_timestamp." [".date("Y-m-d H:i:s",$start_timestamp)."]"."\n";
echo 'End: '.$end_timestamp." [".date("Y-m-d H:i:s",$end_timestamp)."]"."\n";

Also, you can get first and last timestamps of a current month:
Continue reading How to get timestamp of start to end (current) day period on PHP?

PHP debug[timer] – Time of execute/load your code in the page

So, what about performance of your PHP code? 🙂 Answers is simple after tracking the script execution time of your code…
For get executing time of part of your code or of all page time loading you can use simple construction:

$start_time = microtime(true);
// your code here
$end_time = microtime(true);
$result_time = $end_time - $start_time;
echo "Debug info: Code loaded $result_time seconds\n";

How to strip all special symbols/characters from string for save to mysql database

* Function for delete utf special symbols.
    Units of measure:
        \x{00B0} for the degree symbol.
        \x{2103} for the degree celsius symbol.
        \x{2109} for the degree fahrenheit symbol.
        \x{23CD} for the square foot symbol.
        \x{32CC} to \x{32CE} for units symbols.
        \x{3300} to \x{3357} for ideographic units symbols.
        \x{3371} to \x{33DF} for more units symbols.
        \x{33FF} for the gallon symbol.

    Ideograph radicals, strokes, symbols, and descriptors:
        \x{2E80} to \x{2EF3} for CJK radicals.
        \x{2F00} to \x{2FD5} for KangXI radicals.
        \x{2FF0} to \x{2FFB} for ideographic descriptors.
        \x{3037} to \x{303F} for miscellaneous ideographic indicators.
        \x{3190} to \x{319F} for ideographic annotation marks.
        \x{31C0} to \x{31CF} for CJK strokes.
        \x{32C0} to \x{32CB} for ideograph month symbols.
        \x{3358} to \x{3370} for ideograph time symbols.
        \x{33E0} to \x{33FE} for ideograph day symbols.
        \x{A490} to \x{A4C6} for YI radicals.

function removeSpecSymbols($text = '')
        $units = '\x{00B0}\x{2103}\x{2109}\x{23CD}';
        $units .= '\x{32CC}-\x{32CE}';
        $units .= '\x{3300}-\x{3357}';
        $units .= '\x{3371}-\x{33DF}';
        $units .= '\x{33FF}';

        $ideo = '\x{2E80}-\x{2EF3}';
        $ideo .= '\x{2F00}-\x{2FD5}';
        $ideo .= '\x{2FF0}-\x{2FFB}';
        $ideo .= '\x{3037}-\x{303F}';
        $ideo .= '\x{3190}-\x{319F}';
        $ideo .= '\x{31C0}-\x{31CF}';
        $ideo .= '\x{32D0}-\x{32FE}';
        $ideo .= '\x{3358}-\x{3370}';
        $ideo .= '\x{33E0}-\x{33FE}';
        $ideo .= '\x{A490}-\x{A4C6}';

        $text = preg_replace('/\p{So}(?<![' . $units . $ideo . '])/u', ' ', $text);

        return $text;

$text = removeSpecSymbols($text);

How to merge two JSON files in PHP?

If you need merge information from two json files with same structure but with different values then you can use this overide recursive method

//Recursive method for merge 2 json files
function mergeJsonArrays($priority_json, $original_json_content)
        foreach ($original_json_content as $org_content_key => $org_content_value) {
            if (!array_key_exists($org_content_key, $priority_json)) {
                $priority_json[$org_content_key] = $org_content_value;
            } elseif (!is_string($org_content_value)) {
                $priority_json[$org_content_key] =  mergeJsonArrays($priority_json[$org_content_key], $org_content_value);
        return $priority_json;

 $jsonAssoc = true;
  $first_config_file_path = 'config_folder/first_json_file.json';
  $json_first = is_file($first_config_file_path) ? json_decode(file_get_contents($first_config_file_path), $jsonAssoc) : NULL;

  $second_config_file_path = 'config_folder/second_json_file.json';
  $json_second = is_file($second_config_file_path) ? json_decode(file_get_contents($second_config_file_path), $jsonAssoc) : NULL;

 $result_json = mergeJsonArrays($json_first, $json_second);

Example [config_folder/first_json_file.json]
Continue reading How to merge two JSON files in PHP?

Pig Latin via PHP

Pig Latin is a game of alterations played on the English language game. To create the Pig Latin form of an English word the initial consonant sound is transposed to the end of the word and an ay is affixed (Ex.: “banana” would yield anana-bay). Read Wikipedia for more information on rules.

function getPigLatin($word)
$first_pig_latin = preg_replace('#^([^aeiou]+)([aeiou]+)(.*)#', '$2$3$1ay', $word);
$result_pig_latin = preg_replace('#(^[aeiou].*)#', '$1way', $first_pig_latin);
return $result_pig_latin;
echo getPigLatin('banana');

Reverse in PHP

Reverse a string by PHP function strrev

echo strrev("Hello world!");

Result: !dlrow olleH

Reverse words in string

$input_string = 'Hello world! Please reverse my words!';
echo implode(' ', array_reverse(explode(' ', $input_string)));

Result: words! my reverse Please world! Hello

Reverse array items by PHP function array_reverse

$input_array = array(3 , 1, "zero", true);
$reversed_array = array_reverse($input_array);

Result: Array ( [0] => 1 [1] => zero [2] => 1 [3] => 3 )

Continue reading Reverse in PHP

How to get file extension on PHP?

There are some examples how to get file extensions via PHP.
Example 1. Quick, short, safe

function get_file_extension($file_name) {
return end(explode('.',$file_name));

Example 2 [PHP 5 >= 5.3.6]. SPL use

function get_file_extension($file_name) {
$file_info = new SplFileInfo($file_name);
return $file_info->getExtension();

Example 3 [PHP 4 >= 4.0.3+]. Pathinfo use

function get_file_extension($file_name) {
return pathinfo($file_name, PATHINFO_EXTENSION);

Send e-mail via PHP

Some popular examples for sending emails via PHP.
Example #1 “Small and common” (Just text) ↓

$customer_email = '';
$email_subject = 'Test e-mail subject';
$email_message = "Hello, Customer\r\nIt's our message information\r\nThanks!";
mail( $customer_email, $email_subject, $email_message);

Example #2 “Simple with headers” (Just text) ↓

$site_email = '';
$customer_email = '';
$email_subject = 'Test e-mail subject';
$email_message = "Hello, Customer\r\nIt's our message information\r\nThanks!";
$headers =  'From: ' .$site_email. "\r\n" .
mail($customer_email, $email_subject, $email_message, $headers);

OR (by only set headers manual) ↓
Continue reading Send e-mail via PHP