PHP syntax basics: php syntax. PHP syntax basics: php syntax 1. PHP syntax basics $ a10; variables can be modified during running $ a10; $ a20; $ b5; echo $ a + $ B; constTHE_VALUE100; constants can only be assigned PHP 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; a constant can only be assigned once; otherwise, a syntax error is reported.
Const THE_VALUE = 100; // THE_VALUE = 200; an echo THE_VALUE error is returned when a syntax error occurs;
- Method for defining constants in PHP4
Define ('The _ value', 200); // THE_VALUE = 200; syntax error echo THE_VALUE;
2. the Function encapsulates a lot of functional code blocks.
function traceHelloPHP(){ echo 'Hello PHP'; echo '
'; echo 'Hello World';}traceHelloPHP();
- Use a function as a callback for other functions
function traceHelloPHP(){ echo 'Hello PHP——back'; echo '
'; echo 'Hello World';}//traceHelloPHP(); $func = 'traceHelloPHP';$func();
- Function input parameters
function sayHello($name){ echo 'Hello '.$name; echo '
';}sayHello('ZhangSan');sayHello('Lisi');
- Multiple Input parameter functions
Function traceNum ($ a, $ B) {echo 'a = '. $ a.', B = '. $ B .'
'; Echo "a = $ a, B = $ B
"; // This write is also possible} traceNum (2, 3 );
- Functions with return values
function add($a,$b){ return $a+$b;}echo add(10,2).'
';
3. Process control
- Cause of garbled characters: the server encoding method is different from the client encoding method. Solution: add the html header and embed the php code into the body.
Control Flow
= 90) {return 'excellent ';} elseif ($ score> = 80) {return 'good';} elseif ($ score >=70) {return 'good ';} elseif ($ score> = 60) {return 'can';} else {return 'differential';} echo getLevel (92 ).'
';?>
- Control statement syntax 2
Function getLevel_switch ($ score) {switch ($ score/10) {case 10: case 9: return 'excellent '; case 8: return 'good'; case 7: return 'good'; case 6: return' can be '; default: return 'bad' ;}} echo getLevel_switch (60 ).'
';
- Control statement writing 3
Function getLevel_break ($ score) {$ result = '--'; switch (intval ($ score/10) {case 10: case 9: $ result = 'excellent '; break; case 8: $ result = 'good'; break; case 7: $ result = 'good'; break; case 6: $ result = 'OK'; break ;} return $ result;} echo getLevel_break (83 ).'
';
4. Loop
for($i=0;$i<100;$i++){ echo 'Hello '.$i.'
';}
- While loop first judgment and then execution
$i = 0;while($i < 50){ echo 'Hello '.$i.'
'; $i++;}
- The do-while loop is first executed in the judgment
$ I = 0; do {echo 'hello'. $ I .'
'; $ I ++; // do not forget auto-increment; otherwise, an endless loop will be programmed.} while ($ I <20 );
- Break quits* Entire *Loop
for($i=0;$i<100;$i++){ echo 'Hello '.$i.'
'; if( $i == 20 ){ break; }}
- Continue jump out* This time *Loop
for($i=0;$i<100;$i++){ echo 'Hello '.$i.'
';// if( $i == 20 ){// break;// } if(20 == $i){ continue; } echo 'Run here '.$i.'
';}
5. logical operations and & | not!
function traceNum(){ for($i=0;$i<=100;$i++){// if(0 == $i%2 && 0==$i%3){// echo $i.'
';// }// if(0 == $i%2 || 0==$i%3){// echo $i.'
';// } if(!(0 == $i%2)){ echo $i.'
'; } }}traceNum();
Lifecycle 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...