Home  >  Article  >  Backend Development  >  PHP syntax basics, php syntax_PHP tutorial

PHP syntax basics, php syntax_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:05:22990browse

PHP syntax basics, php syntax

1.PHP syntax basics
  • $a = 10; Variables can be modified during operation
<span>$a</span> = 10<span>;
</span><span>$a</span> = 20<span>;
</span><span>$b</span> = 5<span>;
</span><span>echo</span> <span>$a</span>+<span>$b</span>;
  • const THE_VALUE = 100; Constants can only be assigned once, otherwise a syntax error will be reported
<span>const</span> THE_VALUE = 100<span>;
</span><span>//</span><span>THE_VALUE = 200; 有语法错误 运行时会报错</span>
<span>echo</span> THE_VALUE;
  • How to define constants in PHP4
<span>define</span>('THE_VALUE',200<span>);
</span><span>//</span><span>THE_VALUE = 200; 有语法错误</span>
<span>echo</span> THE_VALUE;

2. Function is a code block that wraps many functions
  • Simple function
<span>function</span><span> traceHelloPHP(){
    </span><span>echo</span> 'Hello PHP'<span>;
    </span><span>echo</span> '<br/>'<span>;
    </span><span>echo</span> 'Hello World'<span>;
}
traceHelloPHP();</span>
  • Use functions as callback functions for other functions
<span>function</span><span> traceHelloPHP(){
    </span><span>echo</span> 'Hello PHP&mdash;&mdash;back'<span>;
    </span><span>echo</span> '<br/>'<span>;
    </span><span>echo</span> 'Hello World'<span>;
}
</span><span>//</span><span>traceHelloPHP();</span>
 
<span>$func</span> = 'traceHelloPHP'<span>;
</span><span>$func</span>();
  • Incoming parameters of the function
<span>function</span> sayHello(<span>$name</span><span>){
    </span><span>echo</span> 'Hello '.<span>$name</span><span>;
    </span><span>echo</span> '<br/>'<span>;
}
sayHello(</span>'ZhangSan'<span>);
sayHello(</span>'Lisi');
  • Function with multiple input parameters
<span>function</span> traceNum(<span>$a</span>,<span>$b</span><span>){
    </span><span>echo</span> 'a = '.<span>$a</span>.' , b = '.<span>$b</span>.'<br/>'<span>;
    </span><span>echo</span> "a = <span>$a</span> , b = <span>$b</span><br/>";  <span>//</span><span>这样写也是可以的</span>
<span>}
traceNum(</span>2,3);
  • Function with return value
<span>function</span> add(<span>$a</span>,<span>$b</span><span>){
    </span><span>return</span> <span>$a</span>+<span>$b</span><span>;
}
</span><span>echo</span> add(10,2).'<br>';

3. Process control
  • The reason for garbled characters: the encoding method of the server is different from the encoding method of the client. Solution: add html header and embed php code into the body
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Control Flow</title>
</head>
<body>
<?<span>php
</span><span>/*</span><span>*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/10
 * Time: 16:07
 </span><span>*/</span>
<span>function</span> getLevel(<span>$score</span><span>){
    </span><span>if</span>(<span>$score</span> >= 90<span>){
        </span><span>return</span> '优秀'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 80<span>){
        </span><span>return</span> '良好'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 70<span>){
        </span><span>return</span> '好'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 60<span>){
        </span><span>return</span> '可以'<span>;
    }</span><span>else</span><span> {
        </span><span>return</span> '差'<span>;
    }
}
</span><span>echo</span> getLevel(92).'<br/>'<span>;
</span>?>
</body>
</html>
  • Control statement writing method 2
<span>function</span> getLevel_switch(<span>$score</span><span>){
    </span><span>switch</span>(<span>$score</span>/10<span>){
        </span><span>case</span> 10:
        <span>case</span> 9:
            <span>return</span> '优秀'<span>;
        </span><span>case</span> 8:
            <span>return</span> '良好'<span>;
        </span><span>case</span> 7:
            <span>return</span> '好'<span>;
        </span><span>case</span> 6:
            <span>return</span> '可以'<span>;
        </span><span>default</span>:
            <span>return</span> '差'<span>;
    }
}
</span><span>echo</span> getLevel_switch(60).'<br/>';
  • Control statement writing method 3
<span>function</span> getLevel_break(<span>$score</span><span>){
    </span><span>$result</span> = '--'<span>;
    </span><span>switch</span>(<span>intval</span>(<span>$score</span>/10<span>)){
        </span><span>case</span> 10:
        <span>case</span> 9:
            <span>$result</span> = '优秀';<span>break</span><span>;
        </span><span>case</span> 8:
            <span>$result</span> = '良好';<span>break</span><span>;
        </span><span>case</span> 7:
            <span>$result</span> = '好';<span>break</span><span>;
        </span><span>case</span> 6:
            <span>$result</span> = '可以';<span>break</span><span>;
    }
    </span><span>return</span> <span>$result</span><span>;
}
</span><span>echo</span> getLevel_break(83).'<br/>';

4. Loop
  • for loop
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
}</span>
  • While loop, judge first and then execute
<span>$i</span> = 0<span>;
</span><span>while</span>(<span>$i</span> < 50<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
    </span><span>$i</span>++<span>;
}</span>
  • do-while loop is executed first and then judged
<span>$i</span> = 0<span>;
</span><span>do</span><span>{
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br>'<span>;
    </span><span>$i</span>++;   <span>//</span><span>一定不要忘记自增,否则就会编程死循环</span>
}<span>while</span>(<span>$i</span><20);
  • break out *whole* loop
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
    </span><span>if</span>( <span>$i</span> == 20<span> ){
        </span><span>break</span><span>;
    }
}</span>
  • continue jumps out of *this time* loop
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
</span><span>//</span><span>    if( $i == 20 ){
//        break;
//    }</span>
    <span>if</span>(20 == <span>$i</span><span>){
        </span><span>continue</span><span>;
    }
    </span><span>echo</span> 'Run here '.<span>$i</span>.'<br/>'<span>;
}</span>

5. Logical operations AND&& or || NOT!
<span>function</span><span> traceNum(){
    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><=100;<span>$i</span>++<span>){
</span><span>//</span><span>        if(0 == $i%2 && 0==$i%3){
//            echo $i.'<br/>';
//        }
//        if(0 == $i%2 || 0==$i%3){
//            echo $i.'<br/>';
//        }</span>
        <span>if</span>(!(0 == <span>$i</span>%2<span>)){
            </span><span>echo</span> <span>$i</span>.'<br/>'<span>;
        }
    }
}
traceNum();</span>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1069669.htmlTechArticlePHP syntax basics, php syntax 1.PHP syntax basics $a = 10; Variables can be modified during running $ a = 10 ; $a = 20 ; $b = 5 ; echo $a $b ; const THE_VALUE = 100; Constants can only be assigned values...
Statement:
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