Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The best programming language

Name: Anonymous 2012-09-15 19:27

What's the best programming language?

Name: Anonymous 2012-09-16 2:51

>>16
I don't need  to defend PHP. It has lots of warts. The point I'm making is closures in PHP > closures in JavaScript. Observe:

PHP:

$callbacks = array();

for ($n = 1; $n <= 3; $n++) {
    $callbacks[] = function() use ($n) {
        print $n . PHP_EOL;
    };
}

foreach ($callbacks as $callback) {
    call_user_func($callback);
}


The above will print the numbers 1 2 3 each on its own line. Nothing surprising there.

JavaScript:

callbacks = [];

for (n = 1; n <= 3; n++) {
  callbacks[n-1] = function() {
    print(n);
  };
}

for (i in callbacks) {
  callbacks[i].apply();
}


While the seemingly equivalent JavaScript will print 4 4 4 like a retard. PHP in this regard is better for observing the principle of least astonishment. The PHP version can be easily converted into retard mode (which has it uses) by doing:

use (&$n)


instead.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List