☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

The converter as a PHP web page

الأسبوع 7 · الوحدة ١ الموضوع الثالث: محوّل الأعداد كصفحة ويب بلغة PHP: النموذج وأدوات التحكم وكود PHP وعرض النتيجة · 3 دروس
2

PHP behind the page: isset and the ready converters

ICT Secondary 3 Term 1, Unit 1, Topic 3: pp. 37–50
GoalRead the PHP block of the converter: isset for each button and the four ready converters.
New words
PHP block · code between <?php and ?> that the server runs before the page is sent$_POST · the list of values that the form sent, found by the name of each controlisset · asks whether a value exists; for a button name it is true only if that button was pressedecho · puts a value into the page
Explanation

PHP code sits between <?php and ?>. The server runs it before the page is sent, so the visitor never sees it. The values of the form arrive in $_POST under the names we gave: $_POST['number'] is the typed text. When the page is opened for the first time nothing was sent. When a button is pressed its name arrives too, so isset($_POST['btnBin']) is true only if that button was pressed, and each button gets its own if block. Inside the block the code reads the typed number and fills four variables: the typed text, and the binary, decimal and hexadecimal results. Four ready functions do the conversions: bindec reads binary text and gives a number, decbin writes a number in binary, hexdec reads hexadecimal text and dechex writes a number in hexadecimal. None of these four goes straight from binary to hexadecimal, so the page goes through decimal: bindec first, then dechex. The ready functions write hexadecimal in lower case.

FunctionReadsGivesExample
bindecbinary texta numberbindec("1101") = 13
decbina numberbinary textdecbin(90) = 1011010
hexdechexadecimal texta numberhexdec("3C") = 60
dechexa numberhexadecimal text, lower casedechex(200) = c8
Worked examples
Php
1<?php
2$_POST = ['btnBin' => 'Typed number is binary', 'number' => '1101'];   // pretend the form was sent
3$typed = "";
4$bin = "";
5$dec = "";
6$hex = "";
7
8if (isset($_POST['btnBin'])) {
9    $typed = $_POST['number'];
10    $dec = bindec($typed);
11    $bin = $typed;
12    $hex = dechex($dec);
13}
14echo "$bin $dec $hex";
Output
1101 13 d
The block for the binary button alone, with the form pretended by the first line. The visitor typed 1101 and pressed the binary button.
Php
1<?php
2$typed = "";
3$bin = "";
4$dec = "";
5$hex = "";
6
7if (isset($_POST['btnBin'])) {
8    $typed = $_POST['number'];
9    $dec = bindec($typed);
10    $bin = $typed;
11    $hex = dechex($dec);
12}
13if (isset($_POST['btnDec'])) {
14    $typed = $_POST['number'];
15    $dec = $typed;
16    $bin = decbin($dec);
17    $hex = dechex($dec);
18}
19if (isset($_POST['btnHex'])) {
20    $typed = $_POST['number'];
21    $dec = hexdec($typed);
22    $bin = decbin($dec);
23    $hex = $typed;
24}
25?>
The whole PHP block of the page, one if block for each button. In the hexadecimal block the page shows the typed text as the hexadecimal result.
ButtonTypedBinaryDecimalHexadecimal
btnBin1101110113d
btnDec901011010905a
btnHex3C111100603C
Three runs of the page, written as a table. In the last run the hexadecimal result is shown as typed, in capitals, and the binary result comes from decbin.
Class activity

Predict on paper the three results when the visitor types 200 and presses the decimal button, and then when the visitor types B4 and presses the hexadecimal button. Check each answer by converting back.

Practice
1
What does isset($_POST['btnHex']) tell the page?
2
Why does the page use bindec and then dechex to turn binary into hexadecimal?
3
Where does PHP get the typed text from?
4
What does dechex(75) give?