In this paper, for beginners to learn, judgment is the basic grammar in programming is also the most important. This article through to submit the data judgment instance, makes the novice familiar with PHP the judgment application.
Two basic points of knowledge:
1, Security factor: How to prohibit non-submission access?
= = Whether a page is submitted by using the name value of the Submit button in the Post data
2, data type conversion: The year number of the form form submission, found that PHP received after the string type, how to convert?
= = converted automatically to int type in operation by ' 2000 ' +0
Suppress ' illegal access ' prompt when uncommitted (empty ($_post)) {echo ' Enter year '; return;}//Do not click the Submit button to access the IF (Isset ($_post[' Chaxun ')) { $year = $_post[' year '];} else{ echo ' illegal access '; return;} At this point the data type of the post is the string type ' 2000 ', which determines whether it is numeric and is converted to a numeric type if (Is_numeric ($year)) {$year = $year + 0) by $year+0 Else{die (' illegal input ');//NOTE: Test data type echo GetType ($year);//At this time $year is a number type, judging whether it is an integer if (Is_int ($year)) { //main program if ($year <1000 or $year >9999) {die (' Out of Query range '); } ElseIf ($year%4 = = 0 && $year%100!=0 | | $year%400==0) { echo $year. ' is a leap year '; } Else{echo $year. ' is common year ';}} Else{echo ' Please enter a four-digit integer number ';}
or method Two: Strpos ($year, '. ') is not allowed to have decimal points, indirectly judging whether it is an integer/*if (is_numeric ($year) &&!strpos ($year, '. ')) { if ($year%4 = = 0 && $year%100!=0 | | $year%400==0) { echo $year. ' is a leap year '; } else{ echo $year. ' is common year '; }} else{ echo ' Please enter an integer number ';} */
Related reading:
First knowledge PHP basic grammar PHP7 Syntax PHP development 1200 cases sublime PHP grammar check
PHP Syntax (Syntax)
PHP Basic Tutorial 2 php7 PHP Environment build PHP from beginner to proficient