PHP

PHP object oriented membership method


Use 1 column to read the members: you can write 1 by yourself to deepen your understanding.

So one of these requirements; Wish people could talk, do arithmetic… , so we need to use the member method: 1, add speak member method, output I am xiaoming 2. Add jisuan member method, which can be calculated from 1+.. The result of + 1000 3. Modify the jisuan member method, which can receive 1 number n and calculate 1+.. The result of + n 4. Add the add member method to calculate the sum of two Numbers

Reference code:

<?php
    class Person{

        public $name;
        public $age;
        // Add member method
        public function speak(){

            echo " I am xiao Ming ";
        }

        public function jisuan(){
            // Calculated from the  1+..+1000 The results of the
            $result =0;
            for($i=1;$i<=1000;$i++){
                $result+=$i;
            }
            // Take the result of the calculation ruturn  return
            return $result;
        }
        public function jisuan2($n){
            // Calculated from the  1+..+n The results of the
            $result=0;
            for($i=1;$i<=$n;$i++){
                $result+=$i;
            }
            //return
            return $result;
        }
        // To calculate 2 The number and
        public function add($num1,$num2){
            return $num1+$num2;
        }
    }
    $person1=new Person;
    // Talk to people
    $person1->speak().'<br/>';
    // To calculate
    echo '<br/>'.$person1->jisuan();
    //echo '<br/>'.$person1->jisuan(100);
    // With parameters
    echo ' The result of the calculation is :'.$person1->jisuan2(5);
    // To calculate 2 The number and
    echo "<br/>50+50=".$person1->add(50,51);
?>