จริงๆก่อนหน้านี้เคยแชร์เรื่องการ debug PHP กันไปแล้ว แต่มาคราวนี้จะมาใช้อีกตัวซึ่งง่ายกว่า นั่นคือการ debug PHP ด้วย dephpugger

1. Install composer

2. ติดตั้ง xDebug

3. ติดตั้ง dephpugger

4. ตรวจสอบว่า dephpugger สามารถทำงานได้ปกติ หรือขาดอะไรหรือไม่

5. ทดสอบการ debug ด้วยการสร้าง php file ขึ้นมา จากนั้นที่ส่วนไหนก็แล้วแต่ที่ต้องการ debug ให้ใส่ xdebug_break(); เข้าไปใน code ซึ่งเปรียบเสมือนกับการ break point ใน debugger ใดๆนั่นเอง

6. เปิด server สำหรับการ debug

ซึ่งจะเกิด Server ที่ทำการรัน php ไปแล้วขึ้นมาที่ port 8888 ซึ่งถ้ามันทำงานปกติ ก็จะแสดงผลของการทำงานปกติขึ้นมา หากเกิด error ใดๆก็จะแสดงขึ้นมา เช่นกัน

7. หากต้องการ debug ใช้เป็น

แล้วลองเข้า server <debug server>:8888 อีกที ก็จะเห็นที่ฝั่ง console ว่ามีการ debug เกิดขึ้น โดยคำสั่งใน dephpugger มีดังนี้

  • n -> To run a step over in code
  • s -> To run a step into in code
  • set <cmd>:<value> ->Change verboseMode or lineOffset in runtime
  • c -> To continue script until found another breakpoint or finish the code
  • l -> Show next lines in script
  • lp -> Show previous lines in script
  • helph -> Show help instructions
  • $variable -> Get a value from a variable
  • $variable = 33 -> Set a variable
  • my_function() -> Call a function
  • q -> Exit the debugger

Source:: HackerNoon