77
Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

Embed Size (px)

Citation preview

Page 1: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

Chungbuk HRDI of KCCIPhD Kang,Won-Chan

PHP Programming(Functions)

Page 2: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 22 - -

Chechdate()<?$res=checkdate(2,29,2010);if($res){ echo("correct date format."); } else{ echo("incorrect date format"); }

?>

Page 3: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 33 - -

<?function getTotalDays($year,$month){ $day=1; while(checkdate($month,$day,$year)) { $day++; } $day = $day-1; return $day; } $totalDays=getTotalDays(2010,2); echo("2010year Feb has {$totalDays} days.");?>

Page 4: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 44 - -

Date()<? $res=mktime(23,15,23,3,11,2010); echo("using timestamp, ${res}is 2010yr 3th month 11day 23hr 15min 23sec display.<p>"); $str=date(' Y or y',$res); echo("$str<br>"); $str = date(' M or m or F', $res); echo("$str<br>"); $str = date(' d or j', $res); echo("$str<br>"); $str = date(' l or D or w', $res); echo("$str<br>"); $str = date(' H or h', $res); echo("$str<br>"); $str = date(' i', $res); echo("$str<br>"); $str = date(' s', $res); echo("$str<br>"); $str = date(' z', $res); echo("$str<p>"); $str = date("Y m d H i s .",$res); echo("$str<br>"); $str = date("Y xx m d H i s .",$res); echo("$str<br>"); $str = date("Y m d H i s ."); echo("$str"); ?>

Page 5: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 55 - -

<? $res = mktime(23,15,23,1,21,1999); echo("timestamp 으로 ${res} 은 1999 년 1 월 21 일 23 시 15 분 23 초로 다음과 같이 출력할 수 있습니

다 .<p>"); $str = date(' 년도출력 - Y 또는 y', $res); echo("$str<br>"); $str = date(' 월출력 - M 또는 m 또는 F', $res); echo("$str<br>"); $str = date(' 일출력 - d 또는 j', $res); echo("$str<br>"); $str = date(' 요일출력 - l 또는 D 또는 w', $res); echo("$str<br>"); $str = date(' 시간출력 - H 또는 h', $res); echo("$str<br>"); $str = date(' 분출력 - i', $res); echo("$str<br>"); $str = date(' 초출력 - s', $res); echo("$str<br>"); $str = date('1999 년의 몇일째 출력 - z', $res); echo("$str<p>"); $str = date("Y 년 m 월 d 일 H 시 i 분 s 초 입니다 .",$res); echo("$str<br>"); $str = date("Y 년 xx m 월 d 일 H 시 i 분 s 초 입니다 .",$res); echo("$str<br>"); $str = date("Y 년 m 월 d 일 H 시 i 분 s 초 입니다 ."); echo("$str"); ?>

Page 6: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 66 - -

Strftime()<?$str = strftime("%Y 또는 %y");echo(" 년도출력 - $str");echo("<p>");setlocale("LC_TIME","en");$str = strftime("%B 또는 %b");echo(" 월명출력 - $str");echo("<br>");setlocale("LC_TIME","ko");$str = strftime("%B 또는 %b");echo(" 월명출력 - $str");echo("<br>");setlocale("LC_TIME","fr");$str = strftime("%B 또는 %b");echo(" 월명출력 - $str");echo("<p>");$str = strftime("%d");echo(" 일자출력 - $str");echo("<p>");setlocale("LC_TIME","en");$str = strftime("%a 또는 %A 또는 %w");echo(" 요일출력 - $str");echo("<br>");setlocale("LC_TIME","ko");$str = strftime("%a 또는 %A 또는 %w");echo(" 요일출력 - $str");echo("<br>");

setlocale("LC_TIME","fr");$str = strftime("%a 또는 %A 또는 %w");echo(" 요일출력 - $str");echo("<p>");$str = strftime("%H 또는 %I");echo(" 시간출력 - $str");echo("<p>");$str = strftime("%M");echo(" 분출력 - $str");echo("<p>");$str = strftime("%S");echo(" 초출력 - $str");echo("<p>");$str = strftime("%Z");echo("timezone - $str");echo("<p>");$str = strftime("%x - %X");echo(" 날짜 - 일자출력 - $str");echo("<p>");$str = strftime("%U");echo(" 올해 지금까지 ${str} 주가 지났습니다 .");echo("<p>");setlocale("LC_TIME","ko");$str = strftime("%Y %B %d 일 %A %H 시 %M 분 %S 초 ");echo(" 현재 날짜및 시간은 ${str} 입니다 .");?>

Page 7: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 77 - -

Mktime()<? $res = mktime(); echo(“current time timestamp value is ${res}

(sec).<br>"); $res = mktime(0,0,0,1,1,1999); echo("1999, 1. 1 timestamp value is ${res}

(sec).<br>");?>

Page 8: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 88 - -

Gettime()<? $timeinfo = getdate(time()); while(list($key,$val) = each($timeinfo)) { echo("$key - $val<br>"); } echo("<br>"); echo $timeinfo["year"] . " - " .

$timeinfo["month"] . " - " . $timeinfo["weekday"] . " - " . $timeinfo["mday"];?>

Page 9: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 99 - -

Microtime()<? $str=microtime(); echo $str;?>

Page 10: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1010 - -

Calendar<? ########## calulate this year function get_totaldays($year,$month) { $date = 1; while(checkdate($month,$date,$year)) {

$date++; } $date--; return $date; } ########## output calendar function showCalendar($year,$month,$total_days) { $first_day = date('w', mktime(0,0,0,$month,1,$year)); echo("<TABLE WIDTH=\"492\" BORDER=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n"); echo("<TR><TD BGCOLOR=\"BLUE\">\n"); echo("<TABLE WIDTH=\"490\" BORDER=\"0\" cellspacing=\"1\" cellpadding=\"10\">\n"); echo("<TR>\n"); echo(" <TH bgColor=\"lightblue\" COLSPAN=\"7\">${year}Year ${month}Month Claendar</TH>\n"); echo("</TR>\n"); echo("<TR>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">SUN</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">MON</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">THU</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">WED</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">THI</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">FRI</TH>\n"); echo(" <TH bgColor=\"pink\" width=70><img src=\"images/spacer.gif\" width=\"70\" height=\"1\" border=\"0\">SAT</TH>\n"); echo("</TR>\n"); echo("<TR>\n"); $col = 0; for($i = 0; $i < $first_day; $i++) { echo(" <TD BGCOLOR=\"#FFFFFF\">&nbsp;</TD>\n"); $col++; }

