150 likes | 337 Views
Introduction to PHP. Function Array. Function - 1. Build-in function Mathematics: sin, cos MySQL: mysql_connect(), mysql_query(),… String: strlen(), strcmp() File …. Self-built functions function add($a, $b) { return($a+$b); } Echo(add(10,10));. Function - 2. Call by reference
E N D
Introduction to PHP Function Array
Function - 1 • Build-in function • Mathematics: sin, cos • MySQL: mysql_connect(), mysql_query(),… • String: strlen(), strcmp() • File • …. • Self-built functions function add($a, $b) { return($a+$b); } Echo(add(10,10));
Function - 2 Call by reference function add() { //global $x, $y; $c=$y+$x; $x=20; $a=30; return($c`); } $x=10; $y=20; $z=add(); Echo(“$x $y $z”); << fun-t>>
Variable Range $f=10; test(); echo("<BR> $f"); // ? test1(); echo("<BR> $f"); // ? function test(){ $f=$f+10; return($f); } function test1(){ global $f; $f=$f+10; return($f); }
Recursive function function sum($n) { if ($n > 1) return($n+sum($n-1)); else return($n); } function Nsum($n) { $sum=0; for ($i=1;$i<=$n;$i++) $sum=$sum+$i; return($sum); }
Array • Continuous store space • One-dim • Two-dim • Three-dim Protein[0]=“1crn”; Protein[1]=“1jff”; … Protein[n]=“9rnt”; Student[0]=“moon” …
Array r0 R • Initial • C, P, N, O, S, H • $RR=array(4.0, 4.2, 3.5, 3.2, 4.0, 2.0); • $EE=array(0.15, 0.20, 0.16, 0.20, 0.2, 0.02); Count($RR) ATOM 80 N PRO 28 -2.060 -3.192 28.970 1.00 10.67 3PTB 190 ATOM 81 CA PRO 28 -1.712 -3.117 30.422 1.00 10.67 3PTB 191 ATOM 82 C PRO 28 -.517 -2.182 30.675 1.00 10.67 3PTB 192 ATOM 83 O PRO 28 -.243 -1.901 31.853 1.00 10.67 3PTB 193 ATOM 84 CB PRO 28 -1.262 -4.544 30.870 1.00 10.67 3PTB 194 ATOM 85 CG PRO 28 -1.145 -5.364 29.577 1.00 12.59 3PTB 195 ATOM 86 CD PRO 28 -1.704 -4.517 28.393 1.00 12.59 3PTB 196 $line_data[0]["atom"] = substr($line,12,4); $line_data[0]["x"] = substr($line,30,8); $line_data[0]["y"] = substr($line,38,8); $line_data[0]["z"] = substr($line,46,8); Advantages ?
Array • $RR=array(4.0, 4.2, 3.5, 3.2, 4.0, 2.0); • $EE=array(0.15, 0.20, 0.16, 0.20, 0.2, 0.02); For ($I=0;$I<6;$I++) $Trr=$Trr+$RR[$I]; $line_data["atom"] = substr($line,12,4); // can not use
Array - multiDim $twod[0][0]=“name1”; $twod[0][1]=“ID1”; $twod[0][2]=“ch1”; $twod[0][3]=“eng1”; $twod[1][0]=“name2”; $twod[1][1]=“ID2”; $twod[1][2]=“ch2”; $twod[1][3]=“eng2”; echo("<BR> $twod[0][0] $twod[0][2] "); echo("<BR>" . $twod[0][0] . $twod[0][2]);
A function function Scomp($SNum, $ENum) { global $s2, $s1; $SL1=strlen($SNum); $SL2=strlen($ENum); $i=0; while ($SNum[$i] && $ENum[$i] ) { if ($SNum[$i]==$ENum[$i]) { $s1=$s1. "<u>" . $SNum[$i]. "</u>"; $s2=$s2. "<u>" . $ENum[$i]. "</u>"; } else { $s1=$s1 . $SNum[$i]; $s2=$s2 . $ENum[$i]; } $i++; } }
Example & HW • String compare • String parsing • Vector length • Vector angle
String-1 • Substr(str, st, len) (from 0) • Substr(“Jinn-MoonYang”, 4, 2) -M • Substr(“Jinn-MoonYang”, 3, 5) ?? • Substr(“Jinn-MoonYang”, -3) ?? • Trim(str) • trim(“ Jinn-MoonYang ”) “Jinn-MoonYang” • Ltrim() • Rtrim() • Chr() and ord() • Strlen(), strcmp() • Others
String-2 • Printf • Sprintf • $FN=sprintf("tt-%s.dbf",$t); • echo("<BR> $FN"); //??
String-3 (Regular Expression) $rec="ATOM 79 CG2 VAL 27 -4.768 -1.303 25.181 1.00 7.28"; ^: start pattern if (ereg("^ATOM", $rec)) echo("<BR> the ATOM record 1 "); • $: end pattern • “ATOM$” • Char Set: • [0-9] [A-Z] [A-Za-Z] if (ereg("[a-z0-9]", $rec)) echo("<BR> the [A-Z] record ");
String-4 (Regular Expression) • *: {0,} • ?: {0,1} • ^[0-9]{1,}$ • ^.+@.+\\..+$ (.+: > 1 char) $email="moon@cc.nctu.edu.tw"; if (ereg("^.+@.+\\..+\\..+$",$email)) echo("<BR> correct e-mail format");