They do the same thing. Unsetting the $pdo
handle and setting it null both close the connection.
You can test this for yourself. Run the following script in one window, and in a second window open the MySQL client and run SHOW PROCESSLIST
every couple of seconds to see when the connection disappears.
<?php
$pdo = new PDO(..);
sleep(10);
unset($pdo);
echo "pdo unset!
";
sleep(10);
Then change unset($pdo)
to $pdo=null;
and run the test again.
<?php
$pdo = new PDO(..);
sleep(10);
$pdo = null;
echo "pdo set null!
";
sleep(10);
The extra sleep()
at the end is there to give you a moment to see that the connection has dropped, before the PHP script terminates (which would drop the connection anyway).