PHP algorithm example sharing, PHP algorithm example_PHP tutorial

WBOY
Release: 2016-07-13 09:46:34
Original
722 people have browsed it

PHP algorithm example sharing, PHP algorithm example

Only print 0

The specific number is determined by the input parameter n

If n=5, print 00000

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

Copy after login

Print a line 0101010101010101010101

The specific number is determined by the input parameter n

For example, test.php?n=3 prints 010

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

Copy after login

Achieve 1 00 111 0000 11111

 for if implementation

<&#63;php

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

&#63;>

Copy after login

 for switch implementation

<&#63;php

for ($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/>';
}

&#63;>

Copy after login

 while if implementation

While switch implementation

<&#63;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++;
}

&#63;>

Copy after login

Realize 0 01 010 0101……

Realize 0 01 012 0123 3210 210 10 0

Be a calculator

For example, test.php?a=1&b=2&operator=jia output 3

For example, test.php?a=5&b=2&operator=jian outputs 3

For example, test.php?a=2&b=5&operator=cheng outputs 10

For example, test.php?a=6&b=3&operator=chu output 2

<&#63;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);
&#63;>
Copy after login

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1032056.htmlTechArticlePHP algorithm example sharing, PHP algorithm example only prints 0. The specific number is determined by the input parameter n, such as n=5 Just print 00000 php $n = $_GET['n']; for ($i=0; $i $n; $i ) { echo "0"; } Print a line...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template