Prof. Okhwa Lee (Chungbuk National University, Korea) [email protected]
Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Language basic)
-
Upload
alyson-byrd -
Category
Documents
-
view
222 -
download
0
description
Transcript of Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Language basic)
Chungbuk HRDI of KCCIPhD Kang,Won-Chan
PHP Programming(Language basic)
PHP ProgrammingPHP Programming
- - 22 - -
example<html><head><title> Hello PHP!</title></head><body><?php
$var_a=3500;echo("var_a : $var_a <br>");$var_b="hello";echo("var_b : $var_b <br>");$var_b = "200";$var_result = $var_a+ $var_b;echo("var_b : $var_b <br>");echo("sum : $var_result");
?></body></html>
PHP ProgrammingPHP Programming
- - 33 - -
Variable type<html><head><title> Hello PHP!</title></head><body><?php
$var_a=350.0;$type = gettype($var-a);echo("real var_a's value is $var_a, variable is {$type} <br>");$var_a=(int)$var_a;$type=gettype($var_a);echo("integer var_a's valu is $var_a, variable is {$type} <br>");settype($var_a,"double");$type = gettype($var_a);echo("integer var_a's variable is {$type} <br>");
?></body></html>
PHP ProgrammingPHP Programming
- - 44 - -
array<html><head><title> Hello PHP!</title></head><body><?php
$name[]="kang";$name[]="kim";$name["ceo"]="choi";$name[]="lee";echo("$name[0] - $name[1] -$name[ceo] -$name[2]");
?></body></html>
PHP ProgrammingPHP Programming
- - 55 - -
array<html><head><title> Hello PHP!</title></head><body><?php
$member[0][0]="kang";$member[0][1]="kim";$member[1][0]="choi";$member[1][1]="lee";
echo("$member[0][1]<br>");echo("{$member[1][0]}<br>");echo $member[1][1] . "<br>";
?></body></html>
PHP ProgrammingPHP Programming
- - 66 - -
array<?php
$member["ceo"]["age"]=48;$member["ceo"]["sex"]="F";$member["ceo"]["name"]="Kang, won-chan";
$member["cto"]["age"]=35;$member["cto"]["sex"]="M";$member["cto"]["name"]="Go, Min-Jung";
$member["cio"]["age"]=23;$member["cio"]["sex"]="M";$member["cio"]["name"]="Kim, Ji-Suk";
echo $member[cto][name];
?>
PHP ProgrammingPHP Programming
- - 77 - -
array<?
$member=array("ceo"=>array("age"=>48,"sex"=>"F","name"=>"Kang, Won-Chan"),"cto"=>array("age"=>35,"sex"=>"M","name"=>"Ko, Min-Jung"),"cio"=>array("age"=>23,"sex"=>"M","name"=>"Kim, Ji-Suk"));
echo $member[cto][name];?>
PHP ProgrammingPHP Programming
- - 88 - -
Function call<?
class Car { function testCar() {
echo("call to testCar function!");
}}
$sedan = new Car;$sedan->testCar();
?>
PHP ProgrammingPHP Programming
- - 99 - -
Global variableFalse case
<?$a=100;Function Test(){ echo("variable \$a's value is {$a}"); } Test();
?>
True case<?
$a=100;Function Test(){
GLOBAL $a; echo("variable \$a's value is {$a}"); } Test();
?>
PHP ProgrammingPHP Programming
- - 1010 - -
Global variablecase1
<?$a=100;$b=200;function Sum(){ global $a, $b; $b=$a+$b; } Sum(); echo "Variable b's value is ".$b."!";
?>
case2<?
$a=100;$b=200;function Sum(){ $GLOBALS["b"]=$GLOBALS["a"] +$GLOBALS["b"]; } Sum(); echo "Variable b's value is ".$b."!";
?>
PHP ProgrammingPHP Programming
- - 1111 - -
Static variable<?
function countIt() { static $i =10; echo "Variable value is " .$i. "!! <br>"; $i=$i+2; } countIt(); countIt(); countIt();
?>
<?function countIt() { static $i =10; echo "Variable value is " .$i. "!! <br>"; $i=$i+2; } countIt(); countIt(); countIt(); echo "Variable value is " .$i. "!! <br>";
?>
PHP ProgrammingPHP Programming
- - 1212 - -
<?$author ="Jay";${$author}="Kang, Won-Chan";
echo "author :".$author. "<br>";echo "Jay :".${author}. "<br>";echo "Jay :".${$author}. "<br>";echo "Jay :" .$Jay."<br>";
?>
PHP ProgrammingPHP Programming
- - 1313 - -
POST methodTest.php<html><body><form method="POST" action ="formcgi.php">Input Name: <input type="text" name="username"><br>Input Sex : <input type="text" name="sex"><input type="submit" value="transmit"></form></body></html>
Formcgi.php<?
echo("Your name is <b> $username</b> <p>");echo("Your sex is <b> $sex</b> <p>!!");
?>
PHP ProgrammingPHP Programming
- - 1414 - -
//test2.php<html><body><form method="POST" action ="formcgi2.php?table=BBS&code=jcafe">Input Name: <input type="text" name="username"><br>Input Sex : <input type="text" name="sex"><input type="submit" value="transmit"></form></body></html>
//formcgi2.php<? echo("Your name is <b> $username
</b> ");echo("Your sex is <b> $sex </b> <p>!!");echo("POST Method....<br>");while(list($key,$value)=each($HTTP_POST_VARS)){ echo("variable name: $key, value:$value<br>");}echo("GET Method....<br>");while(list($key,$value)=each($HTTP_GET_VARS)){ echo("variable name: $key, value:$value<br>");}
?>
PHP ProgrammingPHP Programming
- - 1515 - -
expressions<?
$a=12;$b=5.2;$res=$a+$b;echo("res by addition :$res<br>");$res=$a-$b;echo("res by subtraction : $res<br>");$res=$a*$b;echo("res by multiplication : $res<br>");$res=$a/$b;echo("res by division : $res<br>");$res=$a%$b;echo("res by modulus : $res<br>");if($a<$b){ echo("\$b is greater than \$a"); }else{ echo("\$a is greater than \$b"); }
?>
PHP ProgrammingPHP Programming
- - 1616 - -
<.> operator<?
$res="hello?"."nice to meet you";echo "result is=>".$res;
?>
PHP ProgrammingPHP Programming
- - 1717 - -
Logical Operator<? $a=5;
$b="";if($a && $b) {
echo "true";} else { echo "false“; }echo("<br>");if($a or $b){echo "true";} else { echo "false“; }echo("<br>");if($a xor $b){echo "true";} else { echo "false“; }echo("<br>");if(!$b){echo "true";} else { echo "false“; }
?>
PHP ProgrammingPHP Programming
- - 1818 - -
If~else<?
$a=12;$b=20;if($a>$b){ echo ("a is great then b<br>");} else if ($a<$b) { echo ("b is great then a <br>");} else { echo "(a=b<br>)";}
?>
<?$a=12;$b=20;if($a>$b) echo ("a is great then b<br>"); else if ($a<$b) echo ("b is great then a <br>"); else echo "(a=b<br>)";
?>
PHP ProgrammingPHP Programming
- - 1919 - -
while<?
$i=0;while($i<10) { echo(" variable \$i's value ={$i}, less then 10. <br>"); $i++;}
?>
PHP ProgrammingPHP Programming
- - 2020 - -
Do~while<?
$i=1;do { echo(" variable \$i's value ={$i} <br>"); $i--;}while($i>0)
?>
PHP ProgrammingPHP Programming
- - 2121 - -
for<?
for($i=0; $i<10;$i++){ echo("variable \$i's value is {$i} <br>");}
?>===============================<?
for($i=0; ;$i++){ if($i<10){ echo("variable \$i's value is {$i} <br>"); } else { break; }}
?>
PHP ProgrammingPHP Programming
- - 2222 - -
Each, list<?
$data = array("Lee one"=>"A", "Kang Two"=>"C", "Kim Three"=>"D");while(list($name,$class)=each($data)) { echo $name." student is ".$class." class student.<br>"; }
?>==================================================
==<?
$data = array(100,200,400,3000);while(list($name,$class)=each($data)) { echo $name." student is ".$class." class student.<br>"; }
?>
PHP ProgrammingPHP Programming
- - 2323 - -
break<?
$arr=array("Kan Won", "Kim Two", "Song Three");$i=0;while($i<sizeof($arr)){ if($arr[$i] == "Kim Two") { $count =$i+1; echo("{$count}th array element!<br>"); break; } $i++;}echo("array reference number is $i.");
?>
PHP ProgrammingPHP Programming
- - 2424 - -
continue<?
$arr=array("Kan Won", "Kim Two", "Song Three");
while(list($key,$value)=each($arr)){ if($key %2) { continue; } echo("Index : $key<br>");}
?>
PHP ProgrammingPHP Programming
- - 2525 - -
switch<?
$var=2;
switch($var) { case (1): echo("variable value is 1"); break; case (2): echo("variable value is 2"); break; case (3): echo("variable value is 3"); break; default: echo("I don't know variable value"); break; }
?>
PHP ProgrammingPHP Programming
- - 2626 - -
includeSave as…. Message.txt
hello, Kang Won-Chan.====================================Save as….Test.php
<html><body><center><h2> Transmit Message!!.</h2></center><?php
include "message.txt“;?></body></html>
PHP ProgrammingPHP Programming
- - 2727 - -
<?php
$arr=array("name1.txt","name2.txt","name3.txt");for($i=0;$i<sizeof($arr);$i++) { $filename="name".($i+1).".txt"; include $filename; echo "<br>";}
?>=================================Save as filname name1.txt Kang, Won-ChanSave as filname name2.txt Kim, Seong-ChulSave as filname name3.txt Kim, Gui-Tai
PHP ProgrammingPHP Programming
- - 2828 - -
<?phpinclude "http://www.yahoo.co.kr";
?>
PHP ProgrammingPHP Programming
- - 2929 - -
require<?php
$i=1;if($i%2){ require "case1.php"; } else { require "case2.php"; }
?>========================================
==Save as case1.php <? echo "CASE 1”; ?>Save as case2.php<?
echo "CASE 2;?>
Miss “
PHP ProgrammingPHP Programming
- - 3030 - -
<?php$i=1;if($i%2){ include "case1.php"; } else { include "case2.php"; }
?>==========================================
Save as case1.php <? echo "CASE 1”; ?>Save as case2.php<?
echo "CASE 2;?>
Miss “
PHP ProgrammingPHP Programming
- - 3131 - -
foreach<?php
$arr=array("Kan Won", "Kim Two", "Song Three");
foreach($arr as $value) { echo $value."<br>";}
?>
PHP ProgrammingPHP Programming
- - 3232 - -
<?php$arr= array("Lee one"=>"A", "Kang Two"=>"C", "Kim Three"=>"D");
foreach($arr as $key=>$value) { echo $key."-".$value."<br>";}
?>===============================<?php
$arr= array("Lee one"=>"A", "Kang Two"=>"C", "Kim Three"=>"D");
while(list($key,$value)=each($arr)){ echo $key."-".$value."<br>";}
?>
PHP ProgrammingPHP Programming
- - 3333 - -
function<?php
$res=summation(1,10);echo("result 1: $res<br>");
$res=summation(1);echo("result 2: $res<br>");
function summation($num1, $num2=5) { $sum=$num1+$num2; return $sum;}
?>
PHP ProgrammingPHP Programming
- - 3434 - -
Return by value<?php
function foo($a){ $b=$a*3; return $b; } $res=foo(4); echo("res: $res<br>");
?>===================================<?php
function foo(){ return array("kang","kim","song");}list($name1, $name2,$name3)=foo();echo("$name1 /$name2/$name3");
?>
PHP ProgrammingPHP Programming
- - 3535 - -
Return by reference<?php
function foo(&$str){ $str.="world...“; }function swap(&$a,&$b) { $temp=$b; $b=$a; $a=$temp; }$a=10;$b=15;echo("\$a:$a - \$b:$b : <br>");swap($a,$b);echo("\$a:$a - \$b:$b<br>");$str="Hello!";echo("$str<br>");foo($str);echo("$str<br>");
?>
PHP ProgrammingPHP Programming
- - 3636 - -
Function argument<?php
function summation(){ $num=func_num_args(); echo " argument". $num."<br>"; if($num>=2){ echo "first argument is".func_get_arg(0)." and<br>"; echo " second argument is".func_get_arg(1)."..."; }}summation(12,15,9);
?>
PHP ProgrammingPHP Programming
- - 3737 - -
- end -