You may already know, that PHP (formerly Personal Home Page or PHP/FI aka. Personal Home Page/Forms Interpreter) has been developed by Rasmus Lerdorf back in 1994. And you may also know, that he wrote PHP as CGI (Common Gateway Interface) set in C to replace some Perl Scripts to manage his very own website only. He never planned to “create” a new programming language at all. This are all standard facts which you can find on ANY website, that wrote an article about PHP facts.

BUT do you also know the following 5 really interesting facts?

 

#5 – Why is it called mysqli_real_*?

Did you ever wonder about the function / method name mysqli_real_escape_string() in PHP? What is so “real” on this function? To answer that we need to go back to PHP 4.0.3 which implemented the first and only mysql_escape_string() function. (Note: We’re talking about the MySQL and not the MySQLi package.) This construct reads the Query string as binary and tries to add escape sequences where it believes it’s appropriate. As you can imagine, this implementation didn’t work really well…

PHP 4.3.0 fixes this issue by adding the new mysql_real_escape_string() function, which uses the information about the character set used to connect to the MySQL Database. So it tries to escape these characters, which need to be escaped. PHP 5.0.0 brought the MySQLi package along, which directly implements the real function to avoid confusion with the unsafe one. The newest PHP Major Release (7) doesn’t contain the normal MySQL package any more, but the real methods of MySQLi remains, of course!

 

#4 – The known PHP tags <?php ?>

The known PHP tags <?php ?>, to “start” and “close” PHP content, was first introduced in PHP 3! The first real PHP/FI Version used the HTML Comment syntax <!-- --> to single out PHP from the HTML source code. And PHP 2 used a similar variant, as the current known one, called <? ?>, so just the additional “php” on the starting tag is missing. You can still use the “short PHP tags” by enabling the short_open_tag option in your PHP.ini file. Below PHP 7 you are also able to enable the ASP.NET known tags (<% %>) by setting the asp_tags option to true.

 

#3 – Create GUIs in PHP

It IS possible to create a Window-based Desktop application with just PHP, and one of the 6 special C-written extensions. If you want to use the known X11 (also known as X Window System), which is the UNIX-common interface to bitmap displays, you just need to compile your PHP 3 version with the php_xcb package. The tcl/tk API can be implemented in your PHP 5 version by adding the PHP/TK library. The native Windows32 Windowing System API requires either the win32std package and PHP 4 (or PHP 5 with some modifications) or WinBinder, which is compatible with PHP 5.1.1 at least.

The last both packages, which are listed on PECL, are ui a PHP 7 written GTK 3+ library and “wxWidgets for PHP” called wxPHP, which brings the wxWidget system to PHP, as the name suggests… I don’t want to answer, if PHP is the best interpretive programming language environment to create Graphical User Interfaces, (or if you should rather use Python). I only want to show you, that there are libraries, you can use for if you need to.

 

#2 – Increase Strings like Numbers

It is really easy to increase numbers step-by-step by using the ++ on your variable. So ++$a will increase your variable by 1 and return the new result, and $a++ will return the current value and increase it afterwards. This method can also be used on a string, which ends up with a alphanumeric character. Try out the following example on your local machine, or directly on PHPtester.net:

<?php

    $string = "pytesNET";
    var_dump(++$string);	// Returns pytesNEU

    $string = "pitZ";
    var_dump(++$string);	// Returns piuA

    $string = "pit_19";
    var_dump(++$string);	// Returns pit20

    $string = "pit_a9";
    var_dump(++$string);	// Returns pitb0

 

PHP uses the same convention as Perl, so it turns the last character into the respective “next” alpha-numeric character. The documentation itself describes this behavior:

PHP follows Perl’s convention when dealing with arithmetic operations on character variables and not C’s. For example, in PHP and Perl $a = ‘Z’; $a++; turns $a into ‘AA’, while in C a = ‘Z’; a++; turns a into ‘[‘ (ASCII value of ‘Z’ is 90, ASCII value of ‘[‘ is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

php.net – Incrementing/Decrementing Operators

 

#1 – Don’t trust PHP

PHP doesn’t use a strict type check per default, which results in strange results while comparing some variables. So it is highly recommended to check the type and value of both variables using === and !==! Nothing unknown will happen if you follow this rule. Many comparable functions and methods allows also to set a specific parameter to true to enable the strict comparison, like on in_array.

Both examples below will result in true, if you don’t enable the strict type check on the used in_array function. This happens because it just uses the loose comparison using "twitter" == true and "twitter" == 0.

<?php

    $myArray = array(
        "pytesNET"  => true,
        "pitNET"    => false,
        "pitIO"     => false
    );
    var_dump(in_array("twitter", $myArray));

    $myArray = array(
        "count" => 100,
        "times" => 10,
        "multi" => 0
    );
    var_dump(in_array("twitter", $myArray));

 

Bonus Facts

On my research for this article, I also found the following short interesting facts and behaviours too, which I don’t want to withhold from you!

Many Ways to Sort Arrays
PHP offers 13 functions to sort arrays, three of them allows to pass a callback function as parameter for the sort process. This functions gets called ONLY, if your array has at least 2 items (which makes sense, because you can’t sort an array which consists of just a single item).
You don’t have Money on Windows
The function money_format() requires the C-library strfmon, which isn’t available per default on any “Windows” operating systems. You may think Oh, but PHP has guaranteed written a respective fallback solution, but no… The function is just undefined. So YOU need to add a fallback solution, to make your script also available on Windows!

PDO, the most-recommended interface to connect to databases, stands for PHP Data Objects.
PEAR, the PHP own package manager and repository, stands for PHP Extension and Application Repository.
PECL, the PHP own package and extension repository, stands for PHP Extension Community Library.