Hello PHP 22 hello 1 php Page 3

  • Slides: 34
Download presentation

Hello PHP 구성 (2/2) 기초 프로그래밍 수행 결과 (hello 1. php) Page 3 Web

Hello PHP 구성 (2/2) 기초 프로그래밍 수행 결과 (hello 1. php) Page 3 Web Programming by Yang-Sae Moon

에러 발생 (2/2) 기초 프로그래밍 수행 결과 (hello 2. php, hello 3. php) Page

에러 발생 (2/2) 기초 프로그래밍 수행 결과 (hello 2. php, hello 3. php) Page 5 Web Programming by Yang-Sae Moon

변수(Variables) (2/5) 기초 프로그래밍 변수의 종류 • 불리언 (Boolean) • 정수형 (integer) • 실수형

변수(Variables) (2/5) 기초 프로그래밍 변수의 종류 • 불리언 (Boolean) • 정수형 (integer) • 실수형 (floating point numbers, real numbers) • 문자열 (string) • 배열 (array) • 객체 (object) 변수형 강도 (strength) • PHP는 형 강도가 매우 약하며, 변수에 할당되는 값에 따라 형이 결정됨 • 예: $totalamount = 300 + 500; // integer type $totalamount = “hello”; // string type 변수형 변환 (casting) • Casting operator 사용: (int), (float), (double), (string), (array), (object) • 예: $totalqty = 0; $totalamount = (double)$totalqty; Page 9 Web Programming by Yang-Sae Moon

