23
P P HP HP 프프프프프 프프프프프

PHP 프로그래밍. Let ’ s start test

Embed Size (px)

Citation preview

Page 1: PHP 프로그래밍. Let ’ s start test

PPHP HP 프로그래밍프로그래밍

Page 2: PHP 프로그래밍. Let ’ s start test

LetLet’’s starts start<? Or <?php ….. ?>

<html><head>

<title> test </title>

</head>

<body bgcolor="white">

<?$string=“test”;

$num=35;

$float=45.14;

echo $string ;

echo "<br>";

echo $num;

echo "<br>";

echo $float;

echo "<br>";

?>

</body></html>

Page 3: PHP 프로그래밍. Let ’ s start test

BasicsBasics

PHP 코드의 시작과 끝 : <script language=“php”> </script> <?php ?> <? ?>

Page 4: PHP 프로그래밍. Let ’ s start test

BasicsBasicsFunction

– function fname (parm,..){ …}e.g.) function add($a,$b)

{$value = $a+$b;return ($value);

}$v = add(30,5);

Page 5: PHP 프로그래밍. Let ’ s start test

functionsfunctions<? function test($args) {

$args += 10; }

$realargs = 10; test($realargs); echo (“$realargs”);?>

=> Results? 10 (call by value)

Page 6: PHP 프로그래밍. Let ’ s start test

functionsfunctionsfunction makecoffee($type = “cappucino”) { return $type;}echo makecoffee();echo makecoffee(“espresso”);

Page 7: PHP 프로그래밍. Let ’ s start test

BasicsBasicsFunction scope

e.g.) $data = “test”;function aaa()

{

$data = “new”;

echo $data;

}

echo $data;

Page 8: PHP 프로그래밍. Let ’ s start test

Identifier, CommentsIdentifier, Comments영문자 , 밑줄 , $ 기호로 시작내장함수는 대소문자 구별하지 않음

– print = Printcomments: /*, */ , //, #자료형

– Integer, Floating-point number, String, array, object

– 특수문자 :\n,\r,\t, \\,\$, \”– string 사용법 : “ ”, ‘ ’

Page 9: PHP 프로그래밍. Let ’ s start test

변수변수 : : 전역변수전역변수 , , 지역변수지역변수<?

$a = 1;

function test()

{

echo $a;

}

test();

?>

<?

$a = 1;

function test()

{

global $a;

echo $a;

}

test();

?>

Page 10: PHP 프로그래밍. Let ’ s start test

Static VariableStatic Variablefunction Test() {

static $a=0;

echo $a;

$a++;

}

Page 11: PHP 프로그래밍. Let ’ s start test

Variable typeVariable typeNo specific type declaration required.Type casting

– $foo=10;– $bar=(double) $foo;

관련함수– gettype(), is_long(). Is_string(), is_array(), is

_object(), …

Page 12: PHP 프로그래밍. Let ’ s start test

변수타입 변환변수타입 변환문자열이 숫자 값일 때 :

– . e E 는 double, 아니면 integer– $foo=1+”10.5”; // double(11.5)– $foo=1+”-1.3e3”;// double (-1299)– $foo=1+”bob-1.3e3”;//1– $foo=1+”bob3”;//1– $foo=1+”10 small pigs”;// 11– $foo=“10.0 pigs”+1.0 ;// double (11.0)

Page 13: PHP 프로그래밍. Let ’ s start test

변수변수 : : 변수 받아오기변수 받아오기

<form action=‘test.php’ method=post>

ID : <input type=text name=userid>

<input type=submit>

</form>

<?

$userid = $_POST[“userid"];

echo $userid;

?>

다른

Html문서

test.php

Page 14: PHP 프로그래밍. Let ’ s start test

ConstantConstant예 )

<?define(“CONSTANT”, “Hello World”);echo(CONSTANT);

?>

Pre-defined constants– _FILE_: 자신파일이름– _LINE_: 현재 파일의 라인 위치– PHP_VERSION, PHP_OS– TRUE, FALSE– E_ERROR, E_WARNING, E_PARSE,

E_NOTICE

Page 15: PHP 프로그래밍. Let ’ s start test

ArrayArray$a[0]=“abc”;$b[“foo”]=13;$a[1][0]=$f;$a[“foo”][1]=$f;

Page 16: PHP 프로그래밍. Let ’ s start test

Array InitializationArray Initialization $a = array (

“color”=>”red”, “taste”=>”sweet”, 3=>4);

$a = array (“apple” => array (

“color” => “red”, “taste”=>”sweet”),

“orange”=> array (…

)

);

Page 17: PHP 프로그래밍. Let ’ s start test

수식과 연산자수식과 연산자 $count +=count; 산술 연산자 :

– $a+$b; $a-$b; $a*$b; $a/$b; $a%$b; 문자열

– $b= “hello”.”world”; 비트연산자

– $a&$b; $a|$b;$a^$b; ~$a; $a<<$b; $a>> $b; 논리연산자

– $a and $b; $a or $b; $a or $b; !$a; $a && $b; $a || $b;

비교연산자– $a == $b; $a !=$b; $a < $b; $a <= $b;

Page 18: PHP 프로그래밍. Let ’ s start test

ifif if (expr) {statement;}

if (expr1) {statement1;}

elseif (expr2)

else { statement2;}

<? if ($a ==5): ?>a=5; b=4;

<? endif: ?>

Page 19: PHP 프로그래밍. Let ’ s start test

whilewhilewhile (expr) { statement1;}while (expr): statement1; endwhile

<?$x=1;

while ($x <= 100):

$sum += $x; $x +=1;

endwhile;

?>

Page 20: PHP 프로그래밍. Let ’ s start test

forfor for (expr1;expr2;expr3) { statement1;

}

for (expr1; expr2; expr3):

statement1;

endfor;

Page 21: PHP 프로그래밍. Let ’ s start test

Class & ObjectClass & Object<?

class Cart {var $items;function add_item ($artnr, $num)

{ $this->items[$artnr] +=$num;

}}$cart = new Cart;$cart->add_item(“10”,1);

?>

Page 22: PHP 프로그래밍. Let ’ s start test

문자열 처리 함수문자열 처리 함수print: 데이타출력

– print (“$var: this is test”);

sprintf,printf: 문자열의 포맷정의– $return=sprintf(“test: %d”, $test);– $var1= sprintf(“%c”, $var[1]);

Page 23: PHP 프로그래밍. Let ’ s start test

PHP: MySQL DBPHP: MySQL DB 연동연동

<?

$connect = mysql_connect( $host, $user, $password );

mysql_select_db( $dbname, $connect );

$query = “select name, age from ………;”;

$result = mysql_query( $query, $connect );

while( $row = mysql_fetch_object( $result ) )

{

echo $row->name;

echo $row->age;

}

mysql_free_result( $result );

?>