Thursday, 5 October 2017

RANDOM BACKGROUND IN PHP

To use a different color as random in php, follow the step below.

·        Step 1: Open your visual studio code or other apps that works for php.

·        Step 2: Open new file and name it like this ramdo.php

·        Step 3: Open php tags and  give it array color.  Example: <?php $colors=array(“red”, ‘’blue”,”lightblue”, “green”, “brown”?>.

·        Step 4: Open your html tags and style the body to have your random color.

Below code summarize the random background color in php. Try it yourself.

                                                      
<?php $colors=array("red","blue","lightblue","green","brown");
?>
<DOCTYPE html>
<html>
                 <head>
                                    </head>
        <style>
     body{
           background-color:<?php echo $colors[rand(0,count($colors)-1)]?>;
     }
     .area{
           width:100px;
           height:100px;
           background-color:<?php echo $colors[rand(0,count($colors)-1)]?>;
           margin-left:0;

           }
      </style>
            <body>
     <div class="area"></div>

            </body>


</html>

No comments:

Post a Comment