A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Note: PHP statements end with a semicolon (;).
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
Syntax for single-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
?>
</body>
</html>
Syntax for multiple-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
that spans over multiple lines
*/
?>
</body>
</html>
Using comments to leave out parts of the code:
<!DOCTYPE html>
<html>
<body>
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
ให้นักเรียน แสดงประวัตินักเรียน โดยใช้ภาษา PHP โดยให้มีข้อมูลดังนี้
รหัสนักเรียน : 99999
ชื่อ-นามสกุล : นายโยธิน ศิริเอ้ย
ชื่อเล่น : โย
อีเมล : yotin.sir@thoengwit.ac.th
ให้ทำการ Comments บรรทัดที่แสดง ชื่อเล่น : โย
ให้ทำการ Comments บรรทัดที่แสดง ชื่อ-นามสกุล : นายโยธิน ศิริเอ้ย และ ชื่อเล่น : โย
1. ไม่ปฏิบัติกิจกรรมใดเลย 0 คะแนน
2. ปฏิบัติโดยให้ได้รับการช่วยเหลือจากครูผู้สอน 5 คะแนน
3. ปฏิบัติโดยได้รับการช่วยเหลือจากเพื่อน 8 คะแนน
4. ปฏิบัติด้วยตนเอง 10 คะแนน