Functions in PHP: Part 1 - Writing Your Own Functions

Functions in PHP: Part 1 - Writing Your Own Functions

John Kary discusses 4 guidelines that if followed will result in clear, focused functions that are easy to read, reason about and reuse. Examples are shown in PHP.

D3e3f4ac37c02289f5dfed115949fc88?s=128

John Kary

July 29, 2015
Tweet

Transcript

  1. 2.

    <?php ! $hash = array( 'username' => 'john', 'food' =>

    'bacon', ); ! echo http_build_query($hash); ! // The above code would output... username=john&food=bacon
  2. 3.

    <?php ! $hash = array( 'username' => 'john', 'food' =>

    'bacon', ); ! echo http_build_query($hash); ! // The above code would output... username=john&food=bacon
  3. 8.
  4. 18.
  5. 19.
  6. 24.

    Given! List of all Users Users With J Names Expect!

    New list of Users whose name starts with J
  7. 29.

    foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  8. 30.

    foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  9. 31.

    foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  10. 32.

    Users With J Names Users With K Names Users With

    L Names Users With M Names Users With N Names Users With O Names
  11. 33.

    Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter
  12. 34.

    Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter Users Name Begins With
  13. 35.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  14. 36.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  15. 40.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  16. 41.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 1. Well Named
  17. 42.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 1. Well Named 2. Change vs Same
  18. 44.

    Integer! Float! Boolean! String! Array! Object 0 1 2 3!

    0.0 1.0 3.14! true false! "hello" ""! array() array('john', 'eric', 'dan')! DateTime User 3. One Return Type
  19. 45.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  20. 46.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  21. 47.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; } 3. One Return Type
  22. 49.

    <?php $a = 1; // global scope ! function test()

    { echo $a; // reference to function scope } ! test(); Global Scope
  23. 50.

    <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  24. 51.

    <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  25. 52.

    <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  26. 53.

    <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  27. 54.

    <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1
  28. 55.

    <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1 Avoid using global
  29. 56.

    <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1 Avoid using global and its friend $GLOBAL
  30. 60.
  31. 63.

    4. Control In and Out Control what goes in! and

    what comes out Return Arguments
  32. 64.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  33. 65.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 4. Control In and Out
  34. 66.

    function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 4. Control In and Out 3. One Return Type 1. Well Named 2. Change vs Same
  35. 68.
  36. 69.
  37. 70.
  38. 71.
  39. 72.
  40. 73.