Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Functions in PHP: Part 1 - Writing Your Own Fun...

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.

John Kary

July 29, 2015
Tweet

More Decks by John Kary

Other Decks in Programming

Transcript

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

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

    'bacon', ); ! echo http_build_query($hash); ! // The above code would output... username=john&food=bacon
  3. Given! List of all Users Users With J Names Expect!

    New list of Users whose name starts with J
  4. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  5. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  6. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  7. Users With J Names Users With K Names Users With

    L Names Users With M Names Users With N Names Users With O Names
  8. Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter
  9. Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter Users Name Begins With
  10. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  11. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  12. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  13. 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
  14. 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
  15. 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
  16. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  17. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  18. 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
  19. <?php $a = 1; // global scope ! function test()

    { echo $a; // reference to function scope } ! test(); Global Scope
  20. <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  21. <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  22. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  23. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  24. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1
  25. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1 Avoid using global
  26. <?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
  27. 4. Control In and Out Control what goes in! and

    what comes out Return Arguments
  28. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  29. 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
  30. 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