Home > Backend Development > PHP Tutorial > PHP learning diary (1) - the use of classes and functions, php diary_PHP tutorial

PHP learning diary (1) - the use of classes and functions, php diary_PHP tutorial

WBOY
Release: 2016-07-12 09:07:46
Original
764 people have browsed it

PHP learning diary (1) - the use of classes and functions, php diary

1. Custom functions

<span>function</span> add(<span>$a</span>,<span>$b</span><span>){
    </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
    </span><span>echo</span> 'add test:'<span>;
    </span><span>echo</span> <span>$c</span><span>;
    </span><span>return</span> <span>$c</span><span>;
}
add(</span>1,2);
Copy after login

Output result:

add test:3
Copy after login

2. Call the functions in the class

1. Double colon::, no need to instantiate, just call the class name directly

<span>class</span><span> test{
    </span><span>public</span> <span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span> <span>$c</span><span>;        
        </span><span>return</span> <span>$c</span><span>;
    }
}
test</span>::add(1,2);
Copy after login

2, ->, the instantiated object uses

<span>class</span><span> test{
    </span><span>public</span> <span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span> <span>$c</span><span>;        
        </span><span>return</span> <span>$c</span><span>;
    }
}
</span><span>$object</span>=<span>new</span><span> test();
</span><span>$object</span>->add(1,3);
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059466.htmlTechArticlePHP learning diary (1) - the use of classes and functions, php diary 1. Custom function function add( $a , $b ){ $c = $a $b ; echo 'add test:' ; echo $c ; return $c ;}add( 1,2); Output...
Related labels:
php
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