Sunteți pe pagina 1din 1

Chapter 3 PHP program controls

(Chapter 2) More about handling text data in PHP

Text machine data Numerics

As i said before about strings of text and assignment, Strings(text data) is to a programming language a data type.
A data type is at bare minimum inside a machine simply a set of assigned switches in memory representing ones and zeros.
How the machine knows instructions from text and from binary data is not important here but simply that it is different to
other data types such as numbers and binary data that can be likened to text(human readable symbols) and because that it
has its own set of characteristics that can be manipulated at the base machine binary level.
The symbols you can see displayed on a screen each have a binary numeric code inside the machine for the machine to
recognise how to display them, and later in a section of this tutorial about how to sort them and recognise sets of them
extremely deliberately called a regular expression operation.
The machine itself that operates on the data(usually the server) has a special pre-set character set called a code page that
is set as the default code page in its Operating System and often for all its applications such as the PHP engine.
PHP has a special configuration file called php.ini that contains various settings to customise the PHP application and
its web server interaction.
The code page has a set of characters (around 200 to 256 of them) each numbered through with exception of some numbers to 255.
The reason they do not exceed 255 is because an 8 bit byte when computing was in early stages before 16 and 32 bit byte file
systems existed the 8 bit byte including 0(zero) cannot hold more than the the numeric integer value 255. Since the introduction
of UTF-16 and unicode charset(short for Character set) (among other human languages such as Russian and Chinese or Hindi or Thai
that's alphabet is somewhat large)with the larger file systems for disk geometry 2 byte 16 bit there have been different actions
required in programming to handle the text data continually rather than assume an 8 bit byte Character set code page in operation.
To make things easier for a programmer since text and computing have a huge relation in information conveyance and usage some
pre defined functions are built into the language to allow extraction or creation of characters numerically and set them. The
reason becomes clearer when you handle text from a form page from your site in CGI(Common Gateway Interface) to create types of
email messages or convert to a text charset type utilised by your organisation. The standard base English character set is known
as ISO-646 , others are ISO-1252 , ISO-8859-1 , Latin-1. The most common is ISO-1252 that contains much of the United States English
ASCII standard character set.
When handling text and reading specifications often a special character will be given in Hexadecimal(base 16)
sometimes in Octal(base 8) but in web programming it is often referred to in base ten(Decimal) as a decimal integer and
almost never binary. Some special characters in any operating system inside text are 2byte 16 bit always of the start of
this tutorial showed how to say to PHP to print a new line character to Standard Output using the escape character and a
lowercase n. The main uses for manipulating character sets is either cryption of messages such as base 64 email attachments
or digestible signed email messages that have security cryption alike to server certificates.
PHP has some useful functions of its own such as iconv() , chr() , ord() , ctype_xdigit() , ucfirst() , setlocale() ,
iconv_mime_encode() , strpbrk() , is_numeric() , bindec() , decoct() , dechex() and base_convert().

Make a file with the below code called characters-php.php

<html>
<head>
<-- characters-php.php --> <title>characters-php.php</title>
</head>
<body>
<?php
$hex_number='000071';
$decimal_number=0;
$binary_number="";
print("<p>In decimal: ".($decimal_number=hexdec($hex_number))."<p>");
print("In binary: ".($binary_number=decbin($decimal_number))."<p>");
print("back to a hexadecimal number: ".(dechex(("".bindec($binary_number))))."<p>");
print('$hex_number was a string and was: '.$hex_number."<p>");
print("The ASCII value is the lowercase letter ".chr($decimal_number));
?>
</body>
</html>
</td>
</tr>
</table>

In much of web programming 2byte 16 bit characters are not common and are more related to programming itself than actual
symbol output for text. Mainly they are \n (unix/linux) new line , \r\n (DOS,windows) new line and \r (Mac PPC) new-line.

URL encoding

But the more important part of this again about characters and number coding for the machine is the
type of encoding in use to make a transaction with a server or application. One such encoding of importance
here is called URL encoding. URL encoding is a process of assigning a hexadecimal value to a symbol for
transfer of data by HTTP or FTP protocol in a request and less often in a response.
It involves converting special characters that would interfere potentially with the request parameters
to an encoding that contains in syntax a % modulus sign in front of 2 hexadecimal digits that are not
read by the server or application as integer number data but string(text) as a data type value.
i.e. %40 is the symbol for @ (address sign often called at). The backslash operator is another as many
text character symbols that are not alpha-numeric are converted to that encoding when sent from a browser.
When information is sent from a browser to the server the server must then decide the termination point
of the request-text-string for its own uses but all of it will be passed to the PHP application. In PERL decoding
or any custom encoding is always done by the programmer but PHP is designed to ease that process and returns
the information decoded ready for use in a script though it does have a set of pre-defined functions PERL
uses to operate on the returned raw data. Those functions are pack() and unpack(). As shown previously and
in the script many functions not available to PERL exist but ord() and chr() are functions PERL has also.

Locale

When building a script and php pages it may be that you want to use a dialect of the English language
or want to use a foreign language. All major script engines and particularly web engines as servers or
operating systems have not only a code page but a locale. A locale has a descriptor and default
character set that matches the symbols used by the language or dialect for text. In both the page output
and the php.ini configuration file the locale for the PHP application can be set. This will result in the
output committing interpretation of each byte as it would number for number to the set code page for the
locale regardless the language and editor the text was written as. Other determining factors of locale
text output finally if over the internet are the user browser settings and operating system but mainly any
declaration in the output html of the php page of a locale.

CGI data, GET and POST request system in PHP

In most web server side languages it is required to both test the CGI form method and to decode the data
after it is obtained. Decoding the request data involves for other languages reconstructing from the hexadecimal
URL encoding but not in PHP. In other languages a test for a url-end following a question mark not being 0 length
or being null is required to determine if the request is a POST or GET request before decoding. PHP is
much simpler again and uses a standard special set of variables and functions to achieve its simplicity.
Quickly here though, a POST request is a limitless data size for the send from a form to the sever and a GET
request is limited to around 1.5 Mega-byte in size and is less secure because its information is shown in the
browser address bar. Anything part of the http protocol request requires http protocol url-encoding of the (if any)
request extra content but is usually and almost always done automatically by the browser application.
To obtain form request information in PHP, a special set of PHP pre-defined variables are used, of, for GET, a
languages standard server variable called $_SERVER["QUERY_STRING"] is used or it can simply have the desired
form field part directly stripped from the incoming data relating the request type $_SERVER['REQUEST_METHOD']
GET/POST/PUT/HEAD by a more direct PHP special variable $HTTP_GET_VARS['emailaddress'] (POST equivalent:
$HTTP_POST_VARS['emailaddress']). The 'emailaddress' name is the field name/id in the form to obtain the data
from.
In Chapter three some tools to assist testing and making decisions about data and then channelling program flow
will be introduced.

The next script will show you the simple operation of a GET request and how to trigger it from the browser as
was told to you in a previous paragraph. Also obtain the variables from the first complete script
standard-html-template.php that make the html page tags(including the title and page end print() call) for use
in this script.
To properly run the script you must use this syntax in the request on the address bar of your browser.
note the bold section that would be the name of the form field picked up from the from.The start of the
sent content(form content) that is not machine request data signified by the ? mark that
precedes it. Also while %20 indicates the space symbol(an escaped character) it is normally written as a + plus sign
in the address bar and is the symbol normally decoded.

http://nicephot.xlphp.net/simple-getrequest.php?sometext=sometext%20th%40t%20will%20print%20to%20the%20browser%20from%20this%20request%20call

<?php
############## simple-getrequest.php ######################
$page_title="my pages title"; # the words my pages title can be changed to any text you want
$html_head_begin = "<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>";
print($html_head_begin.$page_title);
print('</title></head><body><center><p>'."\n"."<br>");
print('Below is the text you sent in the address bar:<p>');
print($HTTP_GET_VARS['sometext']);
print('</p></center></body></html>');
?>

Another feature of PHP is its ease of handling file uploading. To do this the web server PHP script engine simply
has a special file variable to collect the data from, and again, but not as simply, it also has a mail()
pre-defined function to use for sending form or other data to an email address. For the mail() function to operate
it must be correctly configured to the server inside the php.ini file because the actual mailing system is another
program alike to configuring to use a DataBase driver for a database.

In the next chapter will be some special program control operators and methods of program flow channeling that will
be used to extend this script to a simple form script that can decide if the user has filled in the form and then to
process it if they have filled it in or simply print the form for their first use of it.

S-ar putea să vă placă și