生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn:...

23
生生生生生生生生生生 生生生生生生生生生生 Part 6 Part 6 PHP-introduction PHP-introduction
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    230
  • download

    0

Transcript of 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn:...

Page 1: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

生物資訊程式語言應用生物資訊程式語言應用Part 6Part 6

PHP-introductionPHP-introduction

Page 2: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

ObjectivesIn 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.

InternetInternetClient

Server

JavaScript, VBScript PHP, ASP, JSP

22

Page 3: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

ObjectivesIntroduction for PHP

Print

Operator

ProcessingConditional statement

Loop

Useful functionString function

MySQL connection

33

Page 4: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

IntroductionPHP

PHP: Hypertext Preprocessor

Originally called “Personal Home Page Tools”

1997, PHP3

Popular server-side scripting technology

Open-sourceAnyone may view, modify and redistribute source code

Supported freely by community

Platform independent

44

Page 5: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PHPEmbedded directly into XHTML documents

Without having to use multiple print statements (CGI)

Basic applicationScripting delimiters

<?php ?>Must enclose all script code

Variables preceded by $ symbolCase-sensitive ( 有大小寫之分的 )

End statements with semicolon “;”

Comments// for single line

/* */ for multiline

Filenames end with .php by convention

55

Page 6: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PHP hello world<html>

<head><title>web page title</title></head><body><?php

echo “hello world!”;?></body>

</html>

66

PracticePracticeOutput: hello world!

By PHP

http://localhost/php_practice/helloworld.php

Page 7: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PrintOutput string

echo “test”; / print “test”;

雙引號可以代換變數$a = “John";

echo “I am $a”;

echo “I am “.$a;

Output: 「 I am John 」

單引號 ’ ’裏面所包含所有字串都會直接顯示 , 不會代換

77

PracticePracticeecho "I am $a<BR>";

echo 'I am $a<BR>';

http://localhost/php_practice/practice_print.php

Page 8: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

operators

88

算術運算子

運算子 範例 用途

+ $a + $b $a 和 $b 的和。

- $a - $b $a 和 $b 的差。

* $a * $b $a 和 $b 的乘積。

/ $a / $b $a 除以 $b 的商。

% $a % $b$a 除以 $b 的餘

數。

基本的指定運算符就是” =“ 。並不稱做”等於”,實際上意味著把右邊運算式的值指定給左運算數。例如:$a = $a + 2;

指定運算子

位元運算子

範例 名稱 結果

$a & $b And (且) 將在 $a 和 $b 中都為 1 的位設為 1 。

$a | $b Or (或) 將在 $a 或者 $b 中為 1 的位設為 1 。

$a ^ $b Xor (互斥) 將在 $a 和 $b 中不同的位設為 1 。

~ $a Not (補數) 將 $a 中為 0 的位設為 1 ,反之亦然。

$a << $bShift left (左

移)將 $a 中的位向左移動 $b 次(每一次移動都表示“乘以 2” )。

$a >> $bShift right (右

移)將 $a 中的位向右移動 $b 次(每一次移動都表示“除以 2” )。

Page 9: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

99

範例 名稱 解釋

$a == $b 等於 TRUE ,如果 $a 等於 $b 。

$a === $b 全等 TRUE ,如果 $a 等於 $b ,並且它們的類型也相同。 (PHP 4 only)

$a != $b 不等 TRUE ,如果 $a 不等於 $b 。

$a <> $b 不等 TRUE ,如果 $a 不等於 $b 。

$a !== $b 非全等 TRUE ,如果 $a 不等於 $b ,或者它們的類型不同。 (PHP 4 only)

$a < $b 小與 TRUE ,如果 $a 小於 $b 。

$a > $b 大於 TRUE ,如果 $a 大於 $b 。

$a <= $b 小於等於 TRUE ,如果 $a 小於或者等於 $b 。

$a >= $b 大於等於 TRUE ,如果 $a 大於或者等於 $b 。

比較運算子

執行運算子<?php $output = `dir`; echo “<pre> $output </pre>; ?>

PHP 支持一個執行運算符:反引號「 ` .... ` 」。注意這不是單引號! PHP 將嘗試將反引號中的內容作為 shell 命令來執行,並將其輸出資訊返回(例如,可以賦給一個變數而不是簡單地丟棄到標準輸出)。

加一/減一運算子範例 名稱 解釋

++$a 前加 $a 的值加一,然後返回 $a 。

$a++ 後加 返回 $a ,然後將 $a 的值加一。

--$a 前減 $a 的值減一, 然後返回 $a 。

$a-- 後減 返回 $a ,然後將 $a 的值減一。

邏輯運算子範例 名稱 解釋

$a and $bAnd (邏輯

與)TRUE ,如果 $a 與 $b 都為 TRUE 。

$a or $bOr (邏輯

或)TRUE ,如果 $a 或 $b 任一為 TRUE 。

$a xor $bXor (邏輯互

斥)TRUE ,如果 $a 或 $b 任一為 TRUE ,但不同時是。

! $aNot (邏輯

非)TRUE ,如果 $a 不為 TRUE 。

$a && $bAnd (邏輯

與)TRUE ,如果 $a 與 $b 都為 TRUE 。

$a || $bOr (邏輯

或)TRUE ,如果 $a 或 $b 任一為 TRUE 。

Variable (con.)

Page 10: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PracticePracticePractice 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

1010

Page 11: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Conditional statementif (condition)

{

statement;

}

if (condition)

{

statement1;

}

else

{

statement2;

}

1111

$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

Page 12: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Conditional statement(con.)if ( condition 1)

{

statement 1;

}

elseif (condition 2)

{

statement 2;

}

else

{

statement 3;

}

1212

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

Page 13: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Conditional statement(con.)switch ( switch condition )

{

case 'value 1' : statement 1;

break;

case 'value 2' : statement 2;

break;

......

default : statement n;

break;

}

1313

$a = "2";

switch ($a){

case '1';echo " 冠軍 ";break;

case '2';echo " 亞軍 ";break;

case '3';echo " 季軍 "; break;

}

Print : 亞軍

PracticePracticeTake off those “break”

http://localhost/php_practice/practice_switch.php

Page 14: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Loopwhile ( condition )

{

statement;

}

do {

statement;

} while ( condition)

for (start; end; step)

{

statement;

}

1414

$a=1;while ($a<10){

echo "$a<BR>";$a++;

}

$a=1;do{

echo "$a<BR>";$a++;

} while ($a<10)

For($a=1;$a<10;$a++){

echo "$a<BR>";}

PracticPracticee

Page 15: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Loopcontinue: causes the iteration to be skipped.

break: causes the loop to stop and program execution to begin at the statement immediately following the loop.

1515

PracticePracticePrint : 1 3 5 7 9

Print : 2 4 6 8

http://localhost/php_practice/practice_continue.php

Page 16: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

String processingstrlen

Returns the length of the given string .

int strlen(string str);

substrReturns the portion of string specified by the start and length parameters.

string substr(string string, int start, int [length]);

strip_tagsThis function tries to return a string with all HTML and PHP tags stripped from a given str .

string strip_tags(string str);1616

Page 17: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

String processing http://vawidea.org/php%20bible/

ltrimStrip whitespace (or other characters) from the beginning of a string.

string ltrim(string str);

trimThis 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);1717

Page 18: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PracticePracticePractice.

$a = “<sequence>atacgatcgaagagatatatacgcatc</sequence>”;

print $a length

Print the “atacgatcgaagagatatatacgcatc”;

http://localhost/php_practice/practice_string.php

1818

Page 19: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

Frame姓名 :<input name="name" type="text" maxlength="10">

$name = $_POST["name"];

1919

PracticePracticeOutput:

Page 20: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

MySQL connectionmysql_connect

Opens or reuses a connection to a MySQL server. mysql_connect(address, name, password);

Examplemysql_connect("localhost","root","");

mysql_select_dbSelect a MySQL database

mysql_select_db(“database name”);

Examplemysql_select_db(“sequence”);

2020

Page 21: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

MySQL connection(con.)mysql_query

Send a MySQL querymysql_query(“sql query”);

example$SQL = “select * from subject”;$result = mysql_query(SQL);

mysql_fetch_rowGet a result row as an enumerated arrayExample:

2121

while($row = mysql_fetch_row($result)) { for($i=0;$i<count($row);$i++ ) { echo $row[$i].“|"; } }

Page 22: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

MySQL connection(con.)mysql_fetch_array

Fetch a result row as an associative array, a numeric array, or bothExample:

2222

$total=mysql_num_rows($result); for($index=1;$index<=$total;$index++){

$arr[$index]=mysql_fetch_array($result);echo $arr[$index]['title'];

}

Page 23: 生物資訊程式語言應用 Part 6 PHP-introduction. Objectives In this chapter, you will learn: –To understand PHP data types, operators, arrays and control structures.

PracticePracticePractice.

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

2323