PHP Study Notes 1-constants, functions, php Study Notes 1-constants
Constant: the const (php5) statement can only be assigned once. define is used for versions earlier than php5;
1 <? Php2 const THE_VALUE = 100; // const3 echo THE_VALUE in PHP5; 4 5 define ('the _ value', 200 ); // earlier versions than PHP5 can use define6 echo THE_VALUE;
Function: contains a lot of functional code blocks;
Advantage: easy to call elsewhere
1 <? Php 2 3 function traceHelloPHP () {4 echo 'Hello PHP! '; 5 echo' <br/> '; 6 echo 'Hello World! '; 7 echo' <br/> '; 8} 9 traceHelloPHP (); 10 11 // another method of function execution 12 $ func = 'tracehellophp '; // pass the function as a parameter, e.g. callback Method 13 $ func (); 14 15 // input parameter of the function -- single input parameter 16 function sayHelloTo ($ name) {17 echo 'Hello '. $ name. '<br/>'; 18} 19 sayHelloTo ('vito '); 20 21 // input parameters of the function -- multiple input parameters 22 function traceNum ($ a, $ B) {23 // echo 'a = '. $. ', B = '. $ B. '<br/>'; 24 echo "a = $ a, B = $ B"; // simpler syntax 25} 26 traceNum (2, 3 ); 27 28 function add ($ a, $ B) {29 return $ a + $ B; // return value 30} 31 echo add (10, 2); View Code