230 likes | 232 Views
生物資訊程式語言應用 Part 6. PHP-introduction. Objectives. In this chapter, you will learn: To understand PHP data types, operators, arrays and control structures. To understand string processing and regular expressions in PHP. To construct programs that interact with MySQL databases. Internet.
E N D
生物資訊程式語言應用Part 6 PHP-introduction
Objectives In this chapter, you will learn: • To understand PHP data types, operators, arrays and control structures. • To understand string processing and regular expressions in PHP. • To construct programs that interact with MySQL databases. Internet Server Client JavaScript, VBScript PHP, ASP, JSP
Objectives • Introduction for PHP • Print • Operator • Processing • Conditional statement • Loop • Useful function • String function • MySQL connection
Introduction • PHP • PHP: Hypertext Preprocessor • Originally called “Personal Home Page Tools” • 1997, PHP3 • Popular server-side scripting technology • Open-source • Anyone may view, modify and redistribute source code • Supported freely by community • Platform independent
PHP • Embedded directly into XHTML documents • Without having to use multiple print statements (CGI) • Basic application • Scripting delimiters • <?php ?> • Must enclose all script code • Variables preceded by $ symbol • Case-sensitive (有大小寫之分的) • End statements with semicolon “;” • Comments • // for single line • /* */ for multiline • Filenames end with .php by convention
PHP hello world <html> <head> <title>web page title</title> </head> <body><?php echo “hello world!”; ?> </body></html> • Practice • Output: hello world! • By PHP • http://localhost/php_practice/helloworld.php
Print • Output string • echo “test”; / print “test”; • 雙引號可以代換變數 • $a = “John"; • echo “I am $a”; • echo “I am “.$a; • Output:「I am John」 • 單引號 ’ ’裏面所包含所有字串都會直接顯示,不會代換 • Practice • echo "I am $a<BR>"; • echo 'I am $a<BR>'; • http://localhost/php_practice/practice_print.php
operators 位元運算子 • 算術運算子 指定運算子 基本的指定運算符就是”=“。並不稱做”等於”,實際上意味著把右邊運算式的值指定給左運算數。例如: $a = $a + 2;
Variable (con.) 加一/減一運算子 比較運算子 邏輯運算子 執行運算子 <?php $output = `dir`;echo “<pre> $output </pre>; ?> PHP 支持一個執行運算符:反引號「` .... `」。注意這不是單引號!PHP 將嘗試將反引號中的內容作為shell命令來執行,並將其輸出資訊返回(例如,可以賦給一個變數而不是簡單地丟棄到標準輸出)。
Practice • Practice 1. • $a= 4; • $b= 6; • $c= $a + $b; • $a++; • $b=$b%2; • $c= $a + $b; • http://localhost/php_practice/practice_variable_1.php • Practice 2. • Please show me the “dir” result in command mode. • http://localhost/php_practice/practice_variable_2.php
Conditional statement if (condition) { statement; } if (condition) { statement1; } else { statement2; } • $a = "123"; • if($a = = 123) • { • echo "A= $a"; • } • Print : A= 123 • $a = "123"; • if($a < 100) • { • echo "A < 100"; • } • else • { • echo "A > 100"; • } • Print : A> 123
Conditional statement(con.) if ( condition 1) { statement 1; } elseif (condition 2) { statement 2; } else { statement 3; } if ($a < 100) { echo "A < 100"; } elseif ($a > 100 && $a <200) { echo "A > 100 and A < 200"; } else { echo "A > 200"; } Print : A > 100 and A < 200
Conditional statement(con.) switch ( switchcondition) { case 'value 1' : statement 1; break; case 'value 2' : statement 2; break; ...... default : statement n; break; } $a = "2"; switch ($a) { case '1'; echo "冠軍"; break; case '2'; echo "亞軍"; break; case '3'; echo "季軍"; break; } Print : 亞軍 • Practice • Take off those “break” • http://localhost/php_practice/practice_switch.php
Loop $a=1; while ($a<10) { echo "$a<BR>"; $a++; } while ( condition) { statement; } do { statement; } while ( condition) for (start; end; step) { statement; } $a=1; do{ echo "$a<BR>"; $a++; } while ($a<10) For($a=1;$a<10;$a++) { echo "$a<BR>"; } Practice
Loop • continue: causes the iteration to be skipped. • break: causes the loop to stop and program execution to begin at the statement immediately following the loop. • Practice • Print : 1 3 5 7 9 • Print : 2 4 6 8 • http://localhost/php_practice/practice_continue.php
String processing • strlen • Returns the length of the given string . • int strlen(string str); • substr • Returns the portion of string specified by the start and length parameters. • string substr(string string, int start, int [length]); • strip_tags • This function tries to return a string with all HTML and PHP tags stripped from a given str . • string strip_tags(string str);
String processing http://vawidea.org/php%20bible/ • ltrim • Strip whitespace (or other characters) from the beginning of a string. • string ltrim(string str); • trim • This function returns a string with whitespace stripped from the beginning and end of str. • trim() will strip these characters: • " " (ASCII 32 (0x20)), an ordinary space. • "\t" (ASCII 9 (0x09)), a tab. • "\n" (ASCII 10 (0x0A)), a new line (line feed). • "\r" (ASCII 13 (0x0D)), a carriage return. • "\0" (ASCII 0 (0x00)), the NUL-byte. • "\x0B" (ASCII 11 (0x0B)), a vertical tab. • string trim(string str);
Practice • Practice. • $a = “<sequence>atacgatcgaagagatatatacgcatc</sequence>”; • print $a length • Print the “atacgatcgaagagatatatacgcatc”; • http://localhost/php_practice/practice_string.php
Frame • 姓名:<input name="name" type="text" maxlength="10"> • $name = $_POST["name"]; • Practice • Output:
MySQL connection • mysql_connect • Opens or reuses a connection to a MySQL server. • mysql_connect(address, name, password); • Example • mysql_connect("localhost","root",""); • mysql_select_db • Select a MySQL database • mysql_select_db(“database name”); • Example • mysql_select_db(“sequence”);
MySQL connection(con.) • mysql_query • Send a MySQL query • mysql_query(“sql query”); • example • $SQL = “select * from subject”; • $result = mysql_query(SQL); • mysql_fetch_row • Get a result row as an enumerated array • Example: • while($row = mysql_fetch_row($result)) • { • for($i=0;$i<count($row);$i++ ) • { • echo $row[$i].“|"; • } • }
MySQL connection(con.) • mysql_fetch_array • Fetch a result row as an associative array, a numeric array, or both • Example: • $total=mysql_num_rows($result); • for($index=1;$index<=$total;$index++) • { • $arr[$index]=mysql_fetch_array($result); • echo $arr[$index]['title']; • }
Practice • Practice. • Insert a sequence into the MySQL database by phpmyadmin. • Create a database • Create a table • Insert a pseudosequence • Delete the sequence by php code. • http://localhost/php_practice/practice_delete.php