After a basic tutorial on the Web site, there is some understanding of PHP syntax.
Start doing some small projects to practiced hand:
1.1: Generate Random string
Make a page with a text edit box and a button, enter a number in the edit box, click the button to display a randomly generated string on the page, the length of the string is the value entered in the edit box, the string can only contain (0-9,a-z, A-Z).
The code is implemented as follows:
test1.1.php:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<body>
<?php
echo "1.1: Generate random string";
echo "<br>";
echo "Make a page with a text edit box and a button, enter a number in the edit box, click the button to display a randomly generated string on the page, the length of the string is the value entered in the edit box, the string can only contain (0-9,a-z, A-Z). ";
echo "<br>";
?>//reminder, static text is best to use HTML output, do not use PHP
<form action = "<?php echo htmlspecialchars ($_server[" php_self "]);? > "method =" POST ">
Please enter the number:<br>
<input type = "text" name = "Txt1" ><br>
<input type = "submit" value = "commit" name = "BTN1" ><br>
</form>
<?php
$txt 1 = "";
if ($_server["request_method"] = = "POST") {//will be further improved by using JQ for asynchronous invocation, local flush
$txt 1 = testinput ($_post["txt1"]);
if (Is_numeric ($txt 1) && is_int ($txt 1+0) && $txt 1>0)
{
echo "generated". $txt 1. " Bit random code: ";
Echo creatrandom ($txt 1);
}else
echo "Input is not a positive integer";
}
/**//reminders, methods should be formatted with comments
*
* @param $len
*/
function Creatrandom ($len) {//Generate a random code for the corresponding number of digits
$word = $tmp = ";
$chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
while (strlen ($word) < $len) {
$tmp = substr ($chars, (Mt_rand ()%strlen ($chars)), 1); Generate one random code
$word. = $tmp;
}
return $word;
}
function Testinput ($data) {//To process data entered by the user, including removing spaces and backslashes
$data = Trim ($data);
$data = Stripslashes ($data);
$data = Htmlspecialchars ($data);
return $data;
}
?>
</body>
PHP Learning (i)