PHP input and output

Source: Internet
Author: User

PHP uses forms to accept data, echo output data, and $ to define variables.

<?php echo $_post["Sub"]; Post is the name of the form?><formAction=""Method= "POST"> <!--The action is the address of the receiving form, and if it is empty, it is received on the current page and method is the -<inputtype= "text"name= "Sub"/><inputtype= "Submit"value= "Submit" />   <!--Submit Refresh Page -</form> <!--The program flow is: First go through PHP, when the post is empty, then walk the form form, write data, after submission, refresh the page, the post received data displayed in the new page -

Example 1. Summing sum

<formAction=""Method= "POST"><inputtype= "text"name= "a" /><inputtype= "Submit"value= "Calculation" /></form><?php$n=$_post[' a ']; $sum; for ($i =1; $i <= $n; $i + +) {$sum + = $i;} echo $sum;?>

Example 2: Finding the sum of factorial and factorial

<formAction=""Method= "POST"><inputtype= "text"name= "B" /><inputtype= "Submit"value= "Calculation" /></form><?Php$s=1, $sum =0, $n =$_post[' B '];for ($i =1; $i <= $n; $i + +) {$s *= $i; $sum + = $s;} echo "factorial is $s, factorial sum is $sum";?>

Example 3. List of prime numbers within 100

<? phpfor ($i =2; $i <=100; $i + +) {    $k =0;    for ($j =2; $j <= $i; $j + +)    {        if ($i% $j ==0)        {            $k + +;                }            }    if ($k ==1)    {        echo $i; echo "&nbsp;"}    } ?>

Example 4. Finding the root of the ax^2+bx+c=0 of a unary two-time equation

<formAction=""Method= "POST">Please enter the value of a<inputtype= "text"name= "a" />Please enter the value of B<inputtype= "text"name= "B" />Please enter a value for C<inputtype= "text"name= "C" />  <inputtype= "Submit"value= "Calculation" /></form><?php$a=$_post[' a ']; $b =$_post[' B ']; $c =$_post[' C ']; $de = ($b * $b -4* $a * $c); if ($a ==0) {echo "This equation is not a two-time equation!). Re-enter"; }else{echo "This equation is a unary two-time equation!";        echo "<br/>";        if ($de >=0) {$x 1= (-$b +sqrt ($de))/($a);        $x 2= (-$b-sqrt ($de))/($a);            if ($de >0) {echo "This equation has two different real roots"; echo "<br/>"; echo "x1= $x 1"; echo "&nbsp;";        echo "x2= $x 2";            } else {echo "This equation has two identical real roots"; echo "<br/>";        echo "x1=x2= $x 1";    }} else {echo "This equation has no real roots!"; }    }?>

Example 5: Enter gender, height, weight to see if it is a standard weight

<formAction=""Method= "POST">Please enter gender<inputtype= "text"name= "B"  />Please enter height (cm)<inputtype= "text"name= "C"/>Please enter weight (kg)<inputtype= "text"name= "D" /><inputtype= "Submit"value= "Submit" /></form><?php$b=$_post[' B ']; $c =$_post[' C '); $d =$_post[' d '];if ($b = = "Male") {$n = ($d-$c +100);    if ($n >=-3&& $n <=3) {echo "Your weight is the standard weight";    } else if ($n >3) {echo "You need to lose weight!";    } else {echo "You need to increase nutrition!";    }}else if ($b = = "female") {$n = ($d-$c +110);    if ($n >=-3&& $n <=3) {echo "Your weight is the standard weight";    } else if ($n >3) {echo "You need to lose weight!";    } else {echo "You need to increase nutrition!"; }}else{echo "input wrong!";}?>

Example 6. Enter three numbers, sort from large to small

<formAction=""Method= "POST">Please enter the first number<inputtype= "text"name= "a" />Please enter a second number<inputtype= "text"name= "B" />Please enter a third number<inputtype= "text"name= "C" /><inputtype= "Submit"value= "Sort" /></form><?php $x =$_post[a]; $y =$_post[b]; $z =$_post[c];if ($x < $y) {$zhong = $x;    $x = $y; $y = $zhong;}    if ($x < $z) {$zhong = $x;    $x = $z; $z = $zhong;}    if ($y < $z) {$zhong = $y;    $y = $z; $z = $zhong;} echo $x; echo "&nbsp;"; echo $y; echo "&nbsp;"; Echo $z; ?>

PHP input and output

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.