Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP
PHP
  1. รีเซ็ต Key ของ Array ใน PHP (array_values)

    ใช้ array_values() เพื่อ Re-index Array ให้เป็น Numeric Index เรียง 0, 1, 2... ไม่ว่าจะเป็น Associative Array หรือ Array ที่มี Key ช่วงว่าง ตัวอย่างพื้นฐาน string(3) 110 } */ กรณี Key เป็นตัวเลขแต่ไม่เรียง/มีช่องว่าง string(5) Hanks } */ หลัง unset() ต้อง array_values() เสมอ d d e } */ ฟังก

  2. สร้าง Array วันที่ระหว่างสองวันที่ใน PHP

    ฟังก์ชันสร้าง Array ของวันที่ทุกวันระหว่าง Start Date และ End Date ฟังก์ชันมาตรฐาน (DateTime - แนะนำ PHP 5.2+) format($format); } return $dates; } // ใช้งาน $dates = getDateRange(2024-01-01, 2024-01-10); // [2024-01-01, 2024-01-02, ..., 2024-01-10] $datesThai = getDateRange(2024-01-01

  3. ความแตกต่าง echo, print, print_r ใน PHP

    ทั้งสามใช้แสดง Output แต่มีจุดประสงค์และพฤติกรรมต่างกัน echo - Language Construct (ไม่ใช่ Function) ไม่มี Return Value (void) รับ Parameter หลายตัวได้: echo a, b, c; เร็วกว่า print เล็กน้อย ไม่สามารถใช้ใน Expression ได้ echo Hello, , World; // Hello World // echo Hello . . World

  4. ความแตกต่าง array_merge และ array + array (Union) ใน PHP

    ทั้งคู่รวม Array แต่พฤติกรรมต่างกันกับ Key ที่ซ้ำ (Duplicate Keys) array_merge() - รวมและ Re-index Numeric Keys String Key ซ้ำ: Value ตัวหลัง Overwrite ตัวก่อน Numeric Key: Re-index ใหม่ 0,1,2... (ไม่ Overwrite) 20 first second <-- Re-indexed to 1 ) */ Union Operator (+) -

  5. ตั้งค่า Encoding ใน PHP FPDI Library

    FPDI (Free PDF Document Importer) ใช้ร่วมกับ FPDF/TCPDF ต้องการ Font ที่รองรับ Encoding ที่ต้องการ (Thai, Unicode, आदि) ปัญหา: FPDF Core ใช้ Built-in Fonts (Latin-1 เท่านั้น) Built-in Fonts (Helvetica, Times, Courier) รองรับเฉพาะ CP1252/ISO-8859-1 ไม่รองรับ Thai, Chinese, Arabic, Emoji วิธีแก้: เพิ่

  6. สร้าง Anonymous Object ใน PHP (Anonymous Classes PHP 7+)

    ตั้งแต่ PHP 7.0 สามารถสร้าง Class และ Object แบบไม่มีชื่อ (Anonymous Class) ได้เหมือน Lambda Function Syntax พื้นฐาน $object = new class($constructorArgs) extends ParentClass implements Interface { // Properties public $property = value; // Methods public function method() {

  7. Debug และ Log PHP OPcache Issues

    OPcache ปรับปรุง Performance โดย Cache Compiled Bytecode แต่บางครั้งก่อให้เกิด Bug ที่ยากต่อการ Debug (Stale Cache, Corruption, Incompatibility) 1. ปิด OPcache ชั่วคราว (Quick Test)

  8. ตั้งค่า PHPUnit Testing ใน PHPStorm

    คู่มือตั้งค่า PHPUnit Testing Framework ใน PhpStorm IDE แบบ Step-by-Step ข้อกำหนดเบื้องต้น PhpStorm (Professional Edition หรือ Community + Plugin) CLI Interpreter) Composer ติดตั้งแล้วใน Project ขั้นตอนที่ 1: ติดตั้ง PHPUnit ผ่าน Composer # ใน Root ของ Project composer require --dev p

  9. รวม filter_input() Flags ด้วย AND/OR ใน PHP

    filter_input() รองรับ Flags ผ่าน Bitwise OR (|) ไม่ใช่ Logical AND/OR ทั่วไป Flags ที่ใช้ร่วมกันได้ (Bitwise OR) $value = filter_input(INPUT_POST, field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY | FILTER_NULL_ON_FAILURE ); Flags หลักของ filter_input Flagคำอธิบาย FILT

  10. ลบ Space ทั้งหมดออกจาก String ใน PHP

    หลายวิธีในการลบ Whitespace (Space, Tab, Newline) ออกจาก String ใน PHP เลือกตามความต้องการ 1. ลบ Space แค่ Space Character (ASCII 32) ]); // thisisateststring 2. ลบ Whitespace ทั้งหมด (Space, Tab, Newline, Carriage Return) $str = this\tis\na test\rstring; // preg_replace - ใช้ Regex \s (Whitespace

  11. json_encode vs serialize: วิธีเก็บ PHP Array ไหนดีกว่า?

    ทั้ง json_encode และ serialize ใช้เก็บ PHP Array/Object ลง String (Database, File, Cache, Session) แต่มีคุณสมบัติต่างกัน สรุปความแตกต่าง Featurejson_encodeserialize FormatJSON (Standard, Human-readable)PHP Serialized String (Binary-safe) Portabilityสูงมาก (ทุก

  12. จับผลลัพธ์ var_dump ไปเก็บใน String (Output Buffering)

    var_dump() พิมพ์ Output โดยตรง ไม่คืนค่า String ใช้ Output Buffering เพื่อจับ Output ไปใส่ใน Variable ฟังก์ชันมาตรฐาน string(5) third } */ ฟังก์ชันขยาย: รองรับหลาย Variable + Label treat as label for next? No, keep simple. } var_dump($var); } return ob_get_clean(); } //

  13. ดึง YouTube Video ID จาก URL ด้วย PHP (parse_url + parse_str)

    วิธีที่น่าเชื่อถือที่สุดคือใช้ parse_url() และ parse_str() แทน Regex เพราะ URL ของ YouTube มีรูปแบบหลายแบบ รูปแบบ YouTube URL ที่พบบ่อย https://www.youtube.com/watch?v=VIDEO_ID https://youtu.be/VIDEO_ID https://www.youtube.com/embed/VIDEO_ID https://www.youtube.com/v/VIDEO_ID ht

  14. ดึง Subdirectories ทั้งหมดของโฟลเดอร์ใน PHP

    หลายวิธีในการดึงรายการ Subdirectory ของโฟลเดอร์ใน PHP เลือกใช้ตาม Version และความต้องการ วิธีที่ 1: glob() + GLOB_ONLYDIR (แนะนำ, เรียบง่าย) /var/www/html/images ) */ วิธีที่ 2: scandir() + is_dir() (รองรับทุก Version, ควบคุมได้ละเอียด) function getSubdirectories($path) { if (!is_dir($path)) re

  15. ตรวจสอบ Property ใน Object/Class ด้วย PHP (property_exists vs isset)

    PHP มี 2 วิธีหลักตรวจสอบ Property: property_exists() และ isset() พฤติกรรมต่างกันสำคัญ property_exists() ตรวจสอบว่า Property มีอยู่จริงใน Class/Object รวมถึง Private/Protected/Static property_exists(mixed $class, string $property): bool // $class = Class Name (string) หรือ Object Instance // คืนค่า

  16. ประสิทธิภาพ FOR vs FOREACH ใน PHP

    เปรียบเทียบประสิทธิภาพของ for loop และ foreach loop ใน PHP รวมถึงเทคนิค Optimization ความแตกต่างพื้นฐาน for loop: ทำงานกับ Index โดยตรง เข้าถึง Element ผ่าน $arr[$i] foreach loop: ทำงานกับ Iterator ภายใน PHP คัดลอก Array (Copy-on-Write) สำหรับ Value Benchmark: Loop 10,000 รอบ แก้ไขค่า &$v

  17. วิธี Parse CSV File ด้วย PHP

    เพื่อ parse CSV file ใน PHP โค้ดมีดังนี้ ใน fopen() ให้ตั้งค่า path ของ .csv file −ตัวอย่าง$row_count = 1; if (($infile = fopen(path to .csv file, r)) !== FALSE) { while (($data_in_csv = fgetcsv($infile, 800, ,)) !== FALSE) { $data_count = count($data_in_csv); echo $data_cou

  18. ความแตกต่าง Ternary Operator และ Null Coalescing Operator ใน PHP

    ทั้งสอง Operator ใช้สำหรับ Conditional Assignment แต่มีวัตถุประสงค์และพฤติกรรมต่างกัน Ternary Operator (?:) รูปแบบ: (condition) ? expr1 : expr2 = 80) ? B : C); Null Coalescing Operator (??) - PHP 7.0+ รูปแบบ: (variable) ?? fallback คืนค่า Variable ถ้า ไม่ใช่ null มิฉะนั้นคืนค่า Fallback Guest // C

  19. ความแตกต่างระหว่าง !== และ ==! ใน PHP

    ตัวดำเนินการ !== และ ==! มีความหมายต่างกันอย่างสิ้นเชิง !== - Not Identical (Strict Inequality) เปรียบเทียบ ทั้งค่าและชนิดข้อมูล (Type) คืนค่า true ถ้าค่าต่างกัน หรือ ชนิดข้อมูลต่างกัน 1 !== 1; // false (ค่าเท่ากัน, ชนิดเท่ากัน int) 1 !== 1; // true (ค่าเท่ากัน แต่ชนิดต่างกัน: int vs string)

  20. ความแตกต่างระหว่าง | และ || ใน PHP (Bitwise OR vs Logical OR)

    ตัวดำเนินการ | และ || ทั้งคู่เป็น OR แต่ทำงานในระดับต่างกัน | - Bitwise OR (ระดับ Bit) เปรียบเทียบ Bit แต่ละตำแหน่ง หาก Bit ใด Bit หนึ่งเป็น 1 ผลลัพธ์จะเป็น 1 $x = 1; // Binary: 0001 $y = 2; // Binary: 0010 $result = $x | $y; // 0011 = 3 echo $result; // 3 || - Logical OR (ระดับ Boolean) ประเมิน

Total 1286 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:37/65  20-คอมพิวเตอร์/Page Goto:1 31 32 33 34 35 36 37 38 39 40 41 42 43