Home  >  Article  >  Backend Development  >  How to randomly change background images in php

How to randomly change background images in php

PHPz
PHPzOriginal
2023-04-11 10:33:11708browse

PHP is a widely used programming language that provides numerous functions and class libraries to support various application scenarios, such as Web development, data processing, image processing, etc. Among them, web development is one of the fields where PHP is most widely used. PHP can be combined with HTML, CSS, JavaScript and other technologies to build a large number of dynamic web pages, web applications and web services.

In web development, background images are a very important element, which can set off the atmosphere of the web page and increase its beauty and appeal. Some websites even randomly change background images to give visitors a sense of freshness and surprise. Today, I will introduce how to use PHP to implement the function of randomly changing background images.

First, we need to prepare some background images and store them in a directory on the server, such as /images/background/. Here, I have prepared three background images, namely bg1.jpg, bg2.jpg, and bg3.jpg.

Next, we use PHP's rand() function to generate a random number, and use this random number to select a background image as the current background image. The code is as follows:

<?php
    $bgCount = 3; // 背景图片数量
    $bgIndex = rand(1, $bgCount); // 随机生成一个背景图片序号
    $bgImagePath = "/images/background/bg{$bgIndex}.jpg"; // 当前背景图片的路径
?>

In the above code, the $bgCount variable represents the number of background images, and $bgIndex is a random number generated by the rand() function, between 1 and $bgCount. According to the value of the $bgIndex variable, we splice out the path of the current background image $bgImagePath, such as /bg1.jpg.

Next, we apply $bgImagePath to the HTML style to display the background image. The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <title>随机更改背景图片</title>
    <style>
        body {
            background-image: url("<?php echo $bgImagePath; ?>");
            background-size: cover;
            background-position: center center;
            background-repeat: no-repeat;
            height: 100vh;
        }
    </style>
</head>
<body>
    <h1>随机更改背景图片</h1>
    <p>每次刷新本页,背景图片都会变化。</p>
</body>
</html>

As you can see, in the above HTML code, we apply the $bgImagePath variable to the background-image style of the body element to achieve the effect of displaying the background image. In addition, we also set some other styles, such as making the background cover the entire web page, center alignment, etc.

Finally, we integrate the above two pieces of code to realize the function of randomly changing the background image. The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <title>随机更改背景图片</title>
    <?php
        $bgCount = 3; // 背景图片数量
        $bgIndex = rand(1, $bgCount); // 随机生成一个背景图片序号
        $bgImagePath = "/images/background/bg{$bgIndex}.jpg"; // 当前背景图片的路径
    ?>
    <style>
        body {
            background-image: url("<?php echo $bgImagePath; ?>");
            background-size: cover;
            background-position: center center;
            background-repeat: no-repeat;
            height: 100vh;
        }
    </style>
</head>
<body>
    <h1>随机更改背景图片</h1>
    <p>每次刷新本页,背景图片都会变化。</p>
</body>
</html>

The above is the complete code for randomly changing background images using PHP. When you visit this page, it will randomly select one of the prepared background images as the current background image. Each refresh will have a different effect, giving people a sense of surprise and freshness.

The above is the detailed content of How to randomly change background images in php. 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