for($j = 1; $j <= $total_days; $j++) { echo(" <TD BGCOLOR=\"#FFFFFF\">$j</TD>\n"); $col++; if($col == 7) { echo("</TR>\n");

if($j != $total_days) { echo("<TR>\n"); } $col = 0; }

}

while($col > 0 && $col < 7) { echo(" <TD BGCOLOR=\"#FFFFFF\">&nbsp;</TD>\n");

$col++; }

echo("</TR>\n"); echo("</TABLE>\n"); echo("</TD></TR>\n"); echo("</TABLE>\n"); }?>

<html><head> <title>calendar make of PHP</title> <style type="text/css"> <!-- BODY,TR,TH,TD { font-family: " 굴림 "; font-size: 10pt;

line-height : 12pt; } //--> </style></head>

<body bgColor="#FFFFFF">

<?$inputY = "2010";$inputM = "6";

$totaldays = get_totaldays($inputY,$inputM); showCalendar($inputY,$inputM,$totaldays);?>

</body></html>

Page 11: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1111 - -

Page 12: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1212 - -

Addslashes()<? $comment = "Hello, 'Kang'...."; echo("addslashes() before call function :

$comment<p>"); $comment = addslashes($comment); echo("addslashes() after call function :

$comment<p>"); ?>

Page 13: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1313 - -

Stripslashes()<? $comment = "Hello, 'Kang'...."; echo("addslashes() before call function : $comment<p>"); $comment = addslashes($comment); echo("addslashes() after call function : $comment<p>"); $comment = stripslashes($comment); echo("stripslashes() after call function : $comment<p>"); ?>

Page 14: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1414 - -

Htmlspecialchars()<html><body><form method="POST" action="htmlspecialchars.php"><table width="602" border="0" cellspacing="0" cellpadding="1" align="center"><tr><td bgColor="#8080FF"><table width="600" border="0" cellspacing="1" cellpadding="5" align="center"><tr><td width="100" align="left" bgColor="#CFD0ED"><font size=2>Comments <br></font></td><td width="500" bgColor="#FAFAEE"><textarea name="comment" cols="50"

rows="10"></textarea></tr><tr><td align="center" colspan="2" bgColor="#CFD0ED"><font size=2><input type="submit" value="Post"></font></td></tr></table></td></tr></table></form></body></html>

//htmlspecialchars.php<? echo("htmlspecialchars() before call<br>\n \$comment : $comment\n<p>");$res = htmlspecialchars($comment); echo("htmlspecialchars() after call<br>\n \$comment : $res\n<p>"); ?>

Page 15: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1515 - -

Php.ini; magic quotesMagic_quotes_gpc =off;

Page 16: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1616 - -

nl2br<html><body><form method="POST" action="nl2br.php"><table width="602" border="0" cellspacing="0" cellpadding="1" align="center"><tr><td bgColor="#8080FF"><table width="600" border="0" cellspacing="1" cellpadding="5" align="center"><tr><td width="100" align="left" bgColor="#CFD0ED"><font size=2>Comments <br></font></td><td width="500" bgColor="#FAFAEE"><textarea name="comment" cols="50"

rows="10"></textarea></tr><tr><td align="center" colspan="2" bgColor="#CFD0ED"><font size=2><input type="submit" value="Post"><input type="reset" value="Clear"></font></td></tr></table></td></tr></table></form></body></html>

//nl2br.php<? echo("[nl2br() before call]<br>"); echo("$comment<p>"); $res = nl2br($comment); echo("[nl2br() after call]<br>"); echo("$res<p>");?>

Page 17: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1717 - -

Page 18: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1818 - -

echo<?$str ="hello, everyone!";echo("$str ");echo $str;?>

Page 19: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 1919 - -

Print()<?print ("welcome to korea.<br>");$str ="hello!";print(" nice meet you. : ".$str."<br>");print " nice meet you. : ".$str."<br>";?>

Page 20: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2020 - -

Sprintf()<? $a = 18.2; $b = 7.4; $sum = $a + $b; $ext = $a - $b; $res = sprintf("%d", $sum); echo("$res<br>"); $res = sprintf("%f", $sum); echo("$res<br>"); $res = sprintf("%04d", $sum); echo("$res<br>"); $res = sprintf("%04.2f", $sum); echo("$res<br>"); $res = sprintf("%03.2f - %0.2f", $sum, $ext); echo("$res<br>"); $res = sprintf("%d - %c", 97, 97); echo("$res<br>"); ?>

Page 21: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2121 - -

Printf()<? $a = 18.2; $b = 7.4; $sum = $a + $b; printf("%02.2f && %02.2f sum=integer

value is %'x4d!", $a,$b,$sum);?>

Page 22: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2222 - -

Explode()<? $string = "one two threee four five"; $arr = explode(" ", $string); while(list($key,$val) = each($arr)) { echo("\$arr[$key] : $val<br>"); }?>

Page 23: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2323 - -

Implode()/join()<? $arr = array("one", "two", "three",

"four", "five"); $res = implode("/", $arr); echo("$res");?>

Page 24: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2424 - -

Spilt()<? $str = "Kang:Kim:Song:Choi“; $arr = split(":", $str,3); while(list($key,$val) = each($arr)) { echo("\$arr[$key] : $val<br>"); } echo("<br>"); $str = "Kang92Kim91Song87Choi97"; $arr = split("[0-9]{2}", $str); while(list($key,$val) = each($arr)) { echo("\$arr[$key] : $val<br>"); } ?>

Page 25: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2525 - -

Strcmp()<? $str1 = "Hello?“; if(!strcmp($str1,"Hello?")) { echo("correct match string.<br>"); } if($str1 == "Hello?") { echo("correct match string.<br>"); } if(!strcmp("abc", "ABC")) { echo("correct match string.<br>"); } ?>

Page 26: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2626 - -

Substr()<?$res = substr("abcdefg",1);echo("$res<br>");$res = substr("abcdefg",1,3);echo("$res<br>");$res = substr("abcdefg",-3);echo("$res<br>");$res = substr("abcdefg",-3,2);echo("$res<br>");$res = substr("abcdefg",3,-1);echo("$res<br>");$res = substr("abcdefg",1,-3);echo("$res<br>");$res = substr("abcdefg",-6,-1);echo("$res<br>");$res = substr("abcdefg", 0,-1);echo("$res<br>");?>

Page 27: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2727 - -

Strchr()/strstr()<?$str1 = "abcdefghijklmn";$res = strchr($str1,"j");echo("res : $res<br>");

$res = strchr($str1,"jm");echo("res : $res<br>");?>

Page 28: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2828 - -

strrchr<?$str = "abcdefgihijklmn";$res = strchr($str,"f");echo("res : $res<br>");$res = strrchr($str,"f");echo("res : $res<br>");

$str = "abcdefghijklmnabcdefghijklmn";

$res = strchr($str,"f");echo("res : $res<br>");$res = strrchr($str,"f");echo("res : $res<br>");?>

Page 29: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 2929 - -

<?printf("%d -> %c <br>",75,75);$text = "Line 1\nLine 2\nLine 3";$str = strrchr($text,10);echo("str : $str<br>");$last = substr( strrchr( $text, 10 ), 1 );echo("$last - ");?>

Page 30: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3030 - -

Strpos()<?$str1 = "AaBbCcDdAaBb";

$res = strpos($str1,"C");echo("res : $res<br>");

$res = strchr($str1,"C");echo("res : $res<br>");

$res = strpos($str1,97);echo("res : $res<br>");

$res = strpos($str1,"C",5);echo("res : $res<br>");?>

//a=97

//false

<?$str1 = "AaBbCcAaBb";

$res = strrpos($str1,'a');echo("res : $res<br>");?>

Page 31: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3131 - -

Strtoupper()/strtolower()

<?$str = "abcd“;$res = strtoupper($str);echo("res : $res<br>");$res = strtolower($res);echo("res : $res<br>");?>

Page 32: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3232 - -

Ucfirst()/ucwords()<?$str = "abcd“;$res = ucfirst($str);echo("$res<br>");

$str = "a blue angel";$res = ucwords($str);echo($res);?>

Page 33: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3333 - -

Strrev()<?$str = "abcde";$res = strrev($str);echo("$res");?>

Page 34: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3434 - -

Strtr()/str)replace()<?$str = "a blue angel of Hyper world";$res = strtr($str,"angel", "seagull");echo("$res");?>============================

<?$str = "a blue angel of Hyper world";$res = str_replace("angel","seagull",$str);echo("$res");?>

Page 35: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3535 - -

Chop()/trim()<?$str = " *** a blue angel of Hyper world *** ";$res = chop($str);echo("[$res]");?>========================

<?$str = " *** a blue angel of Hyper world *** ";$res = trim($str);echo("[$res]");?>

** Ltrim(), rtrim()

Page 36: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3636 - -

Quotemeta()<?$str = "meta character : . \\ + * ? [ ^ ] ( $ )";$res = quotemeta($str);echo("$res");?>

Page 37: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3737 - -

Ord()/chr()<?$str = "aspell";$res = ord($str);echo("$res<br>");

$res = chr($res);echo("$res<br>");?>

//change to ascii value, first character

//change to code value,

Page 38: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3838 - -

Parse_str()<?$str = "page_num=1&key=PHP4&keyword=subject";parse_str($str);

echo("page_num : $page_num<br>");echo("key : $key<br>");echo("keyword : $keyword<br>");?>

Page 39: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 3939 - -

Parse_str()Strip_tags()Ob_start()Ob_get_contentsOb_end_flush()Ob_end_clean()Flush()Str_repeat()Substr_count()Crypt()

Page 40: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4040 - -

Fopen()/fclose()<?$fp = fopen("/home/younicom/public_html/php/list.dat","a");if(!$fp) { echo("Failed to open the file");} else { echo("Successfully opened the file!");}fclose($fp);?><?$fp = fopen("c:\\Inetpub\\wwwroot\\list.dat","r");if(!$fp) { echo("Failed to open the file");}$contents = fread($fp,10);echo("$contents");fclose($fp);?>

//LINUX style

//Windows style

Page 41: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4141 - -

List.datHe was in time for the last bus.He goes to his office every other day.It is nearly six (o'clock)

**save as same directory filename

Page 42: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4242 - -

Fwrite()<?$fp = fopen("c:\\Inetpub\\wwwroot\\list.dat","a");

if(!$fp) { echo("Failed to open the file");}

$str = "It is of no importance";fwrite($fp,$str);fclose($fp);?>

** IIS Directory attribute change

Page 43: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4343 - -

Fgets()<?$fp = fopen("c:\\Inetpub\\wwwroot\\list.dat","r");if(!$fp) { echo("Failed to open the file");}if(!feof($fp)) { $line = fgets($fp,10000); echo("$line");} if(!feof($fp)) { fpassthru($fp);} // fclose($fp);?>

Page 44: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4444 - -

Readfile()<?$bytes = readfile("c:\\Inetpub\\wwwroot\\list.dat");echo("<br>total size : $bytes bytes");?>

Page 45: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4545 - -

Fgetc()/feof()<?$fp = fopen("list.dat","r");if(!$fp) { echo("Failed to open the file");}while(!feof($fp)) { $chr = fgetc($fp); if(!strcmp($chr,"\n")) { $chr .= "<br>"; } $line .= $chr;}echo("$line"); fclose($fp);?>** fgetss(),fgetcsv,file()

Page 46: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4646 - -

<?$filename = "list.dat";if(!file_exists($filename)) { echo("${filename} is No Exist file.<br>"); exit;} if(!is_file($filename)) { echo("${filename} is no file.<br>"); exit; }if(!is_dir($filename)) { echo("${filename} is no directory.<br>"); exit; }if(!is_link($filename)) { echo("${filename} is no symbolic link.<br>"); exit; }if(!is_readable($filename)) { echo("${filename} is no read file.<br>"); exit; }if(!is_writeable($filename)) { echo("${filename} is write protect file.<br>"); exit; }if(!is_executable($filename)) { echo("${filename} is no excute file.<br>"); exit; }?>

Page 47: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4747 - -

File()Filesize()File_exists()Fileatime()Filemtime()Fileowner()Filegroup()Is_file()Is_dir()Is_link()Is_excutable()Is_readable()Is_writeable()Filetype()

Page 48: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4848 - -

Copy()/rename()<?$src = "list.dat";if(!copy($src,"list_copy.dat")) { echo("Failed to copy $src file"); exit;} ?> ===================<? $src = "list.dat";

if(!rename($src,"rename.dat")) { echo("Failed to rename $src file"); exit;} ?>

Page 49: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 4949 - -

Unlink()<? $src = "list.dat";if(!file_exists($src)) { echo("$src file does not exist!"); exit;}if(!unlink($src)) { echo("Failed to delete $src file"); exit;} ?>

Page 50: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5050 - -

Mkdir()/rmdir()<? $src = "list.dat";if(!mkdir($dirname,0755)) { echo("Failed to create $dirname directory"); exit;}

if(!rmdir($dirname)) { echo("Failed to remove $dirname directory"); exit;}?>

//LINUX style, pemission

Page 51: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5151 - -

Basename()/dirname()<?$path = "/home/ftp/readme.txt";$base = basename($path);echo("$base<br>");

$dir = dirname($path);echo($dir);?>

Page 52: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5252 - -

<?$filename = "list.dat";$line = file($filename);while(list($key,$val) = each($line)) { echo("$val<br>");}?>

Page 53: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5353 - -

Chgrp()Chown()Chmod()Diskfreespace()Touch()Rewind()Fseek()Ftell()

Page 54: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5454 - -

Directory command<?$dirhandle = opendir("/php");$filename = readdir($dirhandle);echo("filename : $filename<br>");$filename = readdir($dirhandle);echo("filename : $filename<br>");$filename = readdir($dirhandle);echo("filename : $filename<br>");$filename = readdir($dirhandle);echo("filename : $filename<br>");closedir($dirhandle);?>

Page 55: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5555 - -

<?$dirhandle = opendir("/home/younicom");while($filename = readdir($dirhandle)) { echo("filename : $filename<br>");} closedir($dirhandle);?>

Page 56: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5656 - -

<?$dirhandle = opendir("c:\\work");while($filename = readdir($dirhandle)) { echo("filename : $filename<br>");} $filename = readdir($dirhandle);echo("filename : $filename<br>");rewinddir($dirhandle);while($filename = readdir($dirhandle)) { echo("filename : $filename<br>");} closedir($dirhandle);?>

Page 57: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5757 - -

Dir()<?$mydir = dir("c:\\work");echo("Current directory : $mydir->path<br>");while($filename = $mydir->read()) { echo("$filename<br>\n");}$mydir->rewind();echo("Current directory : $mydir->path<br>");while($filename = $mydir->read()) { echo("$filename<br>\n");}$mydir->close();?>

Page 58: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5858 - -

parse_url()<?$url =

"http://safelab.skku.ac.kr:8080/~younicom/cgi-bin/guestbook.cgi?db=gbook&page=2";

$arr = parse_url($url);

while(list($key,$val) = each($arr)) { echo("$key - $val<br>");}?>

Page 59: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 5959 - -

Urlencode()<? $search = "PHP install"; $encoded_key = urlencode($search); $url = "<A HREF=\"/board/list.php3?

code=phpqna&page_num=1&key=" . $encoded_key . "&keyword=subject";

$url .= "\">Board Search</A>"; echo($url);?>

Page 60: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6060 - -

Urldecode()<? $search = "PHP Install"; $encoded_key = urlencode($search); $querystring = "code=phpqna&page_num=1&key=" . $encoded_key .

"&keyword=subject"; echo("querystring : $querystring<p>"); $a = split ('&', $querystring); $i = 0; while ($i < sizeof($a)) { $b = split ('=', $a[$i]); $str = "Variable " . htmlspecialchars(urldecode($b[0])) . " decording value is =

" . htmlspecialchars(urldecode($b[1])) . "<br>"; echo($str); $i++; }?>

Page 61: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6161 - -

Gettype()<?$var = 123;echo(gettype($var)); echo "<br>";

$var = 123.5;echo(gettype($var));echo "<br>";

settype($var,"integer");echo(gettype($var));echo "<br>";

$var = "hello world";echo(gettype($var));echo "<br>";

$var = array("123", "246", "369");echo(gettype($var));echo "<br>";?>

Page 62: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6262 - -

Intval()/doubleval()/strval()

<?$var = 123.5;echo(intval($var));echo "<br>";echo(doubleval($var));echo "<br>";echo(strval($var));echo "<br>";?>

Page 63: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6363 - -

Isset()/unset()/empty()<?$var1 = "123.55";echo(isset($var1)); //1(true)echo(isset($var2)); //0(false)echo(empty($var1)); //0

unset($var1);echo(isset($var1)); //0echo(empty($var1)); //1?>

Page 64: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6464 - -

Is_array()Is-double(),is_float(), is-real()Is_int(), is_integer(),is_long()Is_string(), is_object(), is_bool()Is_numeric(), is_resource()Call_user_func()

Call_user_func(‘Hello’,”Jay”);Hello(“Jay”);

Page 65: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6565 - -

Math Function<? $val = -12.3;$res = abs($val);echo("$res<br>");$val = 12.4;$res = round($val);echo("$res<br>");$val = 12.5;$res = round($val);echo("$res<br>");$val = 12.6;$res = round($val);echo("$res<br>");$val = 13.1;$res = ceil($val);echo("$res<br>");$val = 13.1;$res = floor($val);echo("$res<br>");?>

Page 66: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6666 - -

<?$val = 3;$res = pow($val,4);echo("$res<br>");$res = sqrt($res);echo("$res<br>");$res = exp($val);echo("$res<br>");$val = 1;$res = exp($val);echo("$res<br>");$res = log($res);echo("$res<br>");$val = 100;$res = log10($val);echo("$res<br>");$res = pi();echo("$res<br>");?>

Page 67: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6767 - -

<?$res_max = max(13,12,14,10);$res_min = min(23,12,14,10);echo("max : $res_max - min : $res_min<br>");

$arr = array(23,12,14,10);$res_max = max($arr);$res_min = min($arr);echo("max : $res_max - min : $res_min<br>");?>

Page 68: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6868 - -

<?$num = 1213000;$res = number_format($num);echo("$res<br>");

$res = number_format($num,3);echo("$res<br>");

$res = number_format($num,3,"-","'");echo("$res<br>");?>

Page 69: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 6969 - -

<?srand((double)microtime()*1000000);$randval = rand(1,10);echo("$randval");?>

Page 70: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7070 - -

<?$val = sin(pi()/2);printf("%.2f <br>", $val);$val = cos(pi()/2);printf("%.2f <br>", $val);$val = tan(pi()/4);printf("%.2f <br>", $val);?>

Page 71: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7171 - -

Array functionArray(),List(),Each(),Key()Current(), next(),Reset(),Prev(),end()In_array()Sizeof(), range()Shuffle(), pos()sort(),ksort(),asort(),rsort()arsort(),krsort()array_pop(), array_push(), array_shift(), array_unshift(), array_unique(), array_values(), array_keys(), array_reverse(), array_rand(), array_pad(), array_walk(), array_merge(), array_diff(), array_intersect()

Page 72: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7272 - -

<?$val = getenv("SERVER_SOFTWARE");echo("SERVER_SOFTWARE :".$val."<br>");$val = getenv("SERVER_NAME");echo("SERVER_NAME : $val<br>");$val = getenv("GATEWAY_INTERFACE");echo("GATEWAY_INTERFACE : $val<br>");$val = getenv("SERVER_PROTOCOL");echo("SERVER_PROTOCOL : $val<br>");$val = getenv("SERVER_PORT");echo("SERVER_PORT : $val<br>");$val = getenv("REQUEST_METHOD");echo("REQUEST_METHOD : $val<br>");$val = getenv("PATH_INFO");echo("PATH_INFO : $val<br>");$val = getenv("PATH_TRANSLATED");echo("PATH_TRANSLATED : $val<br>");$val = getenv("SCRIPT_NAME");echo("SCRIPT_NAME : $val<br>");$val = getenv("QUERY_STRING");echo("QUERY_STRING : $val<br>");$val = getenv("REMOTE_ADDR");echo("REMOTE_ADDR : $val<br>");$val = getenv("REMOTE_HOST");echo("REMOTE_HOST : $val<br>");$val = getenv("CONTENT_LENGTH");echo("CONTENT_LENGTH : $val<br>");$val = getenv("CONTENT_TYPE");echo("CONTENT_TYPE : $val<br>");$val = getenv("HTTP_USER_AGENT");echo("HTTP_USER_AGENT : $val<br>");?>

Page 73: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7373 - -

<?$time = getlastmod();$modified_str = date('Y-m-d H:i:s',$time);echo("Last modified: $modified_str");?>

Page 74: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7474 - -

Mail()<?echo "current php version is".phpversion();$mailto = "[email protected]";$subject = "A New Message Posted to BBS";$contents = "Hello? This is message using mail() function call";mail($mailto, $subject, $contents);?><?$mailto = "[email protected]";$subject = "A New Message Posted to BBS";$contents = "<a href=\"http://safelab.skku.ac.kr\">Hello? This is

message using mail() function call</a>";$add_header = "From: [email protected]\n";$add_header .= "Reply-To: [email protected]\n";$add_header .= "Content-Type: text/html;charset=EUC-KR“;mail($mailto, $subject, $contents,$add_header);?>

Page 75: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7575 - -

<?$filename = "c:\\work";$fp = fopen($filename,"r") || die("unable to open the file

$filename");?>===================<?$uid = md5(uniqid(rand())); echo("uid : $uid");?>

Page 76: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7676 - -

Etc functionPhpinfo(), phpversion()Die(), exit()Uniqid()Sleep(),usleep()Highlight_string(), highlighr_file()Show_source()

Page 77: Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Functions)

PHP ProgrammingPHP Programming

- - 7777 - -

- end -