PHP Basic Tutorial Seven of the implementation of the calculator

Source: Internet
Author: User
Tags http cookie http post

What this section explains

    • The implementation of the calculator

    • Super Global variables

    • Nested PHP code in HTML

Objective

PHP language is the development of server-side language, is the processing of data, PHP development is unavoidable to interact with the front page, so that data transfer. So how do we get the data from the front desk and pass it to the backstage? They use the HTTP protocol to transmit information. You can see the other side of the blog http://www.php.cn/.

And today's calculator case, design to the data of the front and back data transmission. Its approximate function is to fill in the front page data, submit to the background, the background processing data, and then back to the front desk.

The implementation of the calculator

An HTML page in the foreground calculatingmachine.php

<?php $value = isset ($_get[' value '])? $_get[' value ']: ';? ><! DOCTYPE html>

Front page is the user's input data through post to the background of the numcount.php, in the foreground of the file suffix is PHP end, this is because when the background processing data to the foreground for data display, the most important point, the php suffix file can write HTML code, but the HTML suffix name of the The file can not be written in PHP code (can be set in the configuration file);

Background processing page: numcount.php

<?php//Function require_once './function.php ' of the introduction operation; Get data from an HTML page $num 1 = isset ($_post[' num1 ')?    $_post[' NUM1 ': 0; $num 2 = isset ($_post[' num2 ')?    $_post[' num2 ': 0; $oper = isset ($_post[' oper ')?    $_post[' oper ': ';        Determine if it is a number if (!is_numeric ($num 1) | |!is_numeric ($num 2)) {echo "<script>alert (' Please enter Number ') </script>";    Header ("Refresh:0;url =./calculatingmachine.php");    }//Gets the calculated value $value = 0;            The switch determines the type of operation switch ($oper) {case ' plus ': $value = Plus ($num 1, $num 2);        Break            Case ' Subtract ': $value = Subtract ($num 1, $num 2);        Break            Case ' multiply ': $value = Multiply ($num 1, $num 2);        Break            Case ' pided ': $value = pided ($num 1, $num 2);        Break    Default:echo ";    }//Pass the calculated value to the foreground. Header ("Refresh:0;url =./calculatingmachine.php?value={$value}"); 

The background of the acceptance page, because it is through the POST method of data submission, so you can use the Super global variable $_post[] to get data, and data validation, when not a number, pop-up dialog box prompts, and through the header to jump to the foreground.

The function of manipulating data in the background is encapsulated in a file, which is used by the introduction of a file.

The function of the operation is encapsulated in a file: function.php

<?php    //plus    function Plus ($num 1, $num 2) {        return $num 1 + $num 2;    }    Reduced    function Subtract ($num 1, $num 2) {        return $num 1-$num 2;    }    Multiply    function Multiply ($num 1, $num 2) {        return $num 1 * $num 2;    }    Except    function pided ($num 1, $num 2) {        return $num 1/$num 2;    }

Front page:

Post-processing data is uploaded to the front office:

Super Global variables

In the code above, you can see that the accepted data is received via the Hyper global variable $_post[]. So what are the super global variables in PHP and what do they do?

Many of the predefined variables in PHP are "hyper-global", which means they are available in all scopes of a script. There is no need to perform a global $variable in a function or method; You can access them.

Super-Global classification in PHP:

    • $GLOBALS a globally combined array that contains all the variables. The name of the variable is the key of the array. This means that it is available in all scopes of the script. You do not need to use global $variable in a function or method; To access it.

      <?php$a = 12; Integral type $STR = ' super global variable '; $arr = array (1,2,3,4); Array var_dump ($GLOBALS [' a ']); Echo ' <br> '; var_dump ($GLOBALS [' str ']); Echo ' <br> '; var_dump ($GLOBALS [' Arr ']);

You can see that the Hyper global variable $globals in the above automatically saves the variables in the code.

    • $_server is an array of information, such as header information (header), path, and script location (scripts locations), and so on. The items in this array are created by the WEB server. There is no guarantee that each server will provide all the items, the server may ignore some, or provide some items that are not listed here.
      This hyper-global variable is also related to the HTTP protocol, in which we can get some information about the data we are transmitting.

      <?php    echo $_server[' server_addr '];    ...... The result ...    .. 127.0.0.1

      The above is just one of the values in the server, you get the IP address, and the other value can see the help document.

    • $_get[] An array of variables passed to the current script through a URL parameter. The transmission data on the Web page is, commonly used in two ways, a get and a post, and this super global variable is to save the value passed by the Get method

    • $_post[] An array of variables passed to the current script via the HTTP post method, which is accepted when passing data by post

    • $_request[], in the above two super global variables store different values according to different delivery methods, and this variable is the value of the two passes will be saved.

The following hyper-global variables will be introduced in the future

    • $_files[] File upload variable, an array of items uploaded to the current script via the HTTP POST method. In the back of the file upload, then detailed introduction.

    • $_cookie[] An array of variables passed to the current script by means of an HTTP cookie

    • $_session[] The current script can be used as an array of SESSION variables. More information on how to use it can be learned through the Session function.

The above is the super-global variables in PHP, in the development we will always deal with them.

Embed PHP code in HTML

In the front code of the calculator above, we can see that when the data is processed and delivered to the foreground, the data is displayed by writing the PHP code in the P tag. From here we can see how PHP is nested in HTML code.

    <?php code?>

The development here is the PHP code and HTML code nesting, data and page development together, what mode is useless.

Summarize

The case of the computer contains almost all the previous studies, and all of them are used for their own use. Learn to use the knowledge you have learned.

The above is the PHP Basic tutorial seven of the implementation of the contents of the calculator, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.