chapter2PHPTUT-nicephotog

download chapter2PHPTUT-nicephotog

of 1

Transcript of chapter2PHPTUT-nicephotog

  • 8/9/2019 chapter2PHPTUT-nicephotog

    1/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 toother data types such as numbers and binary data that can be likened to text(human readable symbols) and because that ithas 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 torecognise how to display them, and later in a section of this tutorial about how to sort them and recognise sets of themextremely 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 thatis 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 andits 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 filesystems existed the 8 bit byte including 0(zero) cannot hold more than the the numeric integer value 255. Since the introductionof UTF-16 and unicode charset(short for Character set) (among other human languages such as Russian and Chinese or Hindi or Thaithat's alphabet is somewhat large)with the larger file systems for disk geometry 2 byte 16 bit there have been different actionsrequired 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. Thereason 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 knownas ISO-646 , others are ISO-1252 , ISO-8859-1 , Latin-1. The most common is ISO-1252 that contains much of the United States EnglishASCII 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 andalmost never binary. Some special characters in any operating system inside text are 2byte 16 bit always of the start ofthis tutorial showed how to say to PHP to print a new line character to Standard Output using the escape character and alowercase n. The main uses for manipulating character sets is either cryption of messages such as base 64 email attachmentsor 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

    characters-php.php

    In much of web programming 2byte 16 bit characters are not common and are more related to programming itself than actualsymbol 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 thetype of encoding in use to make a transaction with a server or application. One such encoding of importancehere is called URL encoding. URL encoding is a process of assigning a hexadecimal value to a symbol fortransfer 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 parametersto an encoding that contains in syntax a % modulus sign in front of 2 hexadecimal digits that are notread 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 pointof the request-text-string for its own uses but all of it will be passed to the PHP application. In PERL decodingor any custom encoding is always done by the programmer but PHP is designed to ease that process and returnsthe information decoded ready for use in a script though it does have a set of pre-defined functions PERLuses to operate on the returned raw data. Those functions are pack() and unpack(). As shown previously andin 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 languageor want to use a foreign language. All major script engines and particularly web engines as servers oroperating systems have not only a code page but a locale. A locale has a descriptor and defaultcharacter set that matches the symbols used by the language or dialect for text. In both the page outputand the php.ini configuration file the locale for the PHP application can be set. This will result in theoutput 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 localetext output finally if over the internet are the user browser settings and operating system but mainly anydeclaration 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 dataafter it is obtained. Decoding the request data involves for other languages reconstructing from the hexadecimalURL encoding but not in PHP. In other languages a test for a url-end following a question mark not being 0 lengthor being null is required to determine if the request is a POST or GET request before decoding. PHP ismuch 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 GETrequest 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 desiredform 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 datafrom.In Chapter three some tools to assist testing and making decisions about data and then channelling program flowwill be introduced.

    The next script will show you the simple operation of a GET request and how to trigger it from the browser aswas told to you in a previous paragraph. Also obtain the variables from the first complete scriptstandard-html-template.php that make the html page tags(including the title and page end print() call) for usein 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 thesent 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 signin 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

    Another feature of PHP is its ease of handling file uploading. To do this the web server PHP script engine simplyhas 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 operateit 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 willbe used to extend this script to a simple form script that can decide if the user has filled in the form and then toprocess it if they have filled it in or simply print the form for their first use of it.

    http://nicephot.xlphp.net/chapter3PHPTUT.htmlhttp://nicephot.xlphp.net/chapter3PHPTUT.html