Lesson PHP
emeronTI1³ karENnaMGMBI PHP
PHP KWCa Power Tool EdlRtUv)aneKeRbIR)as;sMrab;begáIt
Web Dynamic Content. vaCa Technology mYyEdlRtUv)aneKeRbIR)as;ya:gTUlMTUlay, Free (Opening Source) nigman\T§iBlya:gxøaMgxøaenAkñúg
karRbkYtRbECgCamYy Technology d¾éTeTotrbs; Microsoft’s ASP.
1>
etI
PHP CaGVI? (What is PHP?)
-
PHP CaTMrg;xøIrbs; PHP: Hypertext Preprocessor
-
PHP Ca Server-side Scripting Language
-
PHP eFVIkar Execute Script rbs;xøÜnenAelI Server
-
PHP
Ca Front-End (User Interface) EdlGaceRbIR)as;CamYy Back-End (Database) mYycMnYndUcCa MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc
-
PHP: eyIgGaceFVIkarEkERb PHP Configuration
(Coding)
-
PHP KW Free ehIyeyIgGac Download BI Internet mkeRbIR)as;)an
2> ehtuGVIeRbIR)as; PHP? (Why PHP?)
-
PHP eyIgGaceRbIR)as;vaenAelI RbePT Operating System epSg²KñadUcCa (Windows, Linux,
Unix,…)
-
PHP GaceRbIR)as;)aneRcInRbePT én Web Server dUcCa Apache, IIS,…
-
PHP eyIgGac Download mkeRbIR)as;eday\tKitéfø
-
PHP gayRsYlkñúgkarsikSa
3> dUcemþcehAfa PHP File? (What
is PHP file?)
-
enAkñúg File rbs; PHP
man Text, HTML Tag nig Script
-
PHP File
RtUv)anbMElgenAelI Browser
Ca HTML Code
-
PHP File
man extension dUcCa
.php, .php3 nig .phtml
3> karbegáIt PHP File
eKeRbIR)as;kmµviFImYycMnYn
dUcCa Text Editer, PSPad,
Microsoft Macromedia Dreamwaver, Notepad,…begáIt PHP File kñúgTMrg;Ca Text ehIyrkSaTukkñúgTMrg; Extension Ca .php, .php3 nig .phtml.
emeronTI2³
RbePTénTinñn½y nigGBaØat
1>
rebobkñúgkarsresr
PHP Statement
and run PHP file
PHP RtUv)aneKsresrcenøaHbiTCitmYy
Edlman 2 TMrg;dUcxageRkam³
-
TMrg;Evg Edlcab;epþImeday <?php niigbBa©b;eday ?>
Syntax:
<?php
PHP
Statements (Coding)
?>
Example:
<?php
echo
“Welcome to UBB”;
?>
-
TMrg;xøIEdlcab;epþImeday <? nigbBa©b;eday
?>
Syntax:
<?
PHP
Statements (Coding)
?>
Example:
<?
echo
“Welcome to UBB”;
?>
2>
PHP Comments
PHP Comments KWCa PHP
Statements EdleRbIsMrab; Block code NamYymin[ Execute. PHP
Comments man 2 TMrg;dUcxageRkam³
-
// KWeRbIsMrab; Block Code EtmYybnÞat;
(Single-line Comment)
-
/* (coding) */ KWeRbIsMrab; Block
Code maneRcInbnÞat;
(Large Comments Block)
3>
RbePTTinñn½yrbs;
PHP nig kareRbIR)as;GBaØat
enAkñúg PHP manRbePTTinñn½yeRcInNas;
EtenAkñúgenHeyIgelIkykEtRbePTTinñn½y mYycMnYnmkniyay dUcCa³
- Booleans
: CaRbePTTinñn½ymYyEdlGacecjlT§pl
True rW False.
Example:
<?
$booleans
= True; //
assign the value TRUE to $booleans
echo $booleans;
?>
- Integers:
KWCaelxmYytYkñúgsMnuMelxKt;.
Example:
<?
$i=12;
$a = 12.5;
echo $i; //
Return 12
echo $a; //
Return 12
?>
-
Floating (Double): KWCaelxmYytYkñúgsMnuMelxBit.
Example:
<?
$i = 12;
$a = 12.5;
echo $i; //
Return 12
echo $a; //
Return 12.5
?>
-
String: CaRbePTGkSr EdlRtUv)aneKsresrenAkñúgcenaøHbiTmYy
(Enclose it in single quote (‘’) or double quote (“”) ).
<?
$st1 =
‘Welcome to UBB’;
$st2 =
”Welcome to UBB”;
$st3 = ‘Welcome to UBB “Battmabang
Center”’;
echo $st1; // Welcome to UBB
echo $st2; // Welcome to UBB
echo $st3; // Welcome to UBB “Battambang
Center”
?>
4>
Operator
4>1 Concatenation operator
Concatenation Operator KWCakartPa¢b;BItMélmYyeTAkan;tMélmYyepSg²eTot.
eKeRbIsBaØa (.) sMrab;tPa¢b;BItMélmYyeTAmYy.
Example 1:
<?
$a
= “Welcome”;
$b = “ to UBB”;
$c = $a.$b;
// Welcome to UBB
?>
Example 2:
<?
$a
= “Welcome”;
$a
.= “ to UBB”;
echo
$a; // Welcome to UBB
?>
4>2 Arithmetic Operator
vaCasBaØaeRbIsMrab;
eFVIkarKNnaenAkñúg elxnBVnþ.
Arithmetic Operators
|
||
Example
|
Name
|
Result
|
-$a
|
Negation
|
Opposite of $a.
|
$a + $b
|
Addition
|
Sum of $a and $b.
|
$a - $b
|
Subtraction
|
Difference of $a and $b.
|
$a * $b
|
Multiplication
|
Product of $a and $b.
|
$a / $b
|
Division
|
Quotient of $a and $b.
|
$a % $b
|
Modulus
|
Remainder of $a divided by $b.
|
4>3 Assignment Operators
Assignment
Operators KWCakarepÞrtMéleTA[GBaØat. enAkñúgkareRbIR)as; Assignment
Operator sUmkMuPan;RclMfavaCa kareRbobeFobesµIr (=).
Example:
<?
$a
= ($b = 4) + 5;
echo
$a; // Return 9
$c
= 4;
$c
+= 5;
echo
$c; // Return 9
$d
= “Hello”;
$d
.= “ UBB”;
echo
$d; // Hello UBB
?>
4>4 Comparison Operators
Comparison
Operator KWCasBaØaeRbIsMrab;eFVIkareRbobeFob tMélBIr (Compare two
values).
Comparison
Operators
|
||
Example
|
Name
|
Result
|
$a == $b
|
Equal
|
TRUE
if $a is equal to $b.
|
$a === $b
|
Identical
|
TRUE
if $a is equal to $b, and they are of the same type.
(introduced in PHP 4)
|
$a != $b
|
Not equal
|
TRUE
if $a is not equal to $b.
|
$a <> $b
|
Not equal
|
TRUE
if $a is not equal to $b.
|
$a !== $b
|
Not identical
|
TRUE
if $a is not equal to $b, or they are not of the same
type. (introduced in PHP 4)
|
$a < $b
|
Less than
|
TRUE
if $a is strictly less than $b.
|
$a > $b
|
Greater than
|
TRUE
if $a is strictly greater than $b.
|
$a <= $b
|
Less than or equal to
|
TRUE
if $a is less than or equal to $b.
|
$a >= $b
|
Greater than or equal to
|
TRUE
if $a is greater than or equal to $b.
|
4>5 Logical Operators
Logical Operators
|
||
Example
|
Name
|
Result
|
$a and $b
|
And
|
TRUE if both $a and $b
are TRUE.
|
$a or $b
|
Or
|
TRUE if either $a or $b
is TRUE.
|
$a xor $b
|
Xor
|
TRUE if either $a or $b
is TRUE, but not both.
|
! $a
|
Not
|
TRUE if $a is not TRUE.
|
$a && $b
|
And
|
TRUE if both $a and $b
are TRUE.
|
$a || $b
|
Or
|
TRUE if either $a or $b
is TRUE.
|
emeronTI 3³ kareRbIR)as; Control Structures
1.
etI
Conditional Statement
eRbIsRmab;eFVIGVI?
Conditional Statement KWCa Series
én
Statements EdlRtUv)an Execute enAeBl lkçx½NÐBit.
2.
kareRbIR)as; if construct
If Construct enAkñúg PHP CaTMrg;mYyEdlmansar³sMxan;enAkñúgTMrg;rbs; PHP. vaGnuBaØat[ Execute Code eTAtamlkçx½NÐEdleyIg)ankMnt;kñúg
Code.
Syntax:
<?
if(expression){
//PHP Code
}
?>
Example:
<?
$a = 10;
$b = 7;
if($a > $b){
echo “a is
greater than b.”;
}
?>
3.
kareRbIR)as; else construct
else construct KWCa construct mYyEdl Execute
Statement mYyenAeBlCYb lkçx½NÐBit. EtvaGac Execute Statement mYyepSgeTot
enAeBlvaCYblkçx½NÐminBit.
Syntax:
<?
if(expression){
Statement
if true
}else{
Statement
if false
}
?>
Exampe:
<?
$a = 10;
$b = 7;
if($a > $b){
echo “a is
greater than b.”;
}else{
echo “b is
greater than a.”;
}
?>
4.
kareRbIR)as; else if / elseif construct
else if / elseif KWCakar Combine rvag if nig else. else )anbMEbk if statement fµI enAkñúg original if EdlCYblkçx½NÐ false.
Syntax:
<?
if(expression 1){
Statement
if expression 1 met TRUE
}elseif(expression 2){
Statement
if expression 2 met TRUE
}………………..{
…………………
}elseif(expression n){
Statement
if expression n met TRUE
}else{
Statement
if expression met FALSE
}
?>
Example:
<?
$day = “Monday”;
if($day ==”Monday”){
echo
“Today is Monday”;
}elseif($day
==”Tuesday”){
echo
“Today is Tuesday”;
}elseif($day
==”Wednesday”){
echo
“Today is Wednesday”;
}elseif($day
==”Thursday”){
echo
“Today is Thursday”;
}elseif($day
==”Friday”){
echo
“Today is Friday”;
}else if($day
==”Statursday”){
echo
“Today is Satursday”;
}else{
echo
“Today is Sunday”;
}
?>
5.
kareRbIR)as; switch construct
switch construct KWvamanlkçx½NÐRsedogKñaenAnwg elseif construct pgEdr.
vaRtUv)an eKeRbIsMrab;eRbobeFob Variable
rW Expression CamYy
Value epSg²Kña
ehIyva Execute eTAtam
karesµIrKñarvag Variable nig Value.
Syntax:
<?
switch(expression){
case value
1:
echo
“Value 1”;
break;
case value
2:
echo
“Value 2”;
break;
……………………….
……………………….
……………………….
case value
n:
echo
“Value n”;
break;
default:
echo
“expression met false”;
break;
}
?>
Example:
<?
$day = "Monday";
switch $day{
case "Monday":
echo "Today is
Monday";
break;
case "Tuesday":
echo "Today is
Tuesday";
break;
case "Wednesday":
echo "Today is
Wednesday";
break;
case "Thursday":
echo "Today is
Thursday";
break;
case "Friday":
echo "Today is
Friday";
break;
case "Satursday":
echo "Today is
Satursday";
break;
default:
echo "Today is Sunday";
break
}
?>
emeronTI
4³ kareRbIR)as; Loop Structures
1> etI Loop structure eRbIsRmab;eFVIGVI?
Loop Structure KWCa Structure
mYyenAkñúg
PHP EdleFVIkar Execute sMnMu Code mþgehIymþgeTotCalMdab;edIm,ITTYllT§pl
kñúglkçx½NÐBit. CaFmµtaGVIEdl Loop dMeNIrkarenaHKW esµIrnwg Statement
mYyEdlmantMél
True rW False.
2> GtßRbeyaCn¾kñúgkareRbIR)as; Loop Structure
Loop Structure RtUv)aneKeRbIR)as;vaedIm,I
kat;bnßykarsresr Code mþgehIymþgeTot
enAkñúglkçx½NÐBit Edl)ankMNt;eday Parameter mYy.
3> The while…… statements
while KWCa Structure mYyenAkñúg
programming language EdleFVIkar execute nested statement(s) mþgehIymþgeTot
kñúgxN³eBlEdl while expression mantMélesµIr True. tMélén Expression
RtUv)anRtYtBinitüral;eBlénkarcab;epþImrbs;
Loop.
Syntax:
<?
while(expression){
Statements
}
?>
Example:
<?
$i
=1;
$sum
= 0;
while($i
< 10){
$sum
= $sum + $i;
$i++;
}
echo
$sum;// Return 45
?>
4> The do-while…… statements
do-while KWva structure
mYyEdlmanlþkçN³RsedogKñaenAnwg
while pgEdr.
b:uEnGVIEdlxusRtg;fa
do-while FanafanwgdMeNIrkar enAtMéldMbUgén Loop eTaHbICavaxuslkçx½NÐ
k¾eday.
Syntax:
<?
do{
Statements
}while(expression);
?>
Example:
<?
$i
=1;
$sum
= 0;
do{
$sum
= $sum + $i;
$i++;
}while($i<10);
echo
$sum; // Return 45
?>
5> The for…… statements
for KWCa structure loop mYyRbePTEdldMeNIrkar
enAkñúgEdnkMNt; first expression nig end expression.
Syntax:
<?
for(expression1,
expression2, expression3){
Statements
}
?>
Example:
<?php
$sum = 0;
for($i=1; $i < 10; $i++){
$sum = $sum + $i;
}
echo $sum; //Return 45
?>
6> The foreach…… statements
foreach KWCa looping
construct mYyEdlRtUv)aneKeRbIR)as;CamYy Array object. foreach
man
2 Syntax:
Syntax1:
<?
foreach(array_expression
as $value){
Statement
}
?>
Example1:
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
$value = $value *
2;
echo
$value."<br>";
}
unset($value); // break the reference with the last element
?>
Syntax2:
<?
<?
foreach(array_expression
as $key => $value){
Statement
}
?>
Example2:
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $key => $value) {
$value = $value *
2;
echo $key."
=> ".$value."<br>";
}
unset($value); // break the reference with the last element
?>
emeronTI 5³ kareRbIR)as; Array Objects
3>
dUcemþcEdlehAfa Array?
Array KWCa Variable EdlGacpÞúktMél)aneRcIn
Data Type dUcKña , Array Name dUcKña EtvaxusKñaeTAtam index
rbs;
Array.
Syntax:
<?
$arr
= array(value1, value2, value3,…,valuen);
?>
4> etI Array manb:unµanRbePT?
enAkñúg
PHP eKEck Array Ca 3 RbePT³
Enumerated
array, Associative array, Multidimensional array
2>1 Enumerated Array
Enumerated Array KWCa Array
EdleRbIR)as;
Integer sMrab;cg¥úlbgðaj elx Index rbs; Array.
Example:
<?
$arr = array("Dara","Veasna","Sokha");
echo $arr[0]."<br>";
echo $arr[1]."<br>";
echo $arr[2]."<br>";
?>
2>2 Associated Array
Associated Array KWCa Array
mYyEdleRbIR)as;
String Ca Index énFaturbs; Array.
Example:
<?
$arr = array("a" => "Dara","b" =>
"Veasna","c" => "Sokha");
echo $arr["a"]."<br>";
echo $arr["b"]."<br>";
echo $arr["c"]."<br>";
?>
2>3 Multidimensional
Arrays
Multidimensional Arrays KWCa Array
mYyRbePTEdlFaturbs;vaCa Array.
Example:
Example:
<?
$arr = array("a" =>
array("sub1" => "I go","sub2" => "We
go"),"b" =>array("ob1" => " to
school", "ob2" => " home"));
echo
$arr["a"]["sub1"].$arr["b"]["ob1"]."<br>";
echo
$arr["a"]["sub2"].$arr["b"]["ob2"]."<br>";
?>
2>3 Array Function
To working with array in PHP we have some function that
working properly with array. These function have:
Function name |
Task |
count($arrName)
|
Use to find the length of array
|
array_slice($arr,NumArr)
|
Use to reduces the number of elements of
array
|
array_shift($arrname)
|
Use to remove the first element of array
|
array_push($arr,$val)
|
Use to increase the element of array
|
array_merge($arr1,$arr2)
|
Use to combine the element of two or more
array
|
foreach( $array)
|
Use to loop the array
|
preg_grep()
|
Use to find the array element
|
array_diff()
|
Find the different element of two array
|
array_reverse()
|
Use to reverse the array element
|
sort()
|
Use to sort array in ascending
|
rsort()
|
Use to sort array in descending
|
asort()
|
Use to sort array without change index
|
ksort()
|
Use to sort the indices of and associative
array
|
krsort()
|
Similar to ksort() function
|
Example
arrSize.php
<HTML>
<HEAD>
<TITLE> Array</TITLE>
<BODY>
<?php
$arr=array("001"=>"Sovtia","002"=>"
Phirom ","003"=>"Long");
$sizeOfarray=count($arr);
print "Size:".$sizeOfarray;
?>
</BODY>
</HTML>
Example
arrSlice.php
<HTML>
<HEAD>
<TITLE> Array</TITLE>
<BODY>
<?php
$arr=array("Sovitia","Long","Phirom","Sambo","Chanthorn");
$Newarr=array();
//Print element befor slice
for($i=0;$i<count($arr);$i++)
{
print "Lecturer
Name:<B>".$arr[$i]."</B><BR>";
}
//Print element after slice slice
print "<hr width=50%><H2>After slice</H2>";
$Newarr=array_slice($arr,2);
for($i=0;$i<count($Newarr);$i++)
{
print "Lecturer
Name:<B>".$Newarr[$i]."</B><BR>";
}
?>
</BODY>
</HTML>
emeronTI 6³
kareRbIR)as;GnuKmn’ (Functions)
1> dUcemþcEdlehAfa Functions?
Function KWCakUnkMNat;kmµviFI
EdlvanwgeFVIkar Execute enAeBleyIgehAeQµaHrbs;vaeTAkEnøgNamYyén Page.
2> RbePTrbs; Functions
enAkñúg PHP eKEck
Functions Ca 2 RbePT³ Built-in functions nig User-defined
functions.
2>1 Built-in functions
Built-in functions
KWCa
Functions EdlmanRsab;rbs; PHP. Ex: str_replace(),
strtoupper(), strtolower(), ucfirst(), trim(), substr(), ….
Example1:
str_replace($search, $replace, $subject)
<?
$search = "BBA";
$replace = "UBB";
$subject = "I have studied in
BBA.";
$result = str_replace($search,$replace,
$subject);
echo $result;//Ihave studied in UBB.
?>
Example2:
strtoupper($string), strtolower($string), ucfirst($string), trim($string)
<?
$string = "UBB";
$trim = " Hello ";
$strtolower = strtolower($string);//(សំរាប់ប្តូរអក្សរធំទៅអក្សរតូច)
$strtoupper = strtoupper($strtolower); //(សំរាប់ប្តូរអក្សរតូចទៅអក្សរធំ)
$trim = trim($trim);
$ucfirst = ucfirst($strtolower);
echo "strtolower is
".$strtolower.".<br>";
echo "strtoupper is
".$strtoupper.".<br>";
echo "trim is
".$trim.".<br>";
echo "ucfirst is
".$ucfirst.".<br>";
?>
Example3:
substr($string,$start,$length)
<?
$string = "UBB";
echo
substr($string,4)."<br>"; //Siem Reap Center
echo
substr($string,-6)."<br>";//Center
echo substr($string,1,6)."<br>";//BU
Sea
echo
substr($string,-4,-1)."<br>";//nte
?>
2>2 User-defined functions
2>2 User-defined functions
User-defined
functions KWCa Functions mYyEdlbegáIteLIgeday Gñksresr Code pÞal;.
vaGacbMeBjtam tMrUvkarrbs;GñkeRbIR)as;.
Syntax1:
Non-Argument
<?
functions
functions_name(){
Statements
}
?>
Syntax2: Function
Arguments
<?
functions
functions_name($arr1, $arr2,…,$arrn){
Statments
}
?>
2>2>1
PHP Functions –
Non Return Values
vaCa Functions
mYyEdlminTamTaȟ
Return tMéleTAkan; User enaHeT.
Example1:
<?
functions student_name(){
$stuent_name = “Dara”;
echo $student_name;
}
student_name();//Dara
?>
Example2:
<?
functions sum($a,$b){
$c = $a + $b;
echo $c;
}
sum(10,5); //15
sum(2,4); //6
?>
2>2>2
PHP Functions –
Return Values
vaCa Functions
mYyEdlTamTaȟ
Return tMéleTAkan; User vij.
Example1:
<?
functions student_name(){
$stuent_name = “Dara”;
return $student_name;
}
echo student_name();//Dara
?>
Example2:
<?
functions sum($a,$b){
$c = $a + $b;
return $c;
}
echo sum(10,5); //15
echo sum(2,4); //6
?>
emeronTI 7³ kareRbIR)as Form in PHP
5> Form nig Form Elements
xageRkamenHCa Code nig interface
én
Form.
eyIgman index.php
<html>
<head>
<title>About Form</title>
</head>
<body>
<form name="form1" method="get/post"
action="#">// (*)
<table
width="500" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td
colspan="2" align="center"><h3>Student
Name</h3></td>
</tr>
<tr>
<td
width="164">Student Name</td>
<td
width="330"><label>
<input
name="txt_studentname" type="text" id="txt_studentname"
size="30">
</label></td>
</tr>
<tr>
<td>Sex</td>
<td><label>
<select
name="txt_sex" id="txt_sex">
<option
value="Female">Female</option>
<option
value="Male">Male</option>
</select>
</label></td>
</tr>
<tr>
<td>Address</td>
<td><label>
<input
name="txt_address" type="text" id="txt_address"
size="45">
</label></td>
</tr>
<tr>
<td
colspan="2"><label>
<input
type="submit" name="submit" id="submit"
value="Submit">
<input
type="reset" name="reset" id="reset"
value="Reset">
</label></td>
</tr>
</table>
</form>
</body>
</html>

