Article

← Retour au blog

🇫🇷 Lire en français

PHP supports UTF-8 characters in function names!

2009-04-08 #864 · rév. 1 · EN

While reworking a function that basically dumps the PHP stack when a crash occurs, I came across an interesting thing I had silently complained about for the past few years: why the hell can’t I write a function name with accents in it?

Well, the dream has come true in PHP 5.x! Here you are: see the function Mélody_είναι_συμπαθητική() which mixes both French accents and Greek characters — meaning: Mélody is friendly.

<code>
<pre>
<?php
// additional code

function Mélody_είναι_συμπαθητική($arg=“avec un long long long argument qu’il faudrait raccourcir un peu”) {

print dumpPHPStack(false); print “\n\n”;
}

function main($arg1, $arg2) {
print “En PHP 5.x on peut appeler des fonctions ayant des accents…\\n”;
Mélody_είναι_συμπαθητική(“avec un long long long argument qu’il faudrait raccourcir un peu”);
}

main(‘here’, “we go”);
?>
</pre>
</code>

Which outputs the following:

<pre> PHP STACK DUMP | # | Line # | File name | Function call | 1 | 102 | /Users/stephanedeluca/Desktop/phpdump.php | dumpPHPStack (false) | 2 | 109 | /Users/stephanedeluca/Desktop/phpdump.php | Mélody_είναι_συμπαθητική ("avec un long long long argument qu'il faudrait raccourcir un peu") | 3 | 112 | /Users/stephanedeluca/Desktop/phpdump.php | main ("here", "we go") </pre>

I also tested right-to-left characters such as Arabic and it works!

<pre> PHP STACK DUMP |# |Line # |File name |Function call |1 |82 |/Users/stephanedeluca/Desktop/phpdump.php |dumpPHPStack (null) |2 |87 |/Users/stephanedeluca/Desktop/phpdump.php |mélody◊صوفي () </pre>

I’m really happy about this.