PHP Learning (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.