$life=42; function meaningOfLife() { global $life; /*在此处重新声明$life为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,将在所有代码片段改变*/ print "The meaning of life is $life "; } meaningOfLife(); ?>
8) 判断函数是否存在:function_exists(function_name),参数为函数名 10、 用PHP连接MySQL 1) 连接:&conn=mysql_connect("localhost", "joeuser", "somepass"); 2) 关闭连接:mysql_close($conn); 3) 数据库与连接建立联系:mysql_select_db(database name, connection index); 4) 将SQL语句给MySQL执行:$result = mysql_query($sql, $conn); //增删改查都是这句 5) 检索数据:返回记录数:$number_of_rows = mysql_num_rows($result); 将记录放入数组:$newArray = mysql_fetch_array($result); 例子: // open the connection $conn = mysql_connect("localhost", "joeuser", "somepass"); // pick the database to use mysql_select_db("testDB",$conn); // create the SQL statement $sql = "SELECT * FROM testTable"; // execute the SQL statement $result = mysql_query($sql, $conn) or die(mysql_error()); //go through each row in the result set and display data while ($newArray = mysql_fetch_array($result)) { // give a name to the fields $id = $newArray['id']; $testField = $newArray['testField']; //echo the results onscreen echo "The ID is $id and the text is $testField "; } ?> 11、 接受表单元素:$_POST[表单元素名], 如ó$_POST[user] 接受url中queryString中值(GET方式):$_GET[queryString] 12、转向其他页面:header("Location: http://www.samspublishing.com"); 13、字符串操作: 1)explode(“-”,str)óJava中的splite 2)str_replace($str1,$str2,$str3) =>$str1要查找的字符串,$str2用来替换的字符串,$str3从这个字符串开始查找替换 3)substr_replace: 14、session: 1)打开session:session_start(); //也可以在php.ini设置session_auto_start=1,不必再每个script都写这句,但是默认为0,则必须要写。 2)给session赋值:$_SESSION[session_variable_name]=$variable; 3)访问session:$variable =$_SESSION[session_variable_name]; 4)销毁session:session_destroy(); 15、显示分类的完整例子: //connect to database $conn = mysql_connect("localhost", "joeuser", "somepass") or die(mysql_error()); mysql_select_db("testDB",$conn) or die(mysql_error()); $display_block = "
My Categories
Select a category to see its items.
"; //show categories first $get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) $display_block = "
"; if ($_GET[cat_id] == $cat_id) { //选择一个分类,看下面的条目 //get items $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title"; $get_items_res = mysql_query($get_items) or die(mysql_error()); if (mysql_num_rows($get_items_res) $display_block = "
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn