Home > Backend Development > PHP Tutorial > PHP开发(16)-global-static-func_get_args-sort-PhpStorm

PHP开发(16)-global-static-func_get_args-sort-PhpStorm

黄舟
Release: 2023-03-06 10:08:01
Original
1099 people have browsed it

PHP Development (16)-global-static-func_get_args-sort-PhpStorm


<?php
    /**
     * 全局变量和局部变量
     * global 全局
     */
    echo "---------- global Demo ----------<br>";
    $name = "iwanghang";
    $sex = "man";
    $_POST[&#39;a&#39;] = 100; // 带下划线的9个默认就已经是全局变量,不需要global
    static $age = 8;
    person();

    function person(){
        global $name;
        echo $name; // 打印结果:iwanghang
        echo $sex; // 会报错
        echo $_POST[&#39;a&#39;]; // 打印结果:100
        echo "<br>";
    }

    /**
     * 静态变量
     * static 静态
     */
    echo "---------- static Demo ----------<br>";
    class A{
        public static $b = 0;
    }

    $c = new A();
    echo A::$b++; // 打印结果:0
    echo "<br>";
    echo A::$b; // 打印结果:1
    echo "<br>";
    $d = new A();
    echo A::$b; // 打印结果:1
    echo "<br>";

    /**
     *  function Demo 1
     */
    echo &#39;---------- function Demo 1 :function add($x,$y){} ----------<br>&#39;;
    echo add(10,10)."<br>"; // 打印结果:20
    echo add(20,20)."<br>"; // 打印结果:40
    function add($x,$y){
        return $x+$y;
    }

    /**
     * function Demo 2
     */
    echo &#39;---------- function Demo 2 : function demo(&$yy){} ----------<br>&#39;;
    $xx = 20;
    function demo(&$yy){
        $yy = 100;
    }
    echo $xx."<br>";
    demo($xx);
    echo $xx."<br>";

    /**
     * function Demo 3
     */
    echo &#39;---------- function Demo 3 : function demo2($number1 = 0, $number2 = 0, $number3 = 0, $number4 = 0){} ----------<br>&#39;;
    demo2();
    demo2(1,2,3,4);
    demo2(1,2);
    demo2(null,null,1,2);
    function demo2($number1 = 0, $number2 = 0, $number3 = 0, $number4 = 0){
        echo "$number1 , $number2 , $number3 , $number4<br>";
    }

    /**
     * function Demo 4
     * func_get_args() 返回一个数组,包含所有参数
     * func_num_args() 返回参数总数
     * func_get_arg() 接受一个数字参数,返回指定参数
     */
    echo &#39;---------- function Demo 4 :func_get_args() ----------<br>&#39;;
    echo demo3(10,20,30,40,50)."<br>";
    echo demo3(1,2,3,4,5)."<br>";
    function demo3(){
        $arr = func_get_args();
        $sum = 0;
        for ($i=0; $i<count($arr); $i++){
            $sum += $arr[$i];
        }
        return $sum;
    }

    /**
     * function Demo 5
     * var
     */
    echo &#39;---------- function Demo 5 :var ----------<br>&#39;;
    $var = "add";
    echo $var(10,20);
    echo "<br>";
    function add2($m,$n){
        return $m + $n;
    }

    /**
     *  print_r 打印出复杂类型变量的值(如数组,对象)
     *  sort 对数组中的元素按字母进行升序排序
     */
    echo &#39;---------- print_r 打印数组 、 sort 升序排序 ----------<br>&#39;;
    $arr2 = array(3,5,1,2,13,8);
    print_r($arr2); // Array ( [0] => 3 [1] => 5 [2] => 1 [3] => 2 [4] => 13 [5] => 8 )
    echo "<br>";
    sort($arr2);
    print_r($arr2); // Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 8 [5] => 13 )
    echo "<br>";

    /**
     * 练习 打印2到32的偶数 其中3的倍数不打印
     */
    echo &#39;---------- function Demo 6 :打印2到32的偶数 其中3的倍数不打印 ----------<br>&#39;;
    demo4(2,32);
    function demo4($start, $num){
        for ($i=$start;$i<$num;$i+=2){
            if ($i%3==0){
                continue;
            }
            echo $i."<br>";
        }
    }
Copy after login

The above is PHP Development (16)-global-static-func_get_args -sort-PhpStorm content, for more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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