Where you can (and can't) use Emoji in PHP
I was noodling around in PHP the other day and discovered that this works:
PHP
<?php
$π = "bread";
echo "Some delicious " . $π;
I mean, there's no reason why it shouldn't work. An emoji is just a Unicode character (OK, not just a character - but we'll get on to that), so it should be fine to use anywhere.
Emoji work perfectly well as function names:
PHP
function πΊπΆ() {
echo "catdog!";
}
πΊπΆ();
Definitions:
PHP
define( "β", "huh?" );
echo β;
And, well, pretty much everywhere:
PHP
class π¦
{
public int $π¦;
public ?string $π¦;
public function __construct(int $π¦, ?string $π¦)
{
$this->π¦ = $π¦;
$this->π¦ = $π¦;
}
}
$π = new π¦(1234, "birb");
echo $π->π¦;
How about namespaces? Yup!
PHP
namespace π;
class π {
public function π() {
echo "Wink!";
}
}
use π\π;
$π = new π();
$π->π();
Even moderately complex Unicode sequences work:
PHP
echo <<<π³οΈβπ
Unicode is magic!
π³οΈβπ;
I've written before about the Quirks and Limitations of Emoji Flags. The humble π³οΈβπ is actually the sequence U+1F3F3 (white flag), U+FE0F (Variation Selector 16), U+200D (Zero Width Joiner), U+1F308 (Rainbow).
Take a complex emoji like "Female Astronaut with Medium Dark Skin Tone" - π§πΎβπ - that also works!
PHP
$π§πΎβπ = 1;
$π·π»ββοΈ = 2;
echo $π§πΎβπ + $π·π»ββοΈ;
Probable the most complex emoji has 10 different codepoints! It looks like this - π§πΎββ€οΈβπβπ§π»
And it works!
PHP
$π§πΎββ€οΈβπβπ§π» = "Kiss Kiss. Bang Bang!";
echo $π§πΎββ€οΈβπβπ§π»[-1];
There are some emoji which don't work;
PHP
$5οΈβ£ = "five";
The 5οΈβ£ emoji is U+0035 (Digit Five), U+FE0F (Variation Selector 16), U+20E3 (Combining Enclosing Keycap). PHP doesn't allow variables to start with digits, so it craps out with PHP Parse error: syntax error, unexpected integer "5", expecting variable or "{" or "$" in php shell code on line 1
You also can't use "punctuation" emoji as though they were normal characters:
PHP
echo 5 β= 6;
And, while not strictly emoji, you can't use mathematical symbols:
PHP
echo 5 β€ 6;
So, there you have it. Is this useful? Well, probably. It is easy to get lost in a sea of text - so little pictograms can make it easier to see what you're doing. If the basic ASCII characters aren't part of your native language, perhaps it is useful to make use of the full range of Unicode.
Does your favourite programming language support Emoji?
"We have Unicode support at home"
The Unicode support at home:
nicopap says:
More comments on Mastodon.