php中的釋放“語句”unset和釋放“函數”mysql_free_result()

來源:互聯網
上載者:User
文章目錄
  • 文法
首先要強調的一點是unset在php中已經不再是一個函數了,既然不是函數,那麼就沒有了傳回值,所以用的時候不能夠用unset的傳回值來做判斷。

其次,在函數中,unset只能銷毀局部變量,並不能銷毀全域變量,來看下手冊的一個例子

<?php
function destroy_foo() {
global $foo;
unset($foo);
}

$foo = ‘bar’;
destroy_foo();
echo $foo;
?>

返回的結果為

bar

為什麼會這樣呢?原因就是unset在函數中只能銷毀局部變數。如果在程式中需要用到銷毀全域變數的應該如何做呢?也很簡單,用$GLOBALS數組來實現。看下面的例子:

<?php
function foo() {
unset($GLOBALS['bar']);
}

$bar = “something”;
foo();
var_dump($bar);
?>

 

 

 

mysql_free_result()

定義和用法

mysql_free_result() 函數釋放結果記憶體。

如果成功,則返回 true,如果失敗,則返回 false。

文法
mysql_free_result(data)
參數 描述
data 必需。要釋放的結果標識符。該結果標識符是從 mysql_query() 返回的結果。
提示和注釋

注釋:mysql_free_result() 僅需要在考慮到返回很大的結果集時會佔用多少記憶體時調用。在指令碼結束後所有關聯的記憶體都會被自動釋放。

例子
<?php$con = mysql_connect("localhost", "peter", "abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }$db_selected = mysql_select_db("test_db",$con);$sql = "SELECT * from Person";$result = mysql_query($sql,$con);print_r(mysql_fetch_row($result));// 釋放記憶體mysql_free_result($result);$sql = "SELECT * from Customers";$result = mysql_query($sql,$con);print_r(mysql_fetch_row($result));mysql_close

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.