PHP Function

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Function
F function
function( F F ก F F )
{
…….. ;
…….. ;
return F F ก F ก ;
}

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Function
<?php
echo hello();
function hello()
{
return "hello";
}
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Function
<?php
echo showNum(1);
function showNum($n)
{
if($n=="1")
{return 1;}
else if($n=="2")
{return 2;}
}
?>
projectsoft.biz
ก
Web Application F
PHP MySQL Ajax(jquery)
PHP Function
showStart(3);
function showStart($n){
$count1=1;
while($count1<=$n){$count2=1;
while($count2<=$count1)
{echo "*";$count2++;}
echo "<br />";
$count1++;}
}
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Sub String Function
substr(string,start position,length );
F
<?php
$str="abc";
echo substr($str,1,1);
?>
F F b

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Sub String Function
<?php
$str="abc";
echo substr($str,1);
?>
F

F
F

bc
F

ก F F F
F

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

ˈ F

F
string
F
F

Web Application F

string
F ก

F ก
string
F

F
PHP Sub String Date
<?php
echo showDate("2010-10-18");
function showDate($date)
{
$day=substr($date,8,2);
$month=substr($date,5,2);
$year=substr($date,0,4);
return $day."/".$month."/".$year;
}
?>
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP String Replace
<?php
$str="hello WORLD";
echo str_replace("hello","HELLO",$str);
?>
F F HELLO WORLD
ก
F
F F string
ก
F ก F ก
F
string
F
string F

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

string
PHP String Replace
<?php
$str="anbncn";
echo str_replace("n","<br />",$str);
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP String Length
Function strlen() F
<?php
echo strlen("php");
?>
F F ˈ
php

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

ก

ก

F 3

string F

ก
PHP String Length
<?php
$str="12345";
if(strlen($str)>6){echo "greater than 6 ";}
else{echo “less than 6”;}
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP String Position
strpos($string, "find string")
Function strpos F
F
F F
string
$string
string
ก
find string
ก
F ก
<?php
$str="@bc";
echo strpos($str,"@");
?> ก
F
ก
0
ก
F 0
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

F

ก
F ก

F

F
F

F

Fก

F
F

F
PHP String Position
ก
F
F ก F
function strpos return false
<?php
$mystring = 'bc';
$findme = 'a';
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string ”.$findme.“ was not found in the string”;
}
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
F ก

ก

file

<?php
$file_name="my_picture.jpg";
$type = substr($file_name, strpos($file_name,"."));
echo $type;
?>
F
ก
.jpg

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
ก

string

ก

string ก
ก
function strtolower()
<?php
$str1="HeLLoWoRLd";
$str2=strtolower($str1);
echo $str2;
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

ก

F ˈ
F ˈ

F ก
F ก F F
ก

string

ก

string ก
ก
function strtoupper()
<?php
$str1=“heLLoWoRld";
$str2= strtoupper($str1);
echo $str2;
?>

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

ก

F ˈ
F ˈ

F

F F
F F F
F function date()
ʾ

ก F ก
ˆ
function date() F
• date("d/m/Y")
• ก F ก
F
ˆ
• date("H:i:s")

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

/ ʾ

/

•

ˆ

Web Application F

ˈ

:

F

:

F
ʾ
•
•
•
•
•
•
•
•

F function date()

function date()
F ก
F
ˈ
d
m
Y
ʾ ..
ก
y
ʾ ..
ก
H
ˈ
i
s
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

F parameter
ʾ

Web Application F

ˈ sting

F
ก

F

<?php
$strDate1="2012-01-01";//
F
$strDate2="2012-02-1";//
echo DateDiff($strDate1,$strDate2);
// ก function DateDiff
F

F

F

// F function DateDiff
F
F $strDate1
$strDate2
function DateDiff($strDate1,$strDate2)
{
//
ˈ
F
// F
F 86400 ( 1
86400
)
F
return (strtotime($strDate2) - strtotime($strDate1))/ ( 60 * 60 * 24 );
}
?> projectsoft.biz
PHP MySQL Ajax(jquery)

ก

Web Application F
ก

/F

<?php
//ก
echo date('Y-m-d', strtotime(' +1 day'));//

n

php
F

echo date('Y-m-d', strtotime('+60 day', strtotime("2012-5-2")));
//
ก
2 2554 60
//ก
กF
F
echo date('Y-m-d', strtotime(' -1 day'));//

echo date('Y-m-d', strtotime('-60 day', strtotime( "2012-1-31" )));
//
F
ก
1 2554 60
?>
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP Include

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP include
include(“
F.php”);
include(“
.php”);
F
ก
F
include
F F
F include1_1.php
<?php echo "Hello ";?>
<?php
include("include1_1.php");
echo " World";
?>
F F Hello World
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F

F
PHP include
F

F include1_1.php F folder include

F

ก

<?php
include("include/include1_1.php");
echo " World";
?>
F
F

F
F

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

ˈ Hello World
F ก F

Web Application F

F

F
F

F ก include
PHP include + Function
F showDate.php
F function F
<?php
function showDate($date)
{
$day=substr($date,8,2);
$month=substr($date,5,2);
$year=substr($date,0,4);
return $day."/".$month."/".$year;
}
?>
projectsoft.biz
ก
PHP MySQL Ajax(jquery)

Web Application F
PHP + Include
<?php
include("function/showDate.php");
$date=showDate("2010-10-01");
echo $date;
?>
F

include

projectsoft.biz
ก
PHP MySQL Ajax(jquery)

function showDate F

Web Application F

Session3