วันอาทิตย์ที่ 23 มกราคม พ.ศ. 2554

ความรู้พื้นฐานเกี่ยวกับภาษา PHP

รูปแบบคำสั่ง  (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"

อาร์เรย์  (Arrays)
o       อาร์เรย์ คือ ตัวแปรชนิดหนึ่งที่สามารถเก็บค่าได้หลายค่าในเวลาเดียวกัน
§        การสร้างอาร์เรย์ให้เรียกใช้ฟังก์ชั่น array()
§        อาร์เรย์จะถูกชี้ตัวแหน่งโดยคีย์
§        การสร้างอาร์เรย์:
$shoppingList = array( 1 => "toothpaste", 2 => "sun cream", 3 => "band-aids");
§        การแสดงค่าจากอาร์เรย์:
echo "The third item in the shopping list is $shoppingList[3];"
ผลลัพธ์:           "The third item in the shopping list is band-aids"


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

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