8 Contoh Coding PHP dan HTML
Bahasa pemrograman ini cukup populer di Indonesia. Bahasa pemrograman ini digunakan untuk pengaturan server pada sebuah aplikasi. Untuk mempelajari bahasa pemrograman PHP kita membutuhkan localhost contohnya XAMPP, Text Editor, dan Browser.
1. Echo/ Print
<!DOCTYPE html> <html> <body> <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?> </body> </html>
Hasilnya :
2. Tipe data
<!DOCTYPE html> <html> <body> <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?> </body> </html>
Hasilnya :
3. Variabel
<!DOCTYPE html> <html> <body> <?php $txt = "W3Schools.com"; echo "I love " . $txt . "!"; ?> </body> </html>
Hasilnya :
4. Konstanta
<!DOCTYPE html> <html> <body> <?php // case-sensitive constant name define("GREETING", "Welcome to W3Schools.com!"); echo GREETING; ?> </body> </html>
Hasilnya :
5. If dan Else
<!DOCTYPE html> <html> <body> <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> </body> </html>
Hasilnya :
6. Array
<!DOCTYPE html> <html> <body> <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> </body> </html>
Hasilnya :
7. Form input
<!DOCTYPE HTML> <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
Hasilnya :
8. Form Validasi
<!DOCTYPE HTML> <html> <head> </head> <body> <?php // define variables and set to empty values $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $website = test_input($_POST["website"]); $comment = test_input($_POST["comment"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
Hasilnya :
Komentar
Posting Komentar