Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the function free() do in PHP?

Tags:

php

In the first code example on this page, this is the second last function called.

The comment says it frees the result set, but I am not able to understand what exactly is meant by freeing a result set? The result set is an Object so what is meant by freeing it?

/* free result set */
$result->free();
like image 974
Jesss Avatar asked Oct 26 '25 21:10

Jesss


1 Answers

The creation of a result set is a form of dynamic memory allocation, almost certainly done when an SQL query was executed, something like:

$conn = some_connection_to_database();
$rows = $conn->query("select name from etc_passwd where group = 'admin'")

Since the intent is for you to then use that result set, it cannot be freed as part of the query itself.

So, once the query is run and the result set is returned, you are responsible for freeing that memory when you're done with it.

That is where free() will be used. The basic idea (in English) is:

  • Get result set from somewhere.
  • Use it.
  • Return it to its source (this is the free bit).
like image 87
paxdiablo Avatar answered Oct 28 '25 12:10

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!