Skip to content

Anonymous Self-Executing Functions in JavaScript and PHP

Nov 14, 2017 1 min read #javascript #php

This post is more than 18 months old. Technology changes quickly, so some of this content may be out of date (but that's not always the case). Please verify any technical or programming information against the current release.

I’ve used the anonymous self-executing paradigm a few times in JavaScript over the years. Something like this:

(function() {
  console.log('So running. Much anonymous.');
})();
javascript

Then I started thinking - can I do the same thing in PHP (not a question of should, a question of can!) Turns out, you can still do it - just a different method of self-execution is required:

<?php
call_user_func(function() {
  print "Oy - even PHP!";
});
php

That’s it for today!