Only print 0
The specific number is determined by the input parameter n
If n=5, print 00000
-
- $n = $_GET['n'];
- for ($i=0; $i < $n; $i++) {
- echo "0";
- }
- ? >
-
-
Copy code
Print a line 0101010101010101010101
The specific number is determined by the input parameter n
For example test.php?n=3 print 010
-
- $n = $_GET['n'];
- for ($i=0; $i < $n; $i++) {
- if ($i % 2 ==0 ) {
- echo "0";
- } else{
- echo "1";
- }
- }
- ?>
-
-
Copy code
Achieve 1 00 111 0000 11111
for if implementation
-
- for ($i = 0; $i < 10; $i++) {
- for ($j = 0; $j <= $i; $j++) {
- if ($i % 2 == 0) {
- echo '0';
- } else {
- echo '1';
- }
- }
- echo '
';
- }
-
- ?>
-
-
Copy code
For switch implementation
-
- 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 '
';
- }
-
- ?>
-
-
Copy code
while if implementation
while switch implementation
-
- $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 '
';
- $ i++;
- }
-
- ?>
-
-
Copy code
Achieve 0 01 010 0101……
Achieve 0 01 012 0123 3210 210 10 0
Make a calculator
For example, test.php?a=1&b=2&operator=jia outputs 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 outputs 2
-
- $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);
- ?>
-
Copy code
The above is the entire content of this article, I hope you all like it.
|