6> kareRbIR)as; $_GET Functions nig GET Method
$_GET Functions KWCa Functions
mYyEdlRbmUltMélecjBI
Form mYyehIybBa¢ÚntMélecj method=”get”
rW
method=”GET”. ral;tMélEdlvabBa¢ÚnecjeyIgGacemIleXIjenA Address
bar rbs;
Browser. vaGacbBa¢Ún)ancMnYntYGkSreRcInbMput KW 100tY.
Example:

http://127.0.0.1/lesson7/get.php?txt_studentname=&txt_sex=Female&txt_address=&submit=Submit
Note: <form
name="form1" method="get" action="get.php">//
(*)
get.php
<?
$st_name =
$_GET['txt_studentname'];
$sex =
$_GET['txt_sex'];
$address =
$_GET['txt_address'];
echo "Student Name:".$st_name."<br>";
echo
"Sex:".$sex."<br>";
echo
"Address:".$address."<br>";
?>
7> kareRbIR)as; $_POST Functions nig POST Method
$_POST Functions KWCa Functions
mYyEdlRbmUltMélecjBI
Form mYyehIybBa¢ÚntMélecj method=”post”
rW
method=”POST”. ral;tMélEdlvabBa¢ÚnecjeyIgGacemIlmineXIjenA Address
bar rbs;
Browser. vaGacbBa¢Ún)anKµankMNt;tYGkSreT.
Note: <form
name="form1" method="post"
action="post.php">// (*)
get.php
<?
$st_name =
$_POST['txt_studentname'];
$sex =
$_POST['txt_sex'];
$address = $_POST['txt_address'];
echo "Student
Name:".$st_name."<br>";
echo
"Sex:".$sex."<br>";
echo
"Address:".$address."<br>";
?>
8> kareRbIR)as; $_REQUEST Functions
$_REQUEST
Functions KWGaceRbIR)as;CMnYsTaMg $_POST, $_GET nig $_COOKIE. $_REQUEST
Functions GaceRbIR)as;sMrab;RbmUltMélCamYyTaMg POST nig GET
method.
Note: <form
name="form1" method="get/post"
action="request.php">// (*)
request.php
<?
$st_name = $_ REQUEST
['txt_studentname'];
$sex = $_ REQUEST ['txt_sex'];
$address = $_ REQUEST
['txt_address'];
echo "Student
Name:".$st_name."<br>";
echo
"Sex:".$sex."<br>";
echo
"Address:".$address."<br>";
?>
6> kareRbIR)as; Form with Array
form_array.php
<html>
<head>
<title>Form with Array</title>
</head>
<body>
<form action="form_array_submit.php" method="post"
name="form_array">
<p><strong>What facultys do you prefer to study?
</strong>
</p>
<p>
<label>
<input
type="checkbox" name="fac[]" id="fac[]"
value="Information Technology">Information Technology<br>
<input
type="checkbox" name="fac[]" id="fac[]"
value="Business Management">Business Management<br>
<input
type="checkbox" name="fac[]" id="fac[]"
value="Engineering">Engineering<br>
<input
type="checkbox" name="fac[]" id="fac[]"
value="English Education">English Education<br>
<input
type="checkbox" name="fac[]" id="fac[]"
value="Law">Law<br>
<input
type="checkbox" name="fac[]" id="fac[]"
value="Tourism">Tourism<br>
</label>
</p>
<input
name="Submit" type="submit"
value="Submit"><input name="Reset"
type="reset" value="Reset">
</form>
</body>
</html>
form_array_submit.php

