Skip to content

Instantly share code, notes, and snippets.

@gilbitron
Last active February 10, 2017 10:23
Show Gist options
  • Select an option

  • Save gilbitron/5020cb89a85f66075d2a4e0128d14631 to your computer and use it in GitHub Desktop.

Select an option

Save gilbitron/5020cb89a85f66075d2a4e0128d14631 to your computer and use it in GitHub Desktop.

Revisions

  1. gilbitron revised this gist Feb 10, 2017. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion example1.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    (function() use ($array) {
    // Add an item to the array
    $array[] = 'item';
    $array[] = 'item';
    })();

    var_dump($array);
    4 changes: 2 additions & 2 deletions example2.php
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    $array = [];

    (function() use (&$array) { // Notice the pass by reference
    // Add an item to the array
    $array[] = 'item';
    // Add an item to the array
    $array[] = 'item';
    })();

    var_dump($array);
  2. gilbitron revised this gist Feb 10, 2017. 2 changed files with 5 additions and 7 deletions.
    7 changes: 3 additions & 4 deletions example1.php
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,10 @@

    $array = [];

    $example = function() use ($array) {
    // Add an item to the array
    (function() use ($array) {
    // Add an item to the array
    $array[] = 'item';
    };
    $example();
    })();

    var_dump($array);

    5 changes: 2 additions & 3 deletions example2.php
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,10 @@

    $array = [];

    $example = function() use (&$array) { // Notice the pass by reference
    (function() use (&$array) { // Notice the pass by reference
    // Add an item to the array
    $array[] = 'item';
    };
    $example();
    })();

    var_dump($array);

  3. gilbitron created this gist Feb 10, 2017.
    19 changes: 19 additions & 0 deletions example1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php

    $array = [];

    $example = function() use ($array) {
    // Add an item to the array
    $array[] = 'item';
    };
    $example();

    var_dump($array);

    /*
    * Output:
    *
    * array(0) {
    * }
    *
    */
    20 changes: 20 additions & 0 deletions example2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    $array = [];

    $example = function() use (&$array) { // Notice the pass by reference
    // Add an item to the array
    $array[] = 'item';
    };
    $example();

    var_dump($array);

    /*
    * Output:
    *
    * array(1) {
    * [0] => string(4) "item"
    * }
    *
    */