中声明一个函数作为变量我开始学习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