Home  >  Article  >  Backend Development  >  Creative use of PHP development: five project practices

Creative use of PHP development: five project practices

PHPz
PHPzOriginal
2023-09-09 19:21:351108browse

Creative use of PHP development: five project practices

Creative use of PHP development: Five project practices

Introduction:
PHP is a powerful server-side scripting language that is widely used in Web development . In addition to traditional web development, we can develop more interesting and practical projects by creatively using PHP. This article will introduce five projects developed creatively using PHP and provide code examples, hoping to bring inspiration to readers.

1. Online voting system
The online voting system can be used to organize various voting activities, such as internal company elections, campus event voting, etc. The following is a sample code:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $option = $_POST['option'];
    
    $file = 'votes.txt';
    $votes = file_get_contents($file);
    $votes = unserialize($votes);
    
    if(array_key_exists($option, $votes)) {
        $votes[$option] += 1;
    } else {
        $votes[$option] = 1;
    }
    
    file_put_contents($file, serialize($votes));
    
    header('Location: results.php');
    exit;
}
?>

<form action="" method="post">
    <label for="option1">Option 1</label>
    <input type="radio" name="option" id="option1" value="option1">
    
    <label for="option2">Option 2</label>
    <input type="radio" name="option" id="option2" value="option2">
    
    <input type="submit" value="Vote">
</form>

2. E-commerce website
E-commerce website is a project that many people are familiar with. The following is a sample code:

<?php
session_start();

$products = [
    [
        'name' => 'Product 1',
        'price' => 10.99,
        'inventory' => 5
    ],
    [
        'name' => 'Product 2',
        'price' => 19.99,
        'inventory' => 10
    ],
    // more products...
];

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $productId = $_POST['product_id'];
    
    if(isset($products[$productId]) && $products[$productId]['inventory'] > 0) {
        if(!isset($_SESSION['cart'])) {
            $_SESSION['cart'] = [];
        }
        
        $_SESSION['cart'][] = $productId;
        
        $products[$productId]['inventory']--;
        
        header('Location: cart.php');
        exit;
    }
}
?>

<h1>Products</h1>

<?php foreach($products as $productId => $product): ?>
    <div>
        <h3><?php echo $product['name']; ?></h3>
        <p>Price: <?php echo $product['price']; ?></p>
        <p>Inventory: <?php echo $product['inventory']; ?></p>
        
        <form action="" method="post">
            <input type="hidden" name="product_id" value="<?php echo $productId; ?>">
            <input type="submit" value="Add to Cart">
        </form>
    </div>
<?php endforeach; ?>

3. Task management tool
Task management tool can help us manage tasks and reminders effectively. The following is a sample code:

<?php
session_start();

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $task = $_POST['task'];
    
    if(!isset($_SESSION['tasks'])) {
        $_SESSION['tasks'] = [];
    }
    
    $_SESSION['tasks'][] = $task;
    
    header('Location: tasks.php');
    exit;
}
?>

<h1>Task Management</h1>

<h2>Add Task</h2>

<form action="" method="post">
    <input type="text" name="task" placeholder="Enter task">
    <input type="submit" value="Add">
</form>

<h2>Tasks</h2>

<?php if(isset($_SESSION['tasks']) && !empty($_SESSION['tasks'])): ?>
    <ul>
        <?php foreach($_SESSION['tasks'] as $task): ?>
            <li><?php echo $task; ?></li>
        <?php endforeach; ?>
    </ul>
<?php else: ?>
    <p>No tasks found.</p>
<?php endif; ?>

4. Online Chat Room
Online chat room is a very interesting and practical project that can be used for real-time communication and collaboration. The following is a sample code:

<?php
session_start();

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $message = $_POST['message'];
    
    if(!isset($_SESSION['chat'])) {
        $_SESSION['chat'] = [];
    }
    
    $_SESSION['chat'][] = $message;
    
    header('Location: chatroom.php');
    exit;
}
?>

<h1>Chatroom</h1>

<h2>Messages</h2>

<?php if(isset($_SESSION['chat']) && !empty($_SESSION['chat'])): ?>
    <ul>
        <?php foreach($_SESSION['chat'] as $message): ?>
            <li><?php echo $message; ?></li>
        <?php endforeach; ?>
    </ul>
<?php else: ?>
    <p>No messages found.</p>
<?php endif; ?>

<h2>Send Message</h2>

<form action="" method="post">
    <input type="text" name="message" placeholder="Enter message">
    <input type="submit" value="Send">
</form>

5. API development
Using PHP to develop APIs can provide data and functions for use by other applications. The following is a sample code:

<?php
header('Content-Type: application/json');

$data = [
    'name' => 'John',
    'age' => 30,
    'email' => 'john@example.com'
];

echo json_encode($data);
?>

Conclusion:
This article introduces five project practices for creative use of PHP development and provides corresponding code examples. Through the flexible use of PHP, we can develop more interesting and practical projects. I hope this article can inspire readers, stimulate creativity, and create more amazing PHP projects.

The above is the detailed content of Creative use of PHP development: five project practices. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn