2023年1月31日星期二

CHATGPT教我, 十分鐘學會/做首個PHP網頁

 

what is php

PHP (Hypertext Preprocessor) is a server-side scripting language used for developing dynamic web pages. It is widely used in web development to build dynamic websites, create and manage databases, process form data, handle cookies, and more. PHP code is executed on the server and generates HTML output that is sent to the client's browser. PHP is widely supported and runs on most web servers, making it a popular choice for web development.

wtmsintb@gmail.com
what i need to do if i want to install a php web server into my windows pc

To install a PHP web server on your Windows PC, you will need to follow these steps:

  1. Download and install XAMPP, a free and open-source web server package that includes Apache, PHP, and MySQL.

  2. Launch the XAMPP Control Panel and start the Apache and MySQL modules.

  3. Create a new folder in the XAMPP htdocs directory and name it as your project. This will be your web server root directory.

  4. Write and save your PHP code in a file with a .php extension in the project directory.

  5. Open your web browser and type http://localhost/your-project-folder-name in the address bar to view your PHP code in the browser.

  6. If your PHP code is working properly, you should see the output generated by the PHP script.

Note: Keep in mind that this is just a local installation for testing purposes. If you want to host your PHP website on the internet, you will need to purchase web hosting and upload your files to the server.

wtmsintb@gmail.com
create a first php for me and guide me to start the php server

Here's a simple PHP script to display "Hello World" on the browser:

php
<!DOCTYPE html> <html> <head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World"; ?> </body> </html>

To start the PHP server, follow these steps:

  1. Download and install XAMPP, a free and open-source web server package that includes Apache, PHP, and MySQL.

  2. Launch the XAMPP Control Panel and start the Apache and MySQL modules.

  3. Create a new folder in the XAMPP htdocs directory and name it as your project. This will be your web server root directory.

  4. Write and save the above PHP code in a file with a .php extension in the project directory.

  5. Open your web browser and type http://localhost/your-project-folder-name/your-php-file-name.php in the address bar to view your PHP code in the browser.

  6. If your PHP code is working properly, you should see "Hello World" displayed in the browser.

Note: Keep in mind that this is just a local installation for testing purposes. If you want to host your PHP website on the internet, you will need to purchase web hosting and upload your files to the server.

wtmsintb@gmail.com
write a to do list php with add and delete button

Here's an example of a to-do list implemented in PHP with add and delete buttons:

php
<!DOCTYPE html> <html> <head> <title>To-Do List</title> </head> <body> <h1>To-Do List</h1> <form action="" method="post"> <input type="text" name="task" placeholder="Add a task"> <input type="submit" name="add" value="Add"> </form> <?php session_start(); if (!isset($_SESSION['tasks'])) { $_SESSION['tasks'] = array(); } if (isset($_POST['add'])) { array_push($_SESSION['tasks'], $_POST['task']); } if (isset($_GET['delete'])) { unset($_SESSION['tasks'][$_GET['delete']]); $_SESSION['tasks'] = array_values($_SESSION['tasks']); } ?> <table> <tr> <th>Task</th> <th>Action</th> </tr> <?php $tasks = $_SESSION['tasks']; for ($i = 0; $i < count($tasks); $i++) { echo "<tr>"; echo "<td>" . $tasks[$i] . "</td>"; echo "<td><a href='?delete=$i'>Delete</a></td>"; echo "</tr>"; } ?> </table> </body> </html>

This script uses PHP's session functions to store the to-do list items. When the add button is pressed, a new task is added to the $_SESSION['tasks'] array. When the delete button is pressed, the corresponding task is removed from the array using the unset function. The list is displayed in a table and the tasks are stored in the session until the session is ended or the browser is closed.

 
 
 



很久以前我已很想學, 用GOOGLE找了大半天都攪不好, 今天用十分鐘就攪掂. 終於明白以往自學的困難是什麼:


「欠缺一個亙動的人在旁教學」


今天, AI做到了, 做教學的老師們, 小心飯碗!重要的事情講三次:

 

小心飯碗!

小心飯碗!

小心飯碗!

沒有留言:

發佈留言

20240313 月供23回

 恒指17166 (20240313-0954)