<html>
<head>
<title>I prefer to study...</title>
</head>
<body>
<?
$fac =$_POST['fac'] ;
echo "<b>I prefer to
study:</b><br>";
echo "<ol>";
foreach($fac as $value){
echo
"<li>".$value."</li><br>";
}
echo "<ol>";
?>
</body>
</html>

emeronTI 8³ kareRbIR)as date() functions
1> The PHP date() functions
The PHP date()
functions KWCaTMrg; format rbs; timestamp [eTaCaTMrg; date/time
EdlGacemIlyl;.
timestamp KWCatYGkSrbnþrbnÞab;KñabegáIt)anCa date and/or
time sMrab;sMKal;RBwtikarN_mYyc,as;las;.
Syntax
date(format,timestamp)
|
Parameter
|
Description
|
Format
|
Required. Specifies
the format of the timestamp
|
timestamp
|
Optional. Specifies
a timestamp. Default is the current date and time
|
2> PHP Date() – Format the Date
xageRkamenHCa
tYGkSrmYycMnYnEdlGaceRbIR)as;kñúgkar format date/time:
format character
|
Description
|
Example
returned values
|
Day
|
---
|
---
|
d
|
Day of the month, 2 digits with leading zeros
|
01 to 31
|
D
|
A textual representation of a day, three letters
|
Mon through Sun
|
j
|
Day of the month without leading zeros
|
1 to 31
|
l (lowercase 'L')
|
A full textual representation of the day of the week
|
Sunday through Saturday
|
N
|
ISO-8601 numeric representation of the day of the week
(added in PHP 5.1.0)
|
1 (for Monday) through 7 (for Sunday)
|
S
|
English ordinal suffix for the day of the month, 2
characters
|
st, nd, rd or th. Works well
with j
|
w
|
Numeric representation of the day of the week
|
0 (for Sunday) through 6 (for Saturday)
|
z
|
The day of the year (starting from 0)
|
0 through 365
|
Week
|
---
|
---
|
W
|
ISO-8601 week number of year, weeks starting on Monday
(added in PHP 4.1.0)
|
Example: 42 (the 42nd week in the year)
|
Month
|
---
|
---
|
F
|
A full textual representation of a month, such as January
or March
|
January through December
|
m
|
Numeric representation of a month, with leading zeros
|
01 through 12
|
M
|
A short textual representation of a month, three letters
|
Jan through Dec
|
n
|
Numeric representation of a month, without leading zeros
|
1 through 12
|
t
|
Number of days in the given month
|
28 through 31
|
Year
|
---
|
---
|
L
|
Whether it's a leap year
|
1 if it is a leap year, 0 otherwise.
|
o
|
ISO-8601 year number. This has the same value as Y,
except that if the ISO week number (W) belongs to the previous or next
year, that year is used instead. (added in PHP 5.1.0)
|
Examples: 1999 or 2003
|
Y
|
A full numeric representation of a year, 4 digits
|
Examples: 1999 or 2003
|
y
|
A two digit representation of a year
|
Examples: 99 or 03
|
Example:
<?php
echo date("d/m/Y") . "<br />";
echo date("d-m-Y") . "<br />";
echo date("d.m.Y")
?>
echo date("d/m/Y") . "<br />";
echo date("d-m-Y") . "<br />";
echo date("d.m.Y")
?>
lT§plén Code xagelIKW³
24/12/2009
24-12-2009
24.12.2009
3> PHP Date() – Adding a
Timestamp
timestamp parameter KWCa optional
parameter enAkñúg date() functions EdlkMnt;tMél timestamp.
RbsinebImin)ankMnt;tMél[ timestamp eTenaH timestamp KWCa current
date.
mktime() functions KWCa functions mYynwge)aHtMél Unix
timestamp sMrab; date.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)
|
Example:
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
4>
list of characters format
date function
Character EdlGaceRbIR)as;enAkñúg
date() functions mandUcCa “.”, “/”, “-“.
emeronTI 9³ kareRbIR)as; PHP Include() and Require()
Functions
enAkñúg
PHP eyIgGaceFVIkar insert content én file
php mYyeTAkñúg file php mYyeTotmuneBlEdl
server side eFVIkar execute va edayeRbI include() rW require()
function. functions TaMg
2 manlkçN³dUcKµaeRcInya:g
EtvaxusKñaRtg;fa³
- include() functions: Gac
generate error. ehIy script enAEttMeNIrkarenAeBlCYb
error.
- require() functions:
minGac generate error. script enAminGactMeNIrkarenAeBlCYb
error.
functions TaMg 2 RtUv)aneKeRbIsMrab;begáIt
functionsmYycMnYn header, footer nig ral; elements
epSg²
EdlGaceRbIR)as;mþgehIymþgeToteTARKb; pages TaMgGs;.
enAeBlEdleyIgcg;eFVIkar update enAelIcMnucmYycMnYn dUcCa header, footer nig elements
epSg²
enaHmincaM)ac; update RKb; pages enaHeT. eyIgGaceFVIkar update
EtenA
elI File include nImYy². eyIgGac save file kñúg extension
Ca
.html, .php, .inc, ….
1> The PHP include() functions
The include() functions eFVIkarRbmUlral;
content enAkñúg include file bBa¢ÚleTAkñúg file
Edl)an
include. RbsinCaman error ekIteLIg include()
functions eFVIkar generate warning rIÉ script enAEttMeNIrkar.
Example:
include.php
<p>
<a
href="#">Home</a>
<a href="#">About
Us</a>
<a
href="#">Contact Us</a>
<a
href="#">Testimonials</a>
</p>
run.php
<html>
<head>
<title>Include</title>
</head>
<body>
<? include('include.php');?>
Now we use include function.
</body>
</html>
(lT§plenAeBlKµan error ekIteLIg)

(lT§plenAeBlman error ekIteLIg)

2> The PHP require() functions
The require() functions eFVIkarRbmUlral;
content enAkñúg require file bBa¢ÚleTAkñúg file
Edl)an
require. RbsinCaman error ekIteLIg require()
functions mineFVIkar generate warning rIÉ script enAQb;tMeNIrkar.
Example:
require.php
<p>
<a
href="#">Home</a>
<a href="#">About
Us</a>
<a href="#">Contact
Us</a>
<a
href="#">Testimonials</a>
</p>
run.php
<html>
<head>
<title>Include</title>
</head>
<body>
<? include('require.php');?>
Now we use include function.
</body>
</html>
(lT§plenAeBlKµan error ekIteLIg)

(lT§plenAeBlman error ekIteLIg)

emeronTI 10³ kareRbIR)as; PHP Cookies and Sessions
1>
The PHP Cookies
1>1 etIGVIeTACa Cookies?
Cookie() RtUv)aneKeRbIR)as;vasMrab;kMnt;sMKal; user. Cookie() Ca file
tUcmYyenAelI server embedded enAelI
computer rbs;Gñk.
enAkñúgeBlEtmYyRbsinebI clients eRcIneFVIkar request eTAkan; cookie() enaH
clients TaMgGs; suT§EtGac
create nig retrieve tMé;lrbs; cookie().
1>1How to Create a Cookie?
setcookie() KWCa function mYyEdlEdleRbIsMrab;begáIt cookie. eyIgKYrkt;sMKal;fa eyIgKYrEtbegáIt cookie() enAelI html tag.
Syntax: setcookie(name,
value, expire);
Example:
xageRkamenH
KWCa]Tarh_kñúgkarbegáIt cookie() eQµaHfa
User edaykMnt;tMél[va admin. cookie() mYyenHnwg
expired kñúgryHeBl 1 ema:g.
<?php
setcookie(“user”, “admin”, time()+3600);
?>
<html>
setcookie(“user”, “admin”, time()+3600);
?>
<html>
</html>
1>2
How to Retrieve a Cookie Value?
$_COOKIE KWCa variable mYyEdleRbIsMrab;TTYltMélrbs;
cookie. isset()
Ca function mYyEdleRbIsMrab;EsVg
rkfaetI cookie )anbegáItEdrrWeT.
Example:
<?php
if(isset($_COOKIE[’user’])){
echo $_COOKIE[’user’];
}else{
echo “Hello guest!”;
}
?>
1>4 How to Delete a Cookie?
enAeBleyIgkMBugEt delete cookie eyIgRtUvR)akdfa expiration date KWenAkñúgGtItkal.
Example:
<?php
// set the expiration date to one hour ago
setcookie(“user”, “”, time()-3600);
?>
// set the expiration date to one hour ago
setcookie(“user”, “”, time()-3600);
?>
1>
The PHP sessions
PHP sessions KWCa variable mYyRtUv)aneKeRbIsMrab; store rW change setting rbs; session user. vapÞúkBt’manrbs; user EtmYyEdlGaceRbIR)as;)aneTAkan;RKb; pages TaMgGs;enAkñúg application
EtmYy.
2>1 Starting a PHP Sessions
muneBleyIg store Tinñn½yeTA[ sessions eyIgKYrEt start vaCamunsin. kar start
sessions eyIgKYrEt start vaenAelI html tag.
Example:
<?php session_start(); ?>
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
2>2
Store and Retrieve a session
enAkñúgkar
Store nig Retrieve
session variable edayeRbIR)as;
PHP $_SESSION variable.
Example:
<?php
session_start();
if(isset($_SESSION[’views’]))
$_SESSION[’views’]=$_SESSION[’views’]+1;
else
$_SESSION[’views’]=1;
echo “Views=”. $_SESSION[’views’];
?>
session_start();
if(isset($_SESSION[’views’]))
$_SESSION[’views’]=$_SESSION[’views’]+1;
else
$_SESSION[’views’]=1;
echo “Views=”. $_SESSION[’views’];
?>
2>3 Destroying a Session
RbsinCaGñkmaneKalbMNglubTinñn½y
ecjBI session eyIgeRbIR)as;nUv Functions
unset() rW session_destroy()
functions.
kareRbIR)as;
unset() function KWlub
session variable c,as;las;.
Example:
<?php
unset($_SESSION['views']);
?>
unset($_SESSION['views']);
?>
kareRbIR)as;
session_destroy() function KWlubral;
session variable TaMgGs;EdlmanenAkñúg
kmµviFI.
Example:
<?php
session_destroy();
?>
session_destroy();
?>
No comments:
Post a Comment