The tutorial comes from:
PHP Chinese net tutorial
PHP manual (Simplified Chinese) link on PHP official website
if conditional statement
The program runs strictly according to the process
Three basic mechanisms: sequence judgment, condition judgment and cycle structure
if (true) { echo 'really'; } echo '<hr/>'; if (true) { echo 'really'; } else { echo 'false'; } echo '<hr/>'; if(false) { echo 'false'; } elseif (false) { echo 'false'; } else { echo 'really'; } echo <<<'FORM' <form action="" method="post"> //Please enter an integer: <input type="number" name="num"/> <input type="submit" value="Submission"> </form> FORM; //First, test whether the post data passed is empty. if ($_POST['num'] != '') { if ($_POST['num'] % 2 == 0) { echo $_POST['num'].'It's an even number.'; } else { echo $_POST['num'].'It's an odd number.'; } } else { echo 'Please enter a valid number~~'; } // var_dump(gettype($_POST['num']));
switch multi branch structure
Switch (expression){ case value 1: Statement 1; break; case value 2: Statement 2; break; case value 3: Statement 3; break; default: Sentence; }
//Print the corresponding language description according to the language name output by the user echo <<<'LANG' <form action="" method="post"> <datalist id="lang"> <option value="html"/> <option value="css"/> <option value="javascript"/> <option value="php"/> </datalist> <label for="lang">The courses I am studying are:</label> <input type="text" name="lang" id="name" list="lang"> <input type="submit" value="Submission"> </form> LANG; // echo $_POST['lang']; if (isset($_POST['lang'])) { switch ($_POST['lang']) { case 'html': echo 'html Is a hypertext markup language, suitable for writing web page structure.'; break; case 'css': echo 'css Is a style sheet of a web page that controls the display of elements.'; break; case 'javascript': echo 'javascript Is the client scripting language. Control the behavior of web pages and interact with users.'; break; case 'php': echo 'php Is a server-side scripting language.'; break; default: echo 'You have entered an unknown language type:'.$_POST['lang']; break; } } // var_dump($_POST['lang']);
for loop statement
For (loop initial variable; conditional expression; modify loop control variable){
//Statements within a loop
}
//Cyclic summation /* $sum = 0; for($i = 0; $i < 10; $i++) { $sum += $i; } echo '0+1+2+3+4+5+6+7+8+9= '; echo $sum; */ //Table auto generator echo <<<'FORM' <h2>Table auto generator</h2> <form action="" method="post"> //Please input: < input type = "number" name = "rows" > line <input type="number" name="clos">column <input type="submit" value="Submission"> </form> FORM; $rows = isset($_POST['rows']) ? $_POST['rows'] : 0; $cols = isset($_POST['clos']) ? $_POST['clos'] : 0; echo '<table border="1" cellspacing="0" cellpadding="5">'; for($i = 0; $i < $rows; $i++) { echo '<tr>'; for($j=0; $j<$cols;$j++){ echo '<td>'.($i*$cols+$j).'</td>'; } echo '</tr>'; } echo '</table>';
while loop statement
1. Put the initial conditions of the cycle outside
2. Execute when the condition expression returns true
3. There must be a statement to update the circulation condition in the circulation body, otherwise it will enter the dead circulation.
while (Cycle condition) { # code... } */ //Table auto generator echo <<<'FORM' <h2>Table auto generator</h2> <form action="" method="post"> //Please input: < input type = "number" name = "rows" > line <input type="number" name="clos">column <input type="submit" value="Submission"> </form> FORM; $rows = isset($_POST['rows']) ? $_POST['rows'] : 0; $cols = isset($_POST['clos']) ? $_POST['clos'] : 0; echo '<table border="1" cellspacing="0" cellpadding="5">'; $i = 0; while ($i < $rows) { echo '<tr>'; $j=0; while ($j < $cols) { echo '<td>'.($i*$cols+$j).'</td>'; $j++; } echo '</tr>'; $i++; } echo '</table>';
I don't know the number of cycles. I can only use the while loop when judging according to the conditions.
do while loop statement
1. Do not test the condition, first execute a cycle body
2. Repeat when the condition expression is true
3. There should also be statements to update the circulation conditions in the circulation body, otherwise it will enter the dead circulation.
4. Unlike while, the loop body should be executed at least once.
//Table auto generator echo <<<'FORM' <h2>Table auto generator</h2> <form action="" method="post"> //Please input: < input type = "number" name = "rows" > line <input type="number" name="clos">column <input type="submit" value="Submission"> </form> FORM; $rows = isset($_POST['rows']) ? $_POST['rows'] : 0; $cols = isset($_POST['clos']) ? $_POST['clos'] : 0; echo '<table border="1" cellspacing="0" cellpadding="5">'; $i = 0; do { echo '<tr>'; $j=0; do { echo '<td>'.($i*$cols+$j).'</td>'; $j++; }while ($j < $cols); echo '</tr>'; $i++; } while ($i < $rows); echo '</table>'; //Refresh page without input var_dump($rows); var_dump($cols); //int(0) int(0)