변수(Variables) (3/5) 기초 프로그래밍 변수형 검사 및 설정 string gettype(mixed var); bool settype(mixed var,

변수(Variables) (3/5) 기초 프로그래밍 변수형 검사 및 설정 string gettype(mixed var); bool settype(mixed var, string type); • gettype() 함수는 변수형을 스트링(“integer”, “double”, …)으로 반환함 • settype() 함수는 변수의 형을 주어진 형으로 바꾸어 줌 • 예: $a = 56; var_type 1. php print gettype($a); // integer settype($a, “double”); print gettype($a); // double 변수형 확인 함수 (true or false를 리턴함) var_type 2. php • is_array() • is_double(), is_float(), is_real() // 모두 같은 함수임 • is_long(), is_integer() // 모두 같은 함수임 • is_string() • is_object() Page 10 Web Programming by Yang-Sae Moon

변수(Variables) (4/5) 기초 프로그래밍 변수에 값 대입 <? PHP $sum = 12 + 23;

변수(Variables) (4/5) 기초 프로그래밍 변수에 값 대입 <? PHP $sum = 12 + 23; // integer type print $sum; ? > <? PHP $sum = 12. 3 – 42. 72; // real(float) type) print $sum; ? > Page 11 Web Programming by Yang-Sae Moon

변수(Variables) (5/5) 기초 프로그래밍 정수형 변수의 표현 (int_rep. php) <? PHP $value = 99999997;

변수(Variables) (5/5) 기초 프로그래밍 정수형 변수의 표현 (int_rep. php) <? PHP $value = 99999997; print $value. “ ”; $value = $value + 1; // $value = 99999998 print $value. “ ”; $value = $value + 1; // $value = 9999999 print $value. “ ”; $value = $value + 1; // $value = 100000001 print $value. “ ”; $value = $value + 1; // $value = 100000002 print $value. “ ”; ? > Page 12 Web Programming by Yang-Sae Moon

연산자 및 수식 계산 (1/2) 기초 프로그래밍 연산자의 종류 및 의미 사용법 의미 oprd

연산자 및 수식 계산 (1/2) 기초 프로그래밍 연산자의 종류 및 의미 사용법 의미 oprd 1 + oprd 2 orpd 1에 ordp 2를 더하기 oprd 1 – orpd 2 orpd 1에서 ordp 2를 빼기 oprd 1 * orpd 2 orpd 1에 ordp 2를 곱하기 orpd 1 / orpd 2 orpd 1에서 ordp 2를 나누기 orpd 1 % orpd 2 orpd 1에서 ordp 2를 나눈 나머지 Page 13 Web Programming by Yang-Sae Moon

연산자 및 수식 계산 (2/2) 기초 프로그래밍 사칙 연산자와 나머지 연산자의 사용 예 oprd.

연산자 및 수식 계산 (2/2) 기초 프로그래밍 사칙 연산자와 나머지 연산자의 사용 예 oprd. php <? PHP $result = 9 + 5; print “ 9 + 5 = $result ”; $result = 9 – 5; print “ 9 – 5 = $result ”; $result = 9 * 5; print “ 9 * 5 = $result ”; $result = 9 / 5; print “ 9 / 5 = $result ”; $result = 9 % 5; print “ 9 % 5 = $result ”; ? > Page 14 Web Programming by Yang-Sae Moon

기타 연산자 (2/2) 기초 프로그래밍 증가 연산자의 사용 예 (inc_dec. php) <? PHP $temp

기타 연산자 (2/2) 기초 프로그래밍 증가 연산자의 사용 예 (inc_dec. php) <? PHP $temp = 1; if($temp++ == 1) print "temp in the 1 st if() is 1"; else print "temp in the 1 st if() is 2"; $temp = 1; if(++$temp == 1) print ", and temp in 2 nd if() is 1. "; else print ", and temp in the 2 nd if() is 2. "; ? > Page 16 Web Programming by Yang-Sae Moon

스트링 연산자 기초 프로그래밍 Concatenation을 수행하는 “. ” 연산자 사용 두 문자열을 연결하는 연산자임

스트링 연산자 기초 프로그래밍 Concatenation을 수행하는 “. ” 연산자 사용 두 문자열을 연결하는 연산자임 (concat. php) <? $city = “in Chunchon”; $name 1 = “Kangwon ”; $name 2 = “National University “; print $name 1. $name 2. $city; ? > Page 17 Web Programming by Yang-Sae Moon

수학적 함수 (1/2) 기초 프로그래밍 삼각함수(sin, cos, …)는 기본적으로 Radian 사용 (2 = 360

수학적 함수 (1/2) 기초 프로그래밍 삼각함수(sin, cos, …)는 기본적으로 Radian 사용 (2 = 360 ) 로그함수 log()는 기본적으로 자연 로그(밑이 e)이며, 대수 로그(밑이 10) 는 log 10()을 사용한다. 삼각함수의 사용 예 (sin_cos 1. php) <? PHP $result = sin(M_PI / 6); print “sin(30) = $result ”; $result = cos(M_PI / 6); print “cos(30) = $result ”; $result = tan(M_PI / 6); print “tan(30) = $result ”; $result = asin(0. 5); print “asin(0. 5) = $result ”; $result = acos(0. 866025); print “acos(0. 866025) = $result ”; $result = atan(0. 57735); print “atan (0. 57735) = $result ”; ? > Page 18 Web Programming by Yang-Sae Moon

수학적 함수 (2/2) 기초 프로그래밍 Degree 값(360도 기준 값) 사용 deg 2 rad() 함수

수학적 함수 (2/2) 기초 프로그래밍 Degree 값(360도 기준 값) 사용 deg 2 rad() 함수 활용 sin_cos 2. php <? PHP $degree = 30; $radian = deg 2 rad($degree); $result = sin($radian); print “sin(30) = $result”; ? > log(), log 10(), sqrt() 사용 예 sin_cos 2. php <? PHP $result = log (10); print “log(10) = $result ”; $result = log 10 (10); print “log 10(10) = $result ”; $result = sqrt (49); print “sqrt(49) = $result”; ? > Page 19 Web Programming by Yang-Sae Moon

연산자 우선순위 결합성 left left left non-associative left right non-associative 기초 프로그래밍 연산자 ,

연산자 우선순위 결합성 left left left non-associative left right non-associative 기초 프로그래밍 연산자 , or xor and = += -= *= /=. = %= != ~= <<= >>= ? : || && | ^ & == != < <= > >= << >> + -. * / % ! ~ ++ -- (int) (double) (string) (array) (object) @ [ new Page 21 Web Programming by Yang-Sae Moon

if-else 구문 (1/2) 기초 프로그래밍 주어진 조건에 따라서 서로 다른 문장을 수행 문법 if(condition)

if-else 구문 (1/2) 기초 프로그래밍 주어진 조건에 따라서 서로 다른 문장을 수행 문법 if(condition) statement; if(condition) if(condition 1) { statement; statement 1; elseif(condition 2) statement 2; statement; } elseif(condition 3) statement; if(condition) elseif(condition 4) statement; else statement; Page 22 Web Programming by Yang-Sae Moon

if-else 구문 (2/2) 기초 프로그래밍 if-else 구문의 예 (ifelse. php) <? PHP $temp 1

if-else 구문 (2/2) 기초 프로그래밍 if-else 구문의 예 (ifelse. php) <? PHP $temp 1 = 1; $temp 2 = 2; if($temp 1 == 1 && $temp 2 == 1) print "condition of if() is true. "; elseif($temp 1 == 1 && $temp 2 == 2) { print "condition of elseif() is true. "; print "temp 1 is 1 and temp 2 is 2. "; } else print "condition of else is true. "; ? > Page 23 Web Programming by Yang-Sae Moon

비교 연산자 (1/2) 기초 프로그래밍 연산자 의 미 == 같다(equal). != 같지 않다(not equal).

비교 연산자 (1/2) 기초 프로그래밍 연산자 의 미 == 같다(equal). != 같지 않다(not equal). < 작다(less than). > 크다(greater than). <= 작거나 같다(less than or equal to). >= 크거나 같다(greater than or equal to). === 동일하다(identical). 두 Operand가 서로 같고 형식(type)이 같다. Page 24 Web Programming by Yang-Sae Moon

삼항 연산자 (? : ) 기초 프로그래밍 (condition) ? statement_true : statement_false; (cond_st. php)

삼항 연산자 (? : ) 기초 프로그래밍 (condition) ? statement_true : statement_false; (cond_st. php) <? PHP $num = 5; (($num % 2) == 1) ? print “홀수” : print “짝수”; print “ ”; $num = 8; (($num % 2) == 1) ? print “홀수” : print “짝수”; ? > Page 26 Web Programming by Yang-Sae Moon

while 구문 (1/2) 기초 프로그래밍 문법 while (expression) statement while 구문의 사용 예 (while.

while 구문 (1/2) 기초 프로그래밍 문법 while (expression) statement while 구문의 사용 예 (while. php) <? PHP $i = 1; $sum = 0; while ($i < 101) { $sum = $sum + $i; $i = $i + 1; } print “The sum from 1 to 100 is $sum”; ? > Page 28 Web Programming by Yang-Sae Moon

while 구문 (2/2) 기초 프로그래밍 무한 루프의 예 <? PHP $i = 0; $value

while 구문 (2/2) 기초 프로그래밍 무한 루프의 예 <? PHP $i = 0; $value = 1; while (TRUE) { $value = $value * 3; $i = $i + 1; if ($value > 10000) break; } print $i; ? > Page 29 Web Programming by Yang-Sae Moon

do-while 구문 기초 프로그래밍 문법 do statement while (expression); do-while 구문의 사용 예 (do-while.

do-while 구문 기초 프로그래밍 문법 do statement while (expression); do-while 구문의 사용 예 (do-while. php) <? PHP $i = 1; do { $j = $i * 5; $i = $i + 1; print $j. “ ”; } while ($j < 100); ? > Page 30 Web Programming by Yang-Sae Moon

함수 정의 (1/3) 기초 프로그래밍 문법 function func_name($var 1, $var 2, . . .

함수 정의 (1/3) 기초 프로그래밍 문법 function func_name($var 1, $var 2, . . . ) { statements; } add() 함수의 정의 func_add. php <? PHP function add($x, $y) { $sum = $x + $y; return $sum; } $result = add (3, 5); print $result; ? > Page 31 Web Programming by Yang-Sae Moon

함수 정의 (2/3) 기초 프로그래밍 Default Values (func_def. php) <? PHP function my_log ($arg,

함수 정의 (2/3) 기초 프로그래밍 Default Values (func_def. php) <? PHP function my_log ($arg, $base = 2) { $result = log ($arg) / log ($base); return $result; } print “log 2(10) = ”. my_log(10, 2). “ ”; print “log 10(100) = ”. my_log(100, 10). “ ”; print “log 2(8) = ”. my_log(8, 2). “ ”; print “log 2(8) = ”. my_log(8). “ ”; ? > Page 32 Web Programming by Yang-Sae Moon

함수 정의 (3/3) 기초 프로그래밍 func_get_args() 함수 활용 (func_arg. php) <? PHP function my_print

함수 정의 (3/3) 기초 프로그래밍 func_get_args() 함수 활용 (func_arg. php) <? PHP function my_print () { $args = func_get_args (); foreach ($args as $arg) print “파라미터: $arg ”; } my_print (“Apple”, “Orange”, “Pear”, “Banana”, “Cherry”); ? > Page 33 Web Programming by Yang-Sae Moon

Homework #7 (실습 #6) 기초 프로그래밍 Page 34 Web Programming by Yang-Sae Moon

Homework #7 (실습 #6) 기초 프로그래밍 Page 34 Web Programming by Yang-Sae Moon