Php algorithm instance sharing, php algorithm instance

Source: Internet
Author: User

Php algorithm instance sharing, php algorithm instance

Print 0 only

The specific number is determined by the input parameter n.

If n = 5, 00000 is printed.

<?php  $n = $_GET['n'];  for ($i=0; $i < $n; $i++) {    echo "0";  }?>

Print a row 0101010101010101010101

The specific number is determined by the input parameter n.

Such as test. php? N = 3 Print 010

<?php  $n = $_GET['n'];  for ($i=0; $i < $n; $i++) {    if ($i % 2 ==0) {      echo "0";    } else{      echo "1";    }  }?>

1 00 111 0000 11111

For if implementation

<?phpfor ($i = 0; $i < 10; $i++) {  for ($j = 0; $j <= $i; $j++) {    if ($i % 2 == 0) {      echo '0';    } else {      echo '1';    }  }  echo '<br/>';}?>

For switch implementation

<?phpfor ($i = 0; $i < 10; $i++) {  for ($j = 0; $j <= $i; $j++) {    switch ($j % 2) {      case '0':      echo "0";      break;    case '1':      echo "1";      break;    }  }  echo '<br/>';}?>

While if implementation

While switch implementation

<?php$i = 0;while ($i < 10) {  $j = 0;  while ($j <= $i) {    switch ($i % 2) {      case 0:        echo '0';        break;      case 1:        echo '1';        break;    }    $j++;  }  echo '<br/>';  $i++;}?>

Implement 0 01 010 0101 ......

0 01 012 0123 3210 210 10 0

Make a calculator

Such as test. php? A = 1 & B = 2 & operator = jia output 3

Such as test. php? A = 5 & B = 2 & operator = jian output 3

Such as test. php? A = 2 & B = 5 & operator = cheng output 10

Such as test. php? A = 6 & B = 3 & operator = chu output 2

<?php  $a = $_GET['a'];  $b = $_GET['b'];  $operator = $_GET['operator'];  function calculate($a,$b,$operator) {    switch ($operator) {      case 'jia':        $result = $a + $b;        return $result;        break;      case 'jian':        $result = $a - $b;        return $result;      break;      case 'cheng':        $result = $a * $b;        return $result;      break;      case 'chu':        $result = $a / $b;        return $result;      break;    }  }  echo calculate($a,$b,$operator);?>

The above is all the content of this article. I hope you will like it.

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.