วันพฤหัสบดีที่ 27 พฤศจิกายน พ.ศ. 2557

โครงสร้างของภาษาPHP

     การเปิดปิดแท็ก PHP  (PHP Code Syntax)

รูปแบบแท็ก
เปิดแท็ก PHP
ปิดแท็ก PHP
แบบมาตรฐาน
<?php
?>
แบบสั้น
<?
?>
แบบ ASP
<%
%>
แบบ Script
<script  language="PHP">
</script>

           รูปแบบคำสั่ง  (PHP Statement)
<HTML>
<BODY>
  <?php
    echo "Hello, World!!”;
   ?>
</BODY>
</HTML>

           ตัวแปร  (Variables)
o      การประกาศตัวแปร
§       การประกาศตัวแปรเริ่มต้นด้วยเครื่องหมาย  $  (Dollar sign)
§       ชื่อตัวแปรต้องเริ่มต้นด้วยตัวอักษรภาษาอังกฤษหรือเครื่องหมายขีดล่าง (underscore "_")
§        ตัวอย่างการประกาศตัวแปรที่ถูกต้อง:
$total
$_cell1
$length_of_string
§        ตัวอย่างการประกาศตัวแปรที่ผิด:                      
total
$1_total
$2_length
o      การกำหนดค่าให้ตัวแปร
§       กำหนดค่าเป็นตัวเลข:
<?php
$total = 10;
?>
§       การกำหนดค่าเป็นข้อความ (string) ให้ใช้ quotes (") หรือ single quote ('):
<?php
$example1 = 'This is a single quoted string';
$example2 = "This is a double quoted string";
?>
§       ข้อแตกต่างระหว่าง quotes (") กับ single quote ('):
<php
$total = 10; 
$example1 = 'The total is $total';
$example2 = "The total is $total";
?>
ผลการกำหนดค่าให้ตัวแปร $example1:     "The total is $total"
ผลการกำหนดค่าให้ตัวแปร
 $example2:     "The total is 10"
§       การนำข้อความ (string) มาเชื่อมต่อกันโดยใช้จุด "." :
<php
$a = 'apples';
$b = 'bananas';
$c = '';                    
$c = $a . ' and ' . $b;
?>
ผลการกำหนดค่าให้ตัวแปร  $c:      "apples and bananas"
§       การนำข้อความ (string) มาเชื่อมต่อกันโดยใช้ ".=" :
<php
$a = 'apples';                      
$a .= ' and bananas';
?>

ผลการกำหนดค่าให้ตัวแปร  $a:      "apples and bananas"

อักขระต้องห้าม  (Escaping Characters)

Character
Escaped Character
Description
ไม่มี
\n
Adds a linefeed
ไม่มี
\r
Adds a carriage return
ไม่มี
\t
Adds a tab
\
\\
Backslash
$
\$
Dollar Sign
"
\"
Double Quote

 ตัวดำเนินการ  (Operators)

Operator
ความหมาย
==
เท่ากับ (Equal to)
!=
ไม่เท่ากับ (Not equal to)
<> 
ไม่เท่ากับ (Not equal to)
น้อยกว่า (Less than)
มากกว่า (Greater than)
<=
น้อยกว่าหรือเท่ากับ (Less than or equal to)
>=
มากกว่าหรือเท่ากับ (Greater than or equal to)


           ภาษา PHP มีลักษณะเป็น embedded script หมายความว่าเราสามารถฝังคำสั่ง PHP ไว้ในเว็บเพจร่วมกับคำสั่ง(Tag) ของ HTML ได้ และสร้างไฟล์ที่มีนามสกุลเป็น .php, .php3 หรือ .php4 ซึ่งไวยากรณ์ที่ใช้ใน PHP เป็นการนำรูปแบบของภาษาต่างๆ มารวมกันได้แก่ C, Perl และ Java ทำให้ผู้ใช้ที่มีพื้นฐานของภาษาเหล่านี้อยู่แล้วสามารถศึกษา และใช้งานภาษานี้ได้ไม่ยาก

ตัวอย่างที่ 1
1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>Example 1 </title>
</head>
<body>

<?
   echo"Hi, I'm a PHP script!";
?>

</body>
</html>

            จากตัวอย่าง บรรทัดที่ 6 - 8 เป็นส่วนของสคริปต์ PHP ซึ่งเริ่มต้นด้วย <? ตามด้วยคำสั่งที่เรียกฟังก์ชั่นหรือข้อความ และปิดท้ายด้วย ?> สำหรับตัวอย่างนี้เป็นสคริปต์ที่แสดงข้อความว่า "Hi, I'm a PHP script" โดยใช้คำสั่ง echo ซึ่งเป็นคำสั่งที่ใช้ในการแสดงผลของภาษาสคริปต์ PHP ซึ่งจะแสดงผลดังนี้
Hi, I'm a PHP script!

ตัวอย่างที่2
เราสามารถฝังคำสั่ง PHP ไว้ในเว็บเพจหนึ่งๆ โดยเปิดและปิดด้วยแท็ก(Tag) ของ PHP กี่ครั้งก็ได้ ดังตัวอย่างต่อไปนี้


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<head>
<title>Example 1 </title>
</head>
<body>

<table border=1>
<tr>
<td>
<? echo"PHP script block 1"; ?></td>
<td>
<? echo"PHP script block 2 "; ?></td>
</tr>
</table>

<?
   echo"PHP script block 3 <br> ";
   echo date("ขณะนี้เวลา H:i น.");
?>

</body>
</html>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น