Upload
rodney-martin
View
224
Download
7
Embed Size (px)
Citation preview
PPHP HP 프로그래밍프로그래밍
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>
BasicsBasics
PHP 코드의 시작과 끝 : <script language=“php”> </script> <?php ?> <? ?>
BasicsBasicsFunction
– function fname (parm,..){ …}e.g.) function add($a,$b)
{$value = $a+$b;return ($value);
}$v = add(30,5);
functionsfunctions<? function test($args) {
$args += 10; }
$realargs = 10; test($realargs); echo (“$realargs”);?>
=> Results? 10 (call by value)
functionsfunctionsfunction makecoffee($type = “cappucino”) { return $type;}echo makecoffee();echo makecoffee(“espresso”);
BasicsBasicsFunction scope
e.g.) $data = “test”;function aaa()
{
$data = “new”;
echo $data;
}
echo $data;
Identifier, CommentsIdentifier, Comments영문자 , 밑줄 , $ 기호로 시작내장함수는 대소문자 구별하지 않음
– print = Printcomments: /*, */ , //, #자료형
– Integer, Floating-point number, String, array, object
– 특수문자 :\n,\r,\t, \\,\$, \”– string 사용법 : “ ”, ‘ ’
변수변수 : : 전역변수전역변수 , , 지역변수지역변수<?
$a = 1;
function test()
{
echo $a;
}
test();
?>
<?
$a = 1;
function test()
{
global $a;
echo $a;
}
test();
?>
Static VariableStatic Variablefunction Test() {
static $a=0;
echo $a;
$a++;
}
Variable typeVariable typeNo specific type declaration required.Type casting
– $foo=10;– $bar=(double) $foo;
관련함수– gettype(), is_long(). Is_string(), is_array(), is
_object(), …
변수타입 변환변수타입 변환문자열이 숫자 값일 때 :
– . 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)
변수변수 : : 변수 받아오기변수 받아오기
<form action=‘test.php’ method=post>
ID : <input type=text name=userid>
<input type=submit>
</form>
<?
$userid = $_POST[“userid"];
echo $userid;
?>
다른
Html문서
test.php
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
ArrayArray$a[0]=“abc”;$b[“foo”]=13;$a[1][0]=$f;$a[“foo”][1]=$f;
Array InitializationArray Initialization $a = array (
“color”=>”red”, “taste”=>”sweet”, 3=>4);
$a = array (“apple” => array (
“color” => “red”, “taste”=>”sweet”),
“orange”=> array (…
)
);
수식과 연산자수식과 연산자 $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;
ifif if (expr) {statement;}
if (expr1) {statement1;}
elseif (expr2)
else { statement2;}
<? if ($a ==5): ?>a=5; b=4;
<? endif: ?>
whilewhilewhile (expr) { statement1;}while (expr): statement1; endwhile
<?$x=1;
while ($x <= 100):
$sum += $x; $x +=1;
endwhile;
?>
forfor for (expr1;expr2;expr3) { statement1;
…
}
for (expr1; expr2; expr3):
statement1;
endfor;
Class & ObjectClass & Object<?
class Cart {var $items;function add_item ($artnr, $num)
{ $this->items[$artnr] +=$num;
}}$cart = new Cart;$cart->add_item(“10”,1);
?>
문자열 처리 함수문자열 처리 함수print: 데이타출력
– print (“$var: this is test”);
sprintf,printf: 문자열의 포맷정의– $return=sprintf(“test: %d”, $test);– $var1= sprintf(“%c”, $var[1]);
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 );
?>