📖
php 랜덤 색상 출력

페이지 정보

본문

랜덤으로 출력하려면 배열을 사용하여 무작위로 선택하면 됩니다.
✅ 1. 랜덤 색상 출력 (HEX 코드)[code]<?php
$colors = ["#ff0080", "#00c6ff", "#00ff00"]; // 색상 배열
$randomColor = $colors[array_rand($colors)]; // 랜덤 색상 선택

echo $randomColor;
?>[/code]실행할 때마다 #ff0080, #00c6ff, #00ff00 중 하나가 랜덤으로 출력됩니다.
✅ 2. HTML과 함께 배경색 적용[code]<?php
$colors = ["#ff0080", "#00c6ff", "#00ff00"];
$randomColor = $colors[array_rand($colors)];
?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>랜덤 배경색</title>
    <style>
        body {
            background-color: <?= $randomColor ?>;
            color: white;
            text-align: center;
            font-size: 24px;
            padding: 50px;
        }
    </style>
</head>
<body>
    <h1>랜덤 색상: <?= $randomColor ?></h1>
</body>
</html>[/code]실행할 때마다 랜덤한 배경색과 해당 색상의 HEX 코드가 표시됩니다.
✅ 필요에 따라 배열에 더 많은 색상을 추가할 수도 있습니다.

댓글목록

등록된 댓글이 없습니다.


🔍 검색

회사소개 개인정보처리방침 서비스이용약관
Copyright © rainbowgarden.shop All rights reserved.