Wednesday, May 11, 2016

Interesting and Strange Facts about PHP, Every Developer should know

Are you getting started with PHP ? Then you gotta know these facts about php. It will help to generate interest in yourself and encourage others to learn php.

Many of these facts are reason for PHP being so popular.
  • PHP originally stood for Personal Home Page.
  • PHP which is now officially known as 'Hypertext Preprocessor' was released in the year 1995.
  • Initially written as a set of Common Gateway Interface (CGI) in 'C' (1994).
  • PHP was originally designed to replace a set of Perl scripts to maintain his Personal Home Pages (also known as PHP).
  • PHP was originally created by Rasmus Lerdorf in 1995. He wrote the original Common Gateway Interface (CGI) binaries.
  • Zeev Suraski and Andi Gutmans, two developers at the Technion IIT, rewrote the parser in 1997 and formed the base of PHP 3.
  • PHP 3 was official launched in June 1998.
  • Suraski and Gutmans rewrote the PHP 3's core, producing the Zend Engine in 1999. They also founded Zend Technologies in Ramat Gan, Israel.
  • On May 22, 2000, PHP 4, powered by the Zend Engine 1.0, was released.
  • The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification.
  • On July 13, 2004, PHP 5 was released, powered by the new Zend Engine II. PHP 5 introduced full featured object-oriented programming support. It was there in PHP 3 and PHP 4 but only the basic features.
  • PHP is free software released under the PHP License, which is incompatible with the GNU General Public License (GPL) due to restrictions on the use of the term PHP.
  • PHP was originally designed to create dynamic and more interactive web pages. It is the most widely-used, open-source and general-purpose scripting language.
  • It is possible to use PHP in almost every operating system. PHP can be used in all major operating systems including Linux, Microsoft Windows, Mac OS X, and RISC OS.
  • PHP uses procedural programming or object oriented programming and also a mixture of them.
  • PHP is installed on over 20 million websites and 1 million web servers. (PHP: 244M sites, 2.1M IP addresses)
  • 75% of Web 2.0 sites are built in PHP. PHP is used by 81.7% of all the websites whose server-side programming language we know.
  • There are about 5 million PHP developers worldwide.
  • The latest release of PHP till now is 7.0.6. It was released on April 29, 2016. During 2014 and 2015, a new major PHP version was developed, which was numbered PHP 7. The numbering of this version involved some debate. While the PHP 6 Unicode experiment had never been released, several articles and book titles referenced the PHP 6 name, which might have caused confusion if a new release were to reuse the name. After a vote, the name PHP 7 was chosen
  • Some of the biggest online brands, such as Facebook, ProProfs, Digg, Friendster, Flickr, Technorati, and Yahoo! are powered by PHP
Popular sites using PHP
Strange PHP Facts about PHP

Floating Point Imprecision

Most of you probably know that floating-point numbers cannot really represent all real numbers. Besides, some operations between two well represented numbers can lead to surprising situations. This is because the finite precision with which computers represent numbers and so it affects not just PHP but all the programming languages. Inaccuracies in floating point problems have been given programmers headaches from the beginning of the discipline.
Let's look at this very small snippet:

echo (int) ((0.1 + 0.7) * 10);

What do you think will be the result? If you guessed 8, you'd be wrong I'm sorry, This code will actually print 7. 

Now let's see why this happens. The first operation executed is:

0.1 + 0.7 // result is 0.79999999999

The result of the arithmetic expression is stored internally as 0.7999999, not 0.8 due to the precision issue, and this is where the problem starts.

The second operation executed is:

0.79999999 * 10 = 7.999999999

This operation works well but preserves the error.

The third and last operation executed is:

(int) 7.9999999 // result is 7

The expression makes use of an explicit cast. When a value is convert to int, PHP truncates the decimal portion of the number which makes the result 7.

There are two interesting things to note here:
  • If you cast the number to float instead of int, or if you don’t cast at all, you'll get 8 as result as you expected.
  • Although the CPU doesn’t know, and we actually we got this error due to its imprecisions, it's working accordingly to the mathematics paradox that asserts 0.999… is equal to 1.

How PHP "Increments" Strings

During our everyday work we write instructions that perform incrementing/decrementing operations like these:

$a = 5;
$b = 6;

Everyone easily understands what’s going on with those statements. But given the following statements, guess what will be the output:

$a = 1;
$b = 3;
echo $a++ + $b;
echo $a + ++$b;
echo ++$a + $b++;

Did you write down your answers? Good. Let's check all.

Not too difficult, right? Now let’s raise the bar a little. Have you ever tried to increment strings? Try to guess what the output will be for the following:

$a = 'fact_2';
echo ++$a;

$a = '2nd_fact';
echo ++$a;

$a = 'a_fact';
echo ++$a;

$a = 'a_fact?';
echo ++$a;

It’s not so easy this time, is it. Let’s uncover the answers.

Surprised? Using an increment operator on a string that ends with a number has the effect of increasing the letter (the one following it in the alphabet, e.g. t -> u). Regardless if the string starts with digits or not, the last character is changed. But there is no effect if the string ends with a non-alphanumeric character.

This is well documented by the official documentation about the incrementing/decrementing operators but most of you may not have read it because you probably thought nothing special was there. I must admit I thought the same until a short time ago.

The Mystery of Value Appearance

You’re a real array master in PHP, admit it. Don’t be shy. You already know everything about how to create, manage, and delete arrays. Nonetheless, the next example might surprise you at first sight.

Often you work with arrays and need to search if a given value is in an array. PHP has a function for this, in_array(). Let’s see it in action:

$array = array(
  'isReady' => false,
  'isPHP' => true,
  'isStrange' => true
var_dump(in_array('', $array));

What will be the output? Place your bets and let’s check it.

Isn’t this a little bit strange? We have an associative array where its values are all boolean and if we search for a string, we got true. Has this value magically appeared? Let’s see another example.

$array = array(
  'count' => 1,
  'references' => 0,
  'ghosts' => 1
var_dump(in_array('pawan', $array));

And what we get is…

Once again the in_array() function returns true. How is this possible?

You’re experiencing one of both best and worst, loved and hated PHP features. PHP isn’t strongly typed and this is where the error starts. In fact, all the following values, if compared in PHP, are considered the same unless you use the identity operator:

By default, the in_array() function uses the loose comparison, a non-empty ("") and non-zero-filled ("0") string is equal to true and to all non-zero numbers (like 1). Hence, in the first example we got true because '' == true, while in the second we have 'aurelio' == 0.

To solve the problem you've to use the third and optional parameter of in_array() that allows you to compare the values strictly. So, if we write:

$array = array(
  'count' => 1,
  'references' => 0,
  'ghosts' => 1
var_dump(in_array('pawan', $array, true));

we'll finally get false as we expect!

1 comment:

  1. Excellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in hyderabad we want work out extra strategies in this regard, thanks for sharing.