【技术宅11】php入门运算

//1、空bool

       $a='';

       $b=NULL;

       $c=false;

       $d=0;

       $e='0';

       $f=array();

       $g=array(array());

       $h='NULL';

       var_dump($a);//string(0) ""

       var_dump($b);//NULl

       var_dump($c);//bool(false)

       var_dump($d);//int(0)

       var_dump($e);//string(1) "0"

       var_dump($f);//array(0) { }

       var_dump($g);//array(1) { [0]=> array(0) { } }

       var_dump($h);//string(4) "NULL"

      

       //ture

              //$a == $b == $c == $d

              //$d == $e;

             

       //false

              //$a === $b === $c === $d

              //$b == $h

              //$a ==$f

 

//2、global在函数内外的区别

       //在函数内,外部的改变对内部起作用

              $n=5;

              function sum($k){

                     global $n;

                     return $k+$n;

              }

             

              echo $n.'<br/>';         //5

              echo sum(10).'<br/>';    //15

              $n=20;

              echo $n.'<br/>';        //20  

              echo sum(10).'<br/>';   //30

 

       //在函数外,外部的改变对内部不起作用

              global $n;

              function sum($k){

                     $n=5;

                     return $k+$n;

              }

             

              echo $n.'<br/>';         //''

              echo sum(10).'<br/>';    //15

              $n=20;

              echo $n.'<br/>';        //20  

              echo sum(10).'<br/>';   //15

             

 

//3、九九口诀表

       for($i=1;$i<10;$i++){

              for($j=1;$j<=$i;$j++){

                     echo "$i*$j=".$i*$j."&nbsp;";

              }

              echo "<br/>";

       }

 

//4、n的阶乘

       function jc($m){

              $n=1;

              for($i=1;$i<=$m;$i++){

                     $n=$n*$i;

              }

              return $n;

       }

       echo "12的阶乘=".jc(12);

 

 

//5、累加

       //方法一:

              function leijia($m,$type=1){

                     $n=0;

                     if($type=='o'){//偶数累加

                            for($i=0;$i<=$m;$i+=2){

                                   $n=$n+$i;

                            }                         

                     }elseif($type=='q'){//奇数累加

                            for($i=1;$i<=$m;$i+=2){

                                   $n=$n+$i;

                            }                         

                     }else{//奇偶累加  

                            for($i=1;$i<=$m;$i++){

                                   $n=$n+$i;

                            }

                     }

                     return $n;

              }

 

       //方法二:

              function sum($n):

                     if($n==1) return 1;

                     return $n+sum($n-1);

              }

 

//6、循环输出奇数或偶数

       //方法一:

              $i=1;

              while($i<=100){

                     echo $i.'-';

                     $i +=2;

              }

       //方法二:

              do{

                     echo $i.'-';

                     $i +=2;

              }while($i<=100);

       //方法三:

              for($i=1;$i<100;$i++){

                     if($i%2 == 1){

                            echo $i.'-';

                     }

              }

 

 

//6、正方形

       function jx($x,$y){

              for($i=1;$i<=$x;$i++){

                     for($j=1;$j<=$y;$j++){

                            echo "*"."&nbsp;";

                     }

                     echo "<br/>";

              }

       }

       echo jx(5,5);

 

 

//7、半个金字塔

       function jin($n){

              for($i=1;$i<=$n;$i++){

                     for($j=1;$j<=$i;$j++){

                            echo "*";

                     }

                     echo "<br/>";

              }

       }

       echo jin(10);

 

//8、整个金字塔

       function jzt($n){

              for($i=1;$i<=$n;$i+=2){

                     for($k=1;$k<=($n-$i)/2;$k++){

                            echo "&nbsp;";

                     }

                     for($j=1;$j<=$i;$j++){

                            echo "*";

                     }

                     echo "<br/>";

              }

       }

       echo jzt(10);

 

//9、空心金字塔

       function jzt_kong($n){

              for($i=1;$i<=$n;$i++){

                     for($k=1;$k<=$n-$i;$k++){

                            echo "&nbsp;";

                     }           

                     for($j=1;$j<=($i-1)*2*1;$j++){

                            if($i ==1 || $i == $n){

                                   echo "*";

                            }elseif($j ==1){

                                   echo "*";

                            }else{

                                   if($j ==1 || $j ==($i-1)*2+1){

                                          echo "*";

                                   }else{

                                          echo "&nbsp;";

                                   }

                            }

                     }

                     echo "<br/>";

              }

       }

       echo jzt_kong(10);

 

 

//10、围棋

       function wq($n){

              echo "<table border='1px' width='200px' height='200px'>";

              for($i=1;$i<=$n;$i++){

                     echo "<tr>";

                     for($j=1;$j<=$n;$j++){

                            if($j%2 ==1 && $i%2 == 1){

                                   echo "<td bgcolor='#ccc'></td>";

                            }elseif($j%2 ==0 && $i%2 == 0){

                                   echo "<td bgcolor='#ccc'></td>";

                            }else{

                                   echo "<td></td>";

                            }

                     }

                     echo "</tr>";

              }

              echo "</table>";

       }

       echo wq(10);

 

//11

//        1

//      1 2 1

//    1 2 3 2 1

//  1 2 3 4 3 2 1

//1 2 3 4 5 4 3 2 1

 

       for($i=1;$i<5;$i++){

              for($j=1;$j<=5-$i;$j++){//输出空格

                     echo "&nbsp;";

              }

              for($j=1;$j<=$i;$j++){//输出前半部分

                     echo $j.'';

              }    

              for($j=$i-1;$j>=1;$j--){//输出后半部分

                     echo $j.'';

              }

              echo "<br/>";

       }

你可能感兴趣的:(php入门)