Code PHP ที่แนะนำทั้งหมดในนี้ ไม่ได้ Copy มาจากแหล่งอื่นทั้งหมดเป็นการเขียนของเจ้าของ Blog เอง และไม่ได้เรียงลำดับเรืองของบทความ ให้ผู้ศึกษาทำความเข้าใจและลำดับเอาเองนะครับ
แนะนำโครงสร้าง php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo 1; ?>
<? echo 2; ?>
<script language="php">echo 3;</script>
<% echo 4; %>
</body>
</html>
==============================================
Loop for
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
for ($i=1; $i<=10; $i++){
echo "$i
";
}
?>
</body>
</html>
==============================================
PHP MUL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
for($j=2;$j<=12;$j++)
{
?>
<table width="150" border="1" cellspacing="1">
<tr>
<td colspan="2" align="center"><?php echo $j ?> </td>
</tr>
<?php
for($i=1;$i<=12;$i++)
{
?>
<tr>
<td align="center"><?php echo $i ?> </td>
<td align="center"><?php echo ($i*$j) ?> </td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>
==============================================
PHP-SCORE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$sc = 95;
if($sc > 100 || $sc < 0)
{
exit("Please check score");
}
if($sc < 50)
echo "Grade F";
elseif($sc >= 50 && $sc < 60)
echo "Grade D";
elseif($sc >= 60 && $sc < 70)
echo "Grade C";
elseif($sc >= 70 && $sc < 80)
echo "Grade B";
elseif($sc >= 80)
echo "Grade A";
?>
</body>
</html>
==============================================
PHP-Switch
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$a = 5;
switch($a)
{
case 1: echo "One"; break;
case 2: echo "Two"; break;
case 3: echo "Three"; break;
default: echo "Can't select";break;
}
?>
</body>
</html>
==============================================
PHP-VAR
ตัวแปร
1. ขึ้นต้นด้วยเครื่้องหมาย $
2. ชื่อห้ามมีเครื่องหมายทางคณิตศาสตร์ ช่องว่าง
3. ชื่อห้ามขึ้นต้นด้วยตัวเลข
4. ชื่อตัวแปรพิมพ์เล็ก พิมพ์ใหญ่ต่างกัน
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$var['a'] = 10;
$var['b'] = 20;
$var['c'] = 30;
//print_r($var);
echo $var['c'];
echo "<hr>";
$profile['name'] = 'Thisan';
$profile['nickname']= 'Nott';
$profile['email'] = 'nott@netdesign.ac.th';
echo $profile['email'];
?>
</body>
</html>
==============================================
PHP-Printf
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$id = 2;
echo sprintf("%05d",$id);
?>
</body>
</html>
==============================================
PHP-birdth day select
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$month
=
array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กร
กฏาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกาคม","ธันวาคม");
$yearnow = date("Y");
?>
<form id="form1" name="form1" method="post" action="">
<label for="day"></label>
Birthday :
<select name="day" id="day">
<?php for ($i=1; $i<=31; $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>
<select name="month" id="month">
<?php for ($i=1; $i<=sizeof($month); $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $month[$i-1] ?></option>
<?php } ?>
</select>
<select name="year" id="year">
<?php for ($i=$yearnow; $i>=$yearnow-20; $i--){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>
</form>
<?php
date
?>
</body>
</html>
==============================================
การส่งคาและการรับค่าแบบ Post
การส่งค่าแบบ Post
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="post2.php">
<input type="text" name="a" id="a" />
<input type="text" name="b" id="b" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
การรับค่าแบบ Post
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo $_POST['a'];
echo "
";
echo $_POST['b'];
?>
</body>
</html>
==============================================
การส่งค่าและการรับค่าแบบ GET
การส่งค่าแบบ GET
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<a href="get2.php?a=10&b=20">Link[/url]
</body>
</html>
การรรับค่าแบบ GET
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo $_GET['a'];
echo "
";
echo $_GET['b'];
?>
</body>
</html>
==============================================
PHP-Syntex
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$a = 22;
$b = 3;
echo "$a + $b=".($a + $b)."
";
echo "$a - $b=".($a - $b)."
";
echo "$a * $b=".($a * $b)."
";
echo "$a / $b=".($a / $b)."
";
echo "$a % $b=".($a % $b)."
";
?>
</body>
</html>
==============================================
PHP-if
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$a = 5;
if($a > 10)
{
echo "$a > 10";
}elseif($a == 10)
{
echo "$a = 10";
}elseif($a < 10)
{
echo "$a < 10";
}
?>
</body>
</html>
==============================================
PHP-Bank
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$money = 10000;
$interest = 3/100;
$year = 5;
for ($i=1; $i<=$year; $i++){
$money = ($money*$interest)+$money;
echo "$i year : $money
";
}
?>
</body>
</html>
==============================================
PHP-array
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$score1 = 20;
$score2 = 40;
$score3 = 60;
$score4 = 27;
$score5 = 88;
// $score = array(20,40,60,27,88);
// $score = array(1=>40,4=>88);
// $score = array(
// $score[2][3] = 40;
$score = array(0=>array(20,40,60,27,88),
1=>array(A,B,C,D,E));
// echo "$score[2]";
print_r($score);
echo $score[1][2];
?>
</body>
</html>
แนะนำเว็บสอน PHP
www.phpeveryday.com
ไม่มีความคิดเห็น:
แสดงความคิดเห็น