PHP Small Exercises

Source: Internet
Author: User
Tags php language

A few days ago in Baidu know inside see a netizen asked how to make a small program: in PHP language design a small program, calculate today to the number of days to the next month, all output these days, and make daily date in three colors cycle display, set three forms, let the user choose font Color, The three colors are then looped out to reflect those dates on the output . Today just have time to make it out for him, this actually does not have any practical use, but novice friends can be as a PHP small exercise program, with him to familiarize or deepen the knowledge of PHP!

At that time I gave him a general idea as follows:

1, calculate the number of days to the next month, you can use two arrays an array to access the month of 30 days, an array access to 31 days of the month (of course, there are 29, 28 days, this can be studied carefully, above is just an example)! Then get the month of the day to see in which array the total number of days in the current month is determined, and then you can output all days from the current day to the end of the month!

2, let the user choose three kinds of font color, I think a form on the line, with a checkbox to let users choose the three favorite colors!

3, loop output three kinds of colors, I think it can!

123456789 //$color 是存储三种颜色的数组//$date 是存储所有天数的数组$i = 0;$content‘‘;foreach($dateas$value) {    $content.= ‘<span style="color:‘.$color[$i].‘">‘.$value.‘</span>‘;    $i++;    if($i> 2) $i= 0;}

This $content contains all the days and colors of the style!

The following is the specific code that I have written out of the PHP small exercise project, we can see, the wrong place please correct me!

First, the HTML part of the code, let the user choose the color (no specified how many kinds)

12345678 <formmethod="post">    <input type="checkbox"value="red"name="color[]"/> 红    <inputtype="checkbox"value="black"name="color[]"/> 黑    <inputtype="checkbox"value="blue"name="color[]"/> 蓝    <inputtype="checkbox"value="yellow"name="color[]"/> 黄    <inputtype="checkbox"value="green"name="color[]"/> 绿    <inputtype="submit" value="提交"name="send"/></form>

Then the PHP backend code

12345678910111213141516171819202122232425262728293031 if(isset($_POST[‘send‘])) {    //获取颜色    $color$_POST[‘color‘];    //计算天数    $date=  date(‘Y-m-d‘, time());    $datesexplode(‘-‘$date);    $syarray(‘1‘,‘3‘,‘5‘,‘7‘,‘8‘,‘10‘,‘12‘);    $ssarray(‘4‘,‘6‘,‘9‘,‘11‘);    //判断    if(in_array($dates[1], $sy)) {        $day= range($dates[2], 31);    elseif(in_array($dates[1], $ss)) {        $day= range($dates[2], 30);    else{        if(($dates[0]%4 == 0 && $dates[0]%100 != 0)             || ($dates[0]%100 == 0 && $dates[0]%400 == 0)) {            $day= range($dates[2], 29);        else{            $day= range($dates[2], 28);        }    }    //将天数与颜色搭配    $i= 0;    $content‘‘;    foreach($dayas$value) {        $content.= ‘<span style="color:‘.$color[$i].‘">‘.$value.‘</span> ‘;        $i++;        if($i> (count($color)-1)) $i= 0;    }        echo$content;

So $content can output the desired effect!

Want to get the updated content of Wang Yip's personal blog in time every day? Quickly add the public number "LY89CN", or scan the QR code below!

This article originates from the personal blog of Wang Ye Building, this article address: http://www.ly89.cn/detailB/64.html

Welcome to share this article, reprint please indicate the source and address of this article

PHP Small Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.