พื้นฐานการเขียนภาษา PHP

ผู้เรียนสามารถแทรกภาษา PHP ในเอกสาร     HTML ได้ โดยจะเริ่มต้นด้วยแท็ก เปิด และ ปิด ดังนี้ 

<?php
// แทรกโค้ดภาษา PHP บริเวณนี้
?>

นามสกุลของการเขียนภาษา PHP คือ ".php"

ไฟล์ PHP ปกติจะประกอบด้วยโค้ดภาษา HTML และ PHP ปนๆกันในไฟล์เดียว 

ตัวอย่างไฟล์ ที่ผู้เรียนจะเริ่มต้นเขียน ง่ายๆ ดังนี้ 

โดยจะมีรูปแบบการแสดงผลข้อมูลง่ายโดยใช้คำสั่ง "echo" ตามด้วยข้อความที่ต้องการแสดง 

<!DOCTYPE html>
<html>
<body>

<h1>หน้าแรกการฝึกเขียนภาษา PHP</h1>

<?php
echo "Hello World!";
?>


</body>
</html>


ในภาษา PHP คำสำคัญหรือ keywords (เช่น ifelsewhileecho, etc.), classes, functions, and user-defined functions are not case-sensitive. 

จะไม่มีผลตัวพิมพ์ใหญ่ หรือตัวพิมพ์เล็ก สามารถใช้ได้ทั้งสองแบบ และแสดงผลเหมือนกัน เช่น

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

แต่ถ้าเป็นการกำหนดตัวแปร ให้ระวัง Case-Sensitive เพราะตัวใหญ่และตัวเล็กมีผลที่แตกต่างกันเช่น 

<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>


ตัวแปร$color จะมีผลทันที จึงต้องระวังเวลากำหนดชื่อของตัวแปร 

การเขียน Comments in PHP

ในส่วนนี้จะไม่มีผลต่อการทำงานของโค้ดภาษา แค่ต้องการอธิบายหรือกำกับข้อความ เวลามาดูหรือทบทวนทีหลัง โดยจะเขียนได้ 2 ลักษณะคือ 

เขียนแค่ 1 บรรทัด

<?php
// This is a single-line comment

# This is also a single-line comment
?>

และเขียนแบบหลายบรรทัด 

<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

?>

การประกาศค่าตัวแปร Creating (Declaring) PHP Variables

ตัวแปร เทียบได้กับกล่องหรือภาชนะ ที่ใส่หรือเก็บข้อมูล ข้อความหรือตัวเลข สำหรับภาษา PHP จะประกาศค่าตัวแปรด้วย $และชื่อตัวแปร เช่น

<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

การแสดงผลค่าตัวแปร Output variables จะเรียกใช้คำสั่ง echo และเครื่องหมายคำพูด 

<?php
$txt = "thaifreewaredownload.com";
echo "I love $txt!";
?>

การประกาศค่าตัวแปรภาษา PHP สามารถประกาศ ณ จุดใดก็ได้ โดยแบ่งออก 3 แบบด้วยกันคือ 

  • local
  • global
  • static
สำหรับ global กับ local จะแตกต่างกันที่การประกาศใน Function หรือนอก Fucntion เช่น
<?php
$x = 5// global scope

function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

echo "<p>Variable x outside function is: $x</p>";
?>
ผลการทดสอบจะพบว่า ตัวแปร x อยู่นอกฟังก์ชันจะได้คำตอบที่ถูกต้อง 

สำหรับ local จะประกาศภายในฟังกชัน ค่าที่ได้ก็ต้องดูว่าแบบไหนที่จะแสดงผลถูกต้อง
<?php
function myTest() {
  $x = 5// local scope
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

บางครั้งที่เราประกาศค่าตัวแปร และใช้งานผลการประกาศค่าตัวแปรแล้ว ปกติจะถูกลบออกหรือหายไป แต่ถ้าเราต้องการจะใช้งานตัวแปรต่อ จึงใช้แบบ static หรือค่าคงที 
<?php
function myTest() {
  static $x = 0;
  echo $x;
  $x++;
}

myTest();
myTest();
myTest();
?>
ชนิดของข้อมูล PHP 

PHP Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
ชนิดข้อมูลแบบข้อความหรือ string 
<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>
กำหนดตัวแปร เพื่อแทนที่ข้อความ และ echo ออกมาแสดงผลข้อความ สามารถเขียนในเครื่องหมายคำพูด 'single quote' และ "double quote" ก็ได้
ชนิดข้อมูลแบบตัวเลข จำนวนเต็ม integer เป็นจำนวนเต็มบวก หรือเต็มลบ ห้ามเป็นจุดทศนิยม เช่น 
<?php
$x = 5985;
var_dump($x);
?>

โดยจะใช้คำสั่ง var_dump(ชื่อตัวแปร); เพื่อแสดงผล output ข้อมูลออกมา

------ ชนิดข้อมูล Float แสดงข้อมูลจำนวนทศนิยม หรือเลขยกกำลัง ---------
<?php
$x = 10.365;
var_dump($x);
?>

----- ชนิดข้อมูลแบบ boolean --------------
แสดงข้อมูล 2 ค่า คือ จริงกับเท็จ 
$x = true;
$y = false;

------- array --------------เก็บข้อมูลหลายค่าใน 1 ตัวแปร 

An array stores multiple values in one single variable.

In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

เช่นประกาศค่าตัวแปรชื่อ $cars ซึ่งภายในมีข้อมูลของรถ หลายยี่ห้อ เป็นต้น 

<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>

---------- ชนิดข้อมูลวัตถุหรือ object --------------

Classes and objects are the two main aspects of object-oriented programming.

A class is a template for objects, and an object is an instance of a class.

เมื่อ class และ object มีความสัมพันธ์กันที่เชื่อมโยงกัน มองว่า classes คือรถยนต์ car ซึ่งจะมี objects หรือส่วนประกอบหลายๆ ส่วนขึ้นมาเป็นรถยนต์ เช่น รุ่น สี เป็นต้น และผู้ใช้งานสามารถกำหนดค่าตัวแปร เพื่อเก็บข้อมูลเหล่านี้ 

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Let's assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

ตัวอย่างการเขียนโค้ด PHP ในรูปแบบข้อมูล class และ object

<?php
class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}

$myCar new Car("black""Volvo");
echo $myCar -> message();
echo "<br>";
$myCar new Car("red""Toyota");
echo $myCar -> message();
?>

-----------ชนิดข้อมูล NULL --------------

Null is a special data type which can have only one value: NULL.

ค่าว่าง สำหรับใช้งานที่ต้องการค่าว่าง 

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>


-----------------การจัดการข้อมูลที่เป็นข้อความ string function

ที่สำคัญและพบบ่อย 

คำสั่งภาษา PHP strlen() function returns the length of a string. 

เพื่อนับจำนวนอักษรข้อความ 

<?php
echo strlen("Hello world!"); // outputs 12
?>

PHP str_word_count() นับจำนวนคำของข้อความ

<?php
echo str_word_count("Hello world!"); // outputs 2
?>

PHP strrev() กลับทิศทางของข้อความ 

<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>

PHP strpos() ค้นหาข้อความตามที่ระบุ 

<?php
echo strpos("Hello world!""world"); // outputs 6
?>

Tip: The first character position in a string is 0 (not 1)

str_replace() แทนที่ข้อความใน string เช่น

<?php
echo str_replace("world""Dolly""Hello world!"); // outputs Hello Dolly!
?>

แทนที่คำว่า world ด้วย Dolly

----------------------------

ชนิดข้อมูลจำนวน Number

PHP จะมีคำสั่งในการตรวจสอบข้อมูลจำนวนและตัวเลข 

  • is_int()
  • is_integer() - alias of is_int()
  • is_long() - alias of is_int()
<?php
$x = 5985;
var_dump(is_int($x));

$x = 59.85;
var_dump(is_int($x));
?>

PHP จะมีคำสั่งในการตรวจสอบชนิดข้อมูล ทศนิยมและเลขยกกำลัง  float:

  • is_float()
  • is_double() - alias of is_float()
<?php
$x = 10.365;
var_dump(is_float($x));
?>

PHP มีคำสั่งตรวจสอบจำนวนจำกัด หรือไม่จำกัด  finite or infinite:
  • is_finite()
  • is_infinite()
<?php
$x = 1.9e411;
var_dump($x);
?>

----------------

PHP มีคำสั่งตรวจสอบว่าไม่ใช่จำนวน  is not a number:

  • is_nan()
<?php
$x = acos(8);
var_dump($x);
?>
----------------
PHP is_numeric() สำหรับตรวจสอบว่าเป็นจำนวนตัวเลข
<?php
$x = 5985;
var_dump(is_numeric($x));

$x = "5985";
var_dump(is_numeric($x));

$x = "59.85" + 100;
var_dump(is_numeric($x));

$x = "Hello";
var_dump(is_numeric($x));
?>
----------------------------
The (int), (integer), or intval() สำหรับแปลงจำนวนทศนิยมและข้อความให้เป็นตัวเลข 
<?php
// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;

echo "<br>";

// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;
?>
-------------------------
สูตรทางคณิตกับ PHP
หาค่าประมาณของค่าพาย PI
<?php
echo(pi()); // returns 3.1415926535898
?>
---------------
The min() and max() สำหรับหาค่าต่ำสุดสูงสุด
<?php
echo(min(01503020, -8, -200));  // returns -200
echo(max(01503020, -8, -200));  // returns 150
?>
--------------------
The abs() function หาค่าสัมบูรณ์ของจำนวน เช่น 
<?php
echo(abs(-6.7));  // returns 6.7
?>
----------------------
The sqrt() function หาค่ารากที่สองของจำนวน เช่น 
<?php
echo(sqrt(64));  // returns 8
?>
----------------------
The round() function สำหรับปัดเศษทศนิยม เป็นจำนวนเต็ม
<?php
echo(round(0.60));  // returns 1
echo(round(0.49));  // returns 0
?>
------------------------
The rand() function สำหรับสุ่มจำนวน
<?php
echo(rand());
?>
สุ่มแบบระบุขั้นต่ำ และค่าสูงสุด
<?php
echo(rand(10100));
?>

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

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