DEV Community

Anders Björkland
Anders Björkland

Posted on

Ghosts of Christmas - a few oddities in PHP 👻

Just the Gist

While the ghost of the past had security issues or lack of support for common programming paradigms, the ghost of the present have seen to that there are quirks and oddities in PHP. As luck would have it, we have mostly addressed the ghost of the past. But today we are visited upon by the ghost of the present, but not all it brings us is of evil.

Neat but Inconsistent

Did you know that we have some neat array functions? Fan favorites such as array_map and array_filter let's us transform values and filter them with some handy callback functions. Both of these functions takes two parameters; an array and a callback function to be used on each value in the array. But here's an odd thing: array_map has a callback as its first parameter, and array_filter has a callback as its second parameter.

Pass-by-Both

In programming languages, a function is called by passing it as an argument. When this argument is a variable, it can either be copied and treated as a new variable within that function. This is called pass-by-value. In some languages, it will still look like you have passed the function your very own variable as it may be mutated by the function. This is what can happen in JavaScript. That's because the variable being passed is a reference (or an "address") to an object (including arrays). This is still a pass-by-value system. Java and Python are other examples of languages that uses pass-by-value. And guess what? PHP is a language that uses this system too, by default. That is, PHP can optionally become a pass-by-reference system. So what is that? Let this example illustrate it:

<?php 

function add5(&$num) {
    $num += 5;
}

$a = 10;
add5($a);
echo "December {$a}th";  
// Outputs: December 15th
Enter fullscreen mode Exit fullscreen mode

☝ So what's going on here is that we are passing our variable $a by reference. This means that the variable $a is actually pointing to the same address as the one being used in the function add5. So when we call add5, the value of $a is being changed. This is what we call pass-by-reference. Similar system is used in C++ too.

Function-call by string

We can call functions by using the string name of the function. That means that we can programmatically call functions with the help of call_user_func. Here's an example:

<?php 

function christmasGreeting($name) {
    echo "Happy Christmas, $name!";
}

$season = "christmas";
$functionType = "Greeting";

call_user_func($season . $functionType, "McClane");  
// Output: "Happy Christmas, McClane!"
Enter fullscreen mode Exit fullscreen mode

We have constructed a string that is the concatenation of the string $season and the string $functionType, which gets us "christmasGreeting". Now we can call the function christmasGreeting by passing it as an argument to call_user_func. Any following arguments passed to call_user_func will be passed on to the function.

Talking about functions

If you are used to functional programming, or JavaScript in general, then this next tidbit may not seem so surprising to you. In PHP, functions are first-class objects. This means that functions can be passed around as arguments to other functions, and functions can be returned from other functions. This is a very powerful feature of PHP. Here's a taste:

<?php 

$greeting = function ($name) {
    return "Hello, $name!";
};

function birthdayGreetings($name, callable $greeting) 
{
    echo $greeting($name) . " Happy birthday!\n";
}

birthdayGreetings('DROP TABLE', $greeting);  
// Output: Hello, DROP TABLE! Happy birthday!
Enter fullscreen mode Exit fullscreen mode

What about you?

These were all the oddities for now. Many are pretty neat features of PHP that we don't use much, others has been left for the sake of backward compatibility. Did you find any quirk especially interesting? Did I miss your favorite oddity? Also, is Die Hard a Christmas movie? Comment below and let us know what you think ✍

Discussion (3)

Collapse
grocker42 profile image
Grocker

echo b"Test" does not throw a error.

Collapse
andersbjorkland profile image
Anders Björkland Author

What?! 😯 Now that's a quirk. I need to check that out when I get home.

Collapse
grocker42 profile image
Grocker • Edited on

This has Something to do with the never released PHP 6 and binary Support the b stands for binary.

stackoverflow.com/questions/474944...