Php from entry to discard series-02. php Demo,-02. phpdemo
Php from getting started to giving up series-02. php basic syntax 1. Learning syntax, starting from hello world
PHP (full name: PHP: Hypertext Preprocessor, that is, "PHP: Hypertext Preprocessor") is a common open source scripting language.
<! DOCTYPE html> <body> <? Phpecho "hello world! "; // Output?> </Body>
Php is a server-side scripting language that is embedded into html documents for execution. PHP scripts are executed on the server and then the pure HTML results are sent back to the browser. Write the php language.<? PhpStart?>End.
Ii. php Variables
<?php $x=5; $y=6; $z=$x+$y; echo $z;?>
Iii. php Data Type
String, Integer, Float, Boolean, Array, Object, NULL ).
Note: The var_dump () function returns the data type and value of the variable.
<? Php $ x = 5985; var_dump ($ x); echo "<br>"; $ x =-345; // negative var_dump ($ x ); echo "<br>"; $ cars = array ("Volvo", "BMW", "Toyota"); var_dump ($ cars); echo "<br> "; $ x = "Hello world! "; $ X = null; var_dump ($ x); class Car {var $ color; function Car ($ color =" green ") {$ this-> color = $ color;} function what_color () {return $ this-> color }}?>
Iv. php process control statements
Three process control statements: sequential structure, branch structure, and cyclic structure
1. Branch Structure:
- If statement-Run the code when the condition is set.
- If... else statement-Execute one piece of code when the conditions are met. If the conditions are not met, execute another piece of code.
- If... else statement-Execute a code block when several conditions are created.
- Switch statement-Execute a code block when several conditions are created.
<? Php $ favcolor = "red"; switch ($ favcolor) {case "red": echo "the color you like is red! "; Break; case" blue ": echo" the color you like is blue! "; Break; case" green ": echo" the color you like is green! "; Break; default: echo" your favorite colors are not red, blue, or green! ";}?>
2. Loop Structure
- While-As long as the specified condition is true, the code block is executed cyclically.
- Do... while-Execute the code block once, and then repeat the loop when the specified condition is set.
- For-Number of times the code block is executed cyclically
- Foreach-Loop the code block based on each element in the array
For (initial value; condition; increment) {code to be executed ;}
Foreach ($ array as $ value) {code to be executed ;}
The foreach loop should be different from other languages. In php, the array to be cyclic should be placed before, as interval, and scalar should be placed behind
Output:
onetwothree
OK. php basic syntax learning is complete!