PHP中extract()函數的妙用

來源:互聯網
上載者:User

標籤:html   ons   方便   代碼   函數   bsp   tar   status   ext   

看cakephp 2.3.8的原始碼,很多地方都用 到

  compact(‘name‘, ‘response‘); 

extract($status, EXTR_OVERWRITE);

這樣的代碼。compact()函數之前有查過, extract()函數用的比較少。 原來extract()是將數組的 “解開”成一個一個散開的變數,哈哈哈!!!

----------------------------------------------------------------------------------

參考:http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2009/0723/3090.html

近日在看一個牛人的代碼時,看到一個非常好用的函數:extract(),它的主要作用是將數組展開,鍵名作為變數名,元素值為變數值,可以說為數組的操作提供了另外一個方便的工具,比方說,可以很方便的提取$_POST或者$_GET的元素,對錶單提交上來的內容不能不用一一賦值,直接使用下面代碼:

form.html


 <form action="action.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">

在action.php中只要使用extract()函數將$_POST全域資料解開:
action.php


 <?php
extract($_POST);
//相當於$username = $_POST[‘username‘];
//$password = $_POST[‘password‘];
?>

是不是很方便呢?呵呵

PHP中extract()函數的妙用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.