Dhananjay Kuber

Day 6 at rtCamp

PHP Callback Function

  • A callback function is a function which is passed as an argument into another function.
  • eg:
<?php
function callbackFunc($item) {
  return strlen($item);
}

$strings = ["john", "denver", "david", "jack"];
$lengths = array_map("callbackFunc", $strings);
print_r($lengths);

/*

output:

Array
(
    [0] => 4
    [1] => 6
    [2] => 5
    [3] => 4
)

*/
?>

PHP & JSON

  • PHP has some built in functions to handle JSON.
  • json_encode: used to encode value to json format
<?php
    $user = array("Name"=>"John DOe", "Age"=>37);
    echo json_encode($user);
?>
  • json_decode: used to decode json to php associative array
<?php
    $json = '{"Name":"John Doe","Age":37}';
    var_dump(json_decode($json));
?>

Exception Handling

  • An exception is an object that describes an error or unexpected behaviour of a PHP script.
  • throw keyword allow user to throw an exception.
try {
  if (condition) {
    throw new Exception("Error here");
  }
} catch(Exception $e) {
  
} finally {

}
  • If any exception occured or thrown then the catch block executes.
  • finally block will always run.

Others

  • Apart from this I have added exception handling in Github Timeline assignment
  • Also did UI changes and Email templates.``