Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP ParseError

บทนำ

คลาส ParseError extends คลาส CompileError (ก่อนหน้านี้เป็น subclass ของ Error) ข้อผิดพลาดประเภทนี้ถูกโยนขณะที่ PHP code ภายในสตริงที่ส่งให้ฟังก์ชัน eval() เป็น argument

ฟังก์ชัน eval() ประเมินสตริงที่ให้มาเป็น PHP code

ไวยากรณ์

eval ( string $code ) : mixed

Parameters

Sr.NoParameter & Description
1code
valid PHP code ที่จะประเมิน

Code ที่จะประเมินต้องไม่ฝังใน PHP opening/closing tags และต้องจบด้วย semicolon Valid code คืนค่า NULL ในขณะที่ error ใน code โยน ParseError

ตัวอย่างต่อไปนี้โยน ParseError และจับได้โดย catch block

ตัวอย่าง

<?php $a=10; try{ eval('$a=$a+;'); } catch (ParseError $e){ echo "Parse Error:" . $e->getMessage(); } ?>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้

Parse Error:syntax error, unexpected ';'