Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

The Die and Exit Function in PHP

Please is there any video lessons here on treehouse on the die and exit functions.

1 Answer

jamesjones21
jamesjones21
9,260 Points

a die and dump function is quite simple to make and understand:

you can call the dd (die and dump) function when you want to use it, pass it a value. It will output the value and then stop processing further code.

The pre tags outputs the contents in a readable format:

function dd ($data) {
    echo '<pre>';
      var_dump($data);
   echo '</pre>';
  die;

}

working example:

$data = [1,2,3,4,5];

function dd($data) {
    echo '<pre>';
        var_dump($data);
    echo '</pre>';
    die;

}

dd($data);
echo "Don't forget to output me";

Outputs the following

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}

So once the function is called the echo below it does not get called as the script has stopped. Hope this makes sense ?

Kind regards,

James