版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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.ClientServerJavaScript, VBScriptPHP, ASP, JSPObjectivesIntroduct
2、ion for PHPPrintOperatorProcessingConditional statementLoopUseful functionString functionMySQL connectionIntroductionPHPPHP: Hypertext PreprocessorOriginally called “Personal Home Page Tools”1997, PHP3Popular server-side scripting technologyOpen-sourceAnyone may view, modify and redistribute source
3、codeSupported freely by communityPlatform independentPHPEmbedded directly into XHTML documentsWithout having to use multiple print statements (CGI)Basic applicationScripting delimitersMust enclose all script codeVariables preceded by $ $ symbolCase-sensitive (有大小寫之分的)End statements with semicolon “;
4、”Comments/ for single line/* */ for multilineFilenames end with .php by convention7 PHP hello worldweb page titleOutput: hello world!By PHPhttp:/localhost/php_practice/helloworld.php PrintOutput stringecho “test”; / print “test”; 雙引號可以代換變數$a = “John;echo “I am $a”;echo “I am “.$a;Output:I am John單引號
5、 裏面所包含所有字串都會直接顯示,不會代換echo I am $a;echo I am $a;http:/localhost/php_practice/practice_print.php operators算術運算子算術運算子 運算子運算子範例範例用途用途+$a + $b$a 和 $b 的和。-$a - $b$a 和 $b 的差。*$a * $b$a 和 $b 的乘積。/$a / $b$a 除以 $b 的商。%$a % $b$a 除以 $b 的餘數。基本的指定運算符就是”=“。並不稱做”等於”,實際上意味著把右邊運算式的值指定給左運算數。例如:$a = $a + 2;指定運算子指定運算子位元
6、運算子位元運算子 範例範例名稱名稱結果結果$a & $bAnd(且)將在 $a 和 $b 中都為 1 的位設為 1。$a | $bOr(或)將在 $a 或者 $b 中為 1 的位設為 1。$a $bXor(互斥)將在 $a 和 $b 中不同的位設為 1。 $aNot(補數)將 $a 中為 0 的位設為 1,反之亦然。$a $bShift right(右移)將 $a 中的位向右移動 $b 次(每一次移動都表示“除以 2”)。範例範例名稱名稱解釋解釋$a = $b等於TRUE,如果 $a 等於 $b。$a = $b全等TRUE,如果 $a 等於 $b,並且它們的類型也相同。(PHP 4 o
7、nly)$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。比較運算子比較運算子執行運算子執行運算子?php $output = dir; echo “ $output ; ? PHP 支持一個執行運算符:反引號 . 。注意這不是單引號!PHP 將嘗試將反引號中的內容作為shell命令來執行,並將其輸出資訊返回(例如,可以賦給一個
8、變數而不是簡單地丟棄到標準輸出)。 加一減一運算子加一減一運算子範例範例名稱名稱解釋解釋+$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(邏輯非)TRU
9、E,如果 $a 不為 TRUE。$a & $bAnd(邏輯與)TRUE,如果 $a 與 $b 都為 TRUE。$a | $bOr(邏輯或)TRUE,如果 $a 或 $b 任一為 TRUE。Variable (con.)Practice 1.$a= 4;$b= 6;$c= $a + $b;$a+;$b=$b%2;$c= $a + $b;http:/localhost/php_practice/practice_variable_1.phpPractice 2.Please show me the “dir” result in command mode.http:/localhost/p
10、hp_practice/practice_variable_2.phpConditional statementif (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;Print : A 123Conditional statement(con.)if ( condition 1) statement 1; elseif (condition 2) sta
11、tement 2; else statement 3; if ($a 100)echo A 100 & $a 100 and A 200;Print : A 100 and A 200Conditional statement(con.)switch ( switch condition ) 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;ec
12、ho 亞軍;break;case 3;echo 季軍; break;Print : 亞軍亞軍Take off those “break”http:/localhost/php_practice/practice_switch.php Loopwhile ( condition ) statement; do statement; while ( condition)for (start; end; step) statement; $a=1;while ($a10)echo $a;$a+;$a=1;doecho $a;$a+; while ($a10)For($a=1;$a10;$a+)ech
13、o $a;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. Print : 1 3 5 7 9Print : 2 4 6 8http:/localhost/php_practice/practice_continue.php String processingstrlenReturns the length of the giv
14、en 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);
15、String processing /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 (0 x20), an
16、 ordinary space. t (ASCII 9 (0 x09), a tab. n (ASCII 10 (0 x0A), a new line (line feed). r (ASCII 13 (0 x0D), a carriage return. 0 (ASCII 0 (0 x00), the NUL-byte. x0B (ASCII 11 (0 x0B), a vertical tab. string trim(string str);Practice.$a = “atacgatcgaagagatatatacgcatc”;print $a lengthPrint the “atac
17、gatcgaagagatatatacgcatc”;http:/localhost/php_practice/practice_string.phpFrame姓名: $name = $_POSTname; Output: MySQL connectionmysql_connectOpens or reuses a connection to a MySQL server. mysql_connect(address, name, password);Examplemysql_connect(localhost,root,);mysql_select_dbSelect a MySQL databa
18、semysql_select_db(“database name”);Examplemysql_select_db(“sequence”);MySQL connection(con.)mysql_querySend 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:while($row = mysql_fetch_row($result) for($i=0;$icount($row);$i+ ) echo $row$i.“|; MySQL connection(con.)mysql_fetch_arrayFetch a result row as an associative array, a numeric array, or bothExample:$total=mysql_num_rows($result);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年邮政物流仓储管理培训题库
- 体裁教学法在初中英语阅读教学中的应用调查研究
- 基于控制参数正交分析的交叉口信号优化算法研究
- 基于时间序列分类的电能质量异常诊断方法及应用
- 2026年人力资源租赁软件开发合同
- 2026年电商投放分销代理合同
- 楼梯施工交叉作业组织方案
- 四年级数学下册《小数乘整数:买文具》差异化教学方案
- 2026湖北武汉市第二十九中学招聘高中数学教师1人考试备考试题及答案解析
- 2026江苏淮安市清江浦区淮海街道公益性岗位招聘5人笔试备考题库及答案解析
- 《基础会计学》教学课件-陈国辉、迟旭升-东北财大出版
- TQGCML 3946-2024 柴油发电机组维护保养规范
- 2024广东省高考政治真题卷及答案
- DL∕T 1053-2017 电能质量技术监督规程
- 红十字志愿者培训讲义
- (高清版)JTST 206-1-2023 水运工程塑料排水板应用技术规程
- 内镜护士进修汇报
- 项目推进缓慢表态发言稿三篇
- 高原铁路隧道斜井通风设备配置优化研究
- 2024年西藏开发投资集团有限公司招聘笔试参考题库含答案解析
- 第二章-生命的物质基础
评论
0/150
提交评论