More Related Content Similar to PHP Tutorial (funtion) Similar to PHP Tutorial (funtion) (20) PHP Tutorial (funtion)4. 4
PHP
gettype()
data type
gettype($varname);
Demo
<? #settype.php
settype($i, "integer");
print(gettype($i));
?>
5. 5
PHP
isset()
( = 1, =
blank)
isset($varname);
Demo
<? #isset.php
settype($i, "integer");
print(gettype($i));
print("<br>".isset($a));
print("<br>".isset($i));
?>
7. 7
Introduction to HTML
HTML
<html>
<head>
<!-- comment for HTML -->
</head>
<!-- comment -->
<body>
<h1>php with html</h1>
<?php
//---
print "PHP with HTML";
?>
<form>
<?php
//---
?>
</form>
</body>
</html>
8. 8
Introduction to HTML
HTML
<!--comment for HTML-->
<html>
<head>
<title>MyHTML</title>
</head>
<body>
</body>
</html>
20. 20
Introduction to HTML
Demo – table.htm
<!-- table.htm -->
<html>
<head></head>
<body>
<table border="1"> <!-- tag table opened here -->
<tr> <!-- tag tr for row opened here -->
<th>No.</th> <!-- tag th for table header -->
<th>col1</th>
<th>col2</th>
</tr> <!-- tag th for table header -->
<tr>
<td>row1</td> <!-- tag td for column detail -->
<td>table detail row 1 column 1</td>
<td>table detail row 1 column 2</td>
</tr>
<tr>
<td>row2</td>
<td>table detail row 2 column 1</td>
<td>table detail row 2 column 2</td>
</tr>
</table> <!-- tag table closed here -->
</body>
</html>
21. 21
Introduction to HTML
Demo – frminput.htm
<!-- frminput.htm -->
<html><!-- frminput.htm-->
<head><title>Input form</title></head>
<body>
<form name="frmInput" method="post" action="getInfo.php">
Student Name :<input type=text name=txtName><br>
Student ID :<input type=text name=txtID><br>
Sex : <select name=selSex>
<option value=M> </option>
<option value=W> </option>
</select>
Score :<input type=text name=txtScore><br>
<input type=submit value=Submit><br>
</form>
</body>
</html>
22. 22
Introduction to HTML
Demo – getinfo.php
<? #getinfo.php
echo "<center><h3>";
print " <br>";
print " $txtName<br>";
print " $txtID<br>";
switch($selSex){
case "M" : $selSex=" "; break;
default : $selSex=" "; break;
}
print " $selSex<br>";
if($txtScore < 50){
$getGrade = "D";
}elseif($txtScore < 65){
$getGrade = "C";
}elseif($txtScore < 80){
$getGrade = "B";
}else{
$getGrade = "A";}
print " $getGrade<br>";
echo "</h3></center>";
?>
23. 23
Introduction to HTML
Demo – frmInput02.htm
<html>
<head></head>
<body>
<form name="frmInput" method="post" action="getInfo02.php">
<table>
<tr>
<td align="right">Student Name:</td>
<td><input type=text name=txtName></td>
</tr>
<tr>
<td align="right">Student ID:</td>
<td><input type=text name=txtID></td>
</tr>
24. 24
Introduction to HTML
Demo – frmInput02.htm (
<tr>
<td align="right">Sex:</td>
<td>
<select name=selSex>
<option value=M> </option>
<option value=F> </option>
</select>
</td>
</tr>
<tr>
<td align="right">Score:</td>
<td><input type=text name=txtScore></td>
</tr>
<tr>
<td colspan="2" align="center"><input type=submit value=Submit></td>
</tr>
</table>
</form>
</body>
</html>
25. 25
Introduction to HTML
Demo – getInfo2.php
<? #getinfo02.php
echo "<center><h3>";
print "
<br>";
print " $txtName<br>";
print " $txtID<br>";
switch($selSex){
case "M“ :$selSex=“ "; break;
default :$selSex=“ ";break;
}
26. 26
Introduction to HTML
Demo – getInfo2.php (
print " $selSex<br>";
if($txtScore < 50)
$getGrade = "D";
elseif($txtScore < 65)
$getGrade = "C";
elseif($txtScore < 80)
$getGrade = "B";
else
$getGrade = "A";
print " $getGrade<br>";
echo "<h3></center>";
?>
30. 30
PHP
Demo – substrrp.php
<?php #substrrp.php
$text = "integer is number";
$newtext = substr($text, 0, 10);
print($newtext."<br>");
$newtext = substr_replace($newtext, " not float", 11);
print($newtext."<br>");
$newtext = substr_replace($text, " not float", 11);
print($newtext."<br>");
$newtext = substr_replace($text, " not float", 11,0);
print($newtext."<br>");
?>
34. 34
PHP
strpos()
Demo – strpos.php
<?php #strpos.php
$email = "chatchag@hotmail.com";
$name = substr($email, 0, strpos($email, "@"));
print("Name : ".$name."<br>");
$domain = substr($email, strpos($email, "@")+1, 11);
print("Domain : ".$domain."<br>");
?>
35. 35
PHP
DEMO - frminputstrpos2.htm
<!-- frminputstrpos2.htm -->
<html><!-- frminputstrpos2.htm-->
<head><title>Input form</title></head>
<body></body>
<form name="frmInput" method="post" action="strpos2.php">
E-mail :<input type=text name=txtEMail><br>
Year :<input type=text name=txtID><br>
<input type=submit value=Submit><br>
</form>
</html>
36. 36
PHP
strpos()
Demo – strpos2.php
<?php #strpos2.php
print "<h1>";
print “ 25".substr($txtID, 2, 2)."<br>";
print “ e-Mail : ";
print substr($txtEMail, 0, strpos($txtEMail, "@"));
print "<br>";
print "Domain : ".substr($txtEMail, strpos($txtEMail, "@")+1);
print "<br>";
print "</h1>";
?>
39. 39
PHP
strlen()
int strlen(string, string);
DEMO strlen()
<?php #strlen.php
$email = "chatchag@tot.co.th";
print("email length ".strlen($email)."<br>");
$name = substr($email, 0, strpos($email, "@"));
print("name length ".strlen($name)."<br>");
$domain = substr($email, strpos($email, "@")+1);
print("domain length ".strlen($domain)."<br>");
?>
41. 41
PHP
ltrim(), rtrim(), trim(), chop()
Demo
<?php #trim.php
$email = " chatchag@tot.co.th ";
print "all ".".".$email.".<br>";
print "trim ".".".trim($email).".<br>";
print "ltrim ".".".ltrim($email).".<br>";
print "rtrim ".".".rtrim($email).".<br>";
print "chop ".".".chop($email).".<br>";
?>
47. 47
PHP
implode()
Demo
<?php #implode.php
$email = "chatchag@tot.co.th";
list($name[], $domain) = explode("@", $email);
print $name[0]."<br>";
print $domain."<br>";
$name[] = "yahoo.com";
$newemail = implode("@", $name);
print $newemail."<br>";
?>
49. 49
PHP
strtolower(), strtoupper()
strtolower()
strtoupper()
Demo
<?php #strtou.php
$email = "chatchag@tot.co.th"."<br>";
print strtoupper($email);
$email = "CHATCHAG@TOT.CO.HT";
print strtolower($email);
?>
50. 50
PHP
ucfirst(), ucwords()
ucfirst()
ucwords()
Demo
<?php #ucwords.php
$email = "chatchag@ tot.co.th hotmail.com"."<br>";
print ucwords($email);
$email = "chatchag@ tot.co.th hotmail.com";
print ucfirst($email);
?>
51. 51
PHP
strcmp()
string 2
int strcmp(string str1, string str2)
str1, str2
Demo
<?php #strcmp php
$email1 chatchag@tot co th ;
$email2 chatchag@yahoo com ;
print strcmp $email1, $email1 <br> ;
print strcmp $email1, $email2 <br> ;
print strcmp $email2, $email1 <br> ;
?>
54. 54
PHP
Demo
<? //sprintf.php
$format = "chocolate 2 %s, is 129 %s. ";
$output = sprintf($format, "scoop(s)", "Baht");
print $output."<br>";
?>
<? //printf.php
$model = "AF-111";$unitprice = "25230.255";
$format = “ %s = %.2f ";
printf($format, $model, $unitprice)."<br>";
?>
55. 55
PHP
is_int(), is_integer()
integer
Demo
<? //isint.php
$i = 1.30;
#settype($i, "integer");
if(is_int($i))
print $i." is an integer.<br>";
else
print $i." is not an integer.<br>";
?>
61. 61
PHP
floor(), ceil(), round()
Demo
<?php #round.php
$num1 = 123.2563;
$num2 = 235.2566;
$avg = ($num1 + $num2)/2;
print $avg."<br>";
print round($avg,2)."<br>";
print round($avg,-1)."<br>";
print floor($avg)."<br>";
print ceil($avg)."<br>";
?>