中聲明一個函數作爲變量我開始學習php和我的問題,理解在很多情況下函數被定義爲變量。任何人都可以解釋我它是什麼和例子背後的實際目的:爲什麼在php
$db_name= "testdb";
$table_name= "my_music";
// below we create the connection to the database.
$connection= @mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
// below we are gonna define what database to connect.
$db= @mysql_select_db($db_name, $connection) or die(mysql_error());
//below creates the sql statement so it can insert the data into the table.
$sql= "
INSERT INTO $table_name
(format, title, artist_fn, artist_ln, rec_label, my_notes, date_acq)
VALUES
('$format', '$title', '$artist_fn', '$artist_ln', '$rec_label', '$my_notes', '$date_acq')";
//below is the function that holds the result of the query function
$result= @mysql_query($sql, $connection) or die(mysql_error());
// end of the php for inserting data into database.
?>
正如你在這裏看到的$db
是不以任何下列代碼使用這個代碼是完全連接到數據庫,並做的工作。什麼是替代方式來幫助我更好地理解它。我一直在網上搜索,但找不到任何有用的答案 在此先感謝。
以學習[PDO](http://php.net/pdo)開始。 – 2013-03-13 07:53:40
這不是「將函數聲明爲變量」,它只是將函數的返回值賦給變量。顯然這是沒有必要的,因爲它從來沒有用過,就像你說的那樣。 – deceze 2013-03-13 07:55:31
你有什麼具體的不明白?你能說出來嗎? – hakre 2013-03-13 07:56:42