PHP: Something Borrowed, Something Blue

8 04 2018

PHP first appeared more than two decades ago, becoming wildly popular as if it were some bright, new shiny toy to the delight of developers around the world. Enthusiasts proudly displayed the lovely soft blue logo on their sites. What may be less than apparent even after all these years pertains to the impact of Perl on PHP.  

Sadly, nowadays, Perl’s reputation within the PHP community seems tainted. If one presents a proposal to the Internals List that includes a feature reminiscent of Perl, voting members may discount it if they deem it too Perlish. Yet, PHP owes a certain debt of gratitude to that language, from which it appropriated some basic notions. Lacking a good grasp of Perl, puts a developer at a disadvantage, particularly when perusing PHP source-code like the following:

$strEmpty = '';
$strSpace = ' ';
printf("%s\n", ($strEmpty == 0)? "true" : "false"); 
printf("%s\n",($strSpace == 0)?  "true" : "false");
printf("%s",($strEmpty == $strSpace)? "true": "false");

See live code

The last printf statement while making perfectly good sense, proves that PHP comparisons lack transitivity. So what may one conclude with respect to the prior two statements where an empty string and a string containing a blank space both equate with zero? For a C programmer, these results surely seem bizarre.  In fact, the following C translation of the preceding PHP snippet shows markedly different results:

int main(void) {
char chEmpty = '\0'; // ascii: 0
char chSpace = ' '; // ascii: 32
printf("%s\n",(chEmpty == 0)? "true": "false");
printf("%s\n",(chSpace == 0)? "true": "false");
printf("%s",(chEmpty == chSpace)? "true": "false");
return 0;

See live code

Note, a numeric context ensues by comparing a variable with a numeric value, employing the “==” operator reserved primarily for numeric comparison. Each variable temporarily acquires an integer value according to its ascii value.

PHP, however, does not take its cue from C in this instance but from its predecessor Perl. Perl imposes a pseudo numeric context.  If a numeric operator is used with a Perl script, including a numeric comparison symbol, then the values in a binary operation are first viewed as scalars and then as numeric values.  As it turns out, the strings ” and ‘ ‘ are both perceived as devoid of any numeric value other than zero because of their inherently non-numeric nature, so in a numeric context they each evaluate as zero; see here.  In this vein, consider the following Perl snippet:

$chEmpty = '';
$chSpace = ' ';
printf("%s\n",($chEmpty == 0)? "true":"false");
printf("%s\n",($chSpace == 0)? "true":"false");
printf("%s\n",($chSpace == $chEmpty)? "true":"false");

See live code

The Perl results resemble those of PHP.   Nowadays one may question whether PHP still needs to hang on to this Perlish notion of numeric context, especially since PHP 7 has undermined it. Consider the following code:

$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-1299)
$foo = 1 + "bob-1.3e3";           // $foo is integer (1)
$foo = 1 + "bob3";                // $foo is integer (1)
$foo = 1 + "10 Small Pigs";       // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)     

This snippet extracted from the Manual used to execute without any issues until PHP 7 where a flurry of warnings and notices appear complaining about the parser encountering non-numeric or non well formed values. See live code.

The usefulness now of numeric context in PHP is confined to comparisons, such as to make this kind of statement drawing from an old Beatles tune:

$num = 7;
$str = "7";
$unicodeChar = "\u{2665}";
if ($str == $num) {
  echo "I love PHP $str days a week";
echo $unicodeChar;

See live code

This work is licensed under a Creative Commons License



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: