ใน PHP มีประเภทข้อมูลและนิพจน์ต่างๆ ที่ช่วยให้คุณสามารถทำงานกับข้อมูลและดำเนินการในการเขียนโปรแกรมของคุณได้ ชนิดข้อมูลทั่วไป ได้แก่ ตัวเลข สตริง บูลีน อาร์เรย์ และค่าว่าง คุณสามารถใช้เลขคณิต สตริง การกำหนด การเปรียบเทียบ และนิพจน์เชิงตรรกะเพื่อทำการคำนวณและตรวจสอบเงื่อนไข
ชนิดข้อมูลในภาษา PHP
สตริง:
คำอธิบาย: สตริงคือลำดับของอักขระที่อยู่ในอัญประกาศเดี่ยว('') หรืออัญประกาศคู่("")
ตัวอย่าง:
$name = "John Doe";
echo "Hello, ". $name; // Hello, John Doe
จำนวนเต็ม
คำอธิบาย: จำนวนเต็มคือจำนวนเต็มที่ไม่มีจุดทศนิยม
ตัวอย่าง:
$name = "John Doe";
echo "Hello, ". $name; // Hello, John Doe
ลอย
คำอธิบาย: ทศนิยมคือตัวเลขที่มีจุดทศนิยม
ตัวอย่าง:
$pi = 3.14;
echo "The value of pi is ". $pi; // The value of pi is 3.14
บูลีน
คำอธิบาย: บูลีนคือค่าที่เป็นจริงหรือเท็จ
ตัวอย่าง:
$isLogged = true;
if($isLogged) {
echo "User is logged in";
} else {
echo "User is not logged in";
}
อาร์เรย์
คำอธิบาย: อาร์เรย์คือชุดขององค์ประกอบต่างๆ
ตัวอย่าง:
$numbers = [1, 2, 3, 4, 5];
echo "The number of elements in the array is: ". count($numbers); // The number of elements in the array is: 5
วัตถุ
คำอธิบาย: วัตถุเป็นตัวอย่างของคลาสที่มีคุณสมบัติและเมธอด
ตัวอย่าง:
class Person {
public $name;
public function sayHello() {
echo "Hello, I am ". $this->name;
}
}
$person = new Person();
$person->name = "John";
$person->sayHello(); // Hello, I am John
โมฆะ
คำอธิบาย: NULL แสดงถึงตัวแปรที่ไม่มีการกำหนดค่า
ตัวอย่าง:
$value = null;
if($value === null) {
echo "The value of the variable is NULL";
}
ทรัพยากร
คำอธิบาย: ทรัพยากรแสดงถึงทรัพยากรภายนอก เช่น การเชื่อมต่อฐานข้อมูล
ตัวอย่าง:
$dbConnection = mysqli_connect("localhost", "username", "password", "database");
// Use $dbConnection to query the database
นิพจน์ในภาษา PHP
นิพจน์ทางคณิตศาสตร์
คำอธิบาย: นิพจน์ทางคณิตศาสตร์ดำเนินการทางคณิตศาสตร์
ตัวอย่าง:
$sum = 5 + 3; // Addition
$difference = 10- 4; // Subtraction
$product = 2 * 6; // Multiplication
$quotient = 12 / 3; // Division
$remainder = 10 % 3; // Modulo(Remainder)
นิพจน์การมอบหมายงาน
คำอธิบาย: นิพจน์การกำหนดกำหนดค่าให้กับตัวแปร
ตัวอย่าง:
$x = 5; // Assigning 5 to $x
$y += 10; // Incrementing $y by 10
$z -= 3; // Decrementing $z by 3
นิพจน์เปรียบเทียบ
คำอธิบาย: นิพจน์การเปรียบเทียบจะเปรียบเทียบค่าสองค่าและส่งกลับผลลัพธ์บูลีน
ตัวอย่าง:
$x = 5;
$y = 10;
$isEqual = $x == $y; // Equality comparison
$isNotEqual = $x != $y; // Inequality comparison
$isGreater = $x > $y; // Greater than comparison
$isLess = $x < $y; // Less than comparison
นิพจน์เชิงตรรกะ
Miêu tả: Biểu thức ตรรกะ kết hợp nhiều điều kiện và trả về kết quả บูลีน
ตัวอย่าง:
Description: Logical expressions combine multiple conditions and return a boolean result.
การต่อสตริง
คำอธิบาย: การต่อสายอักขระเป็นการรวมสายอักขระหลายสายเข้าเป็นสายเดียว
ตัวอย่าง:
$name = "John";
$message = "Hello, ". $name. "!"; // Concatenating strings
นิพจน์ที่ประกอบไปด้วย
$age = 20;
$message =($age >= 18) ? "You are an adult": "You are not an adult";
นี่คือนิพจน์ทั่วไปบางประเภทใน PHP การทำความเข้าใจและใช้ประโยชน์จากนิพจน์เหล่านี้มีความสำคัญอย่างยิ่งต่อการคำนวณ การตัดสินใจ และการจัดการข้อมูลในแอปพลิเคชัน PHP



