Learning PHP.PHP (PHP Hypertext Preprocessor) A scripting language used to create dynamic Web pages. With syntax from C, Java and Perl, PHP code is embedded within HTML pages for server side execution. Definitions--A selection * PHP is a server-side, cross-platform, HTML HTML in full HyperText Markup Language Markup language derived from SGML that is used to prepare hypertext documents. Relatively easy for nonprogrammers to master, HTML is the language used for documents on the World Wide Web. embedded scripting language A high-level programming, or command, language that is interpreted (translated on the fly) rather than compiled ahead of time. A scripting, or script, language may be a general-purpose programming language or it may be limited to specific functions used to augment the running of an that lets you create dynamic web pages A Web page that is returned to the user with custom content based on the results of a search or some other request. Also known as "dynamic HTML" or "dynamic content," the "dynamic" word is used with Web sites to refer to custom results individualized to each user in contrast to the . PHP-enabled web pages are treated just like regular HTML pages and you can create and edit them the same way you normally create regular HTML pages. * Self-referentially short for PHP: Hypertext Preprocessor (World-Wide Web, programming) PHP: Hypertext Preprocessor - (PHP) An Open Source, server-side, cross-platform, scripting language used to create dynamic web pages. PHP can be embedded in HTML using special tags like: Hello World '; ?> , an open source, server-side, HTML embedded scripting language used to create dynamic Web pages, In an HTML document, PHP script (similar syntax to that of Perl or C ) is enclosed within special PHP tags. Because PHP is embedded within tags, the author can jump between HTML and PHP (similar to ASP and Cold Fusion cold fusion or low-temperature fusion, nuclear fusion of deuterium, an isotope of hydrogen, at or relatively near room temperature. Fusion, the reaction involved in the release of the destructive energy of a hydrogen bomb, requires extremely ) instead of having to rely on heavy amounts of code to output HTML. And, because PHP is executed on the server, the client cannot view the PHP code. PHP can perform any task that any CGI program CGI program - Common Gateway Interface can. * PHP is a recursive acronym An acronym that is also one of the primary words in the definition. For example, GNU stands for "GNU's Not Unix." Wine means "Wine Is Not an Emulator." See recursion, GNU and Wine. for "PHP Hypertext Preprocessor'. It is an open source, interpretive, HTML centric, server side scripting language. PHP is especially suited for Web development and can be embedded into HTML pages. Also see PSP (PlayStation Portable) See PlayStation. , JSP (JavaServer Page) An extension to the Java servlet technology from Sun that allows HTML to be combined with Java on the same page. The Java provides the processing, and the HTML provides the layout on the Web page. and ASP. * PHP: Hypertext Preprocessor is an open source server side programming language extensively used for web scripts and to process data passed via the Common Gateway Interface from HTML forms etc. PHP can be written as scripts that reside on the server and may produce HTML output that downloads to the web browser The program that serves as your front end to the Web on the Internet. In order to view a site, you type its address (URL) into the browser's Location field; for example, www.computerlanguage.com, and the home page of that site is downloaded to you. . Alternatively, PHP can be embedded within HTML pages that are then saved with a php file extension. The PHP sections of the page are then parsed by the PHP engine on the server and the PHP code stripped out before the page is downloaded to the web browser PHP--A simple tutorial Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic webpage creation with PHP, though PHP is not only capable of creating webpages. PHP-enabled web pages are treated just like regular HTML pages and you can create and edit them the same way you normally create regular HTML pages. What do I need? In this tutorial we assume that your server has activated support for PHP and that all files ending in. php are handled by PHP. On most servers, this is the default extension for PHP files, but ask your server administrator to be sure. If your server supports PHP, then you do not need to do anything. Just create your php files, put them in your web directory and the server will automatically parse them for you. There is no need to compile anything nor do you need to install any extra tools. Think of these PHP-enabled files as simple HTML files with a whole new family of magical tags that let you do all sorts of things. Most web hosts offer PHP support. Let us say you want to save precious bandwidth and develop locally, In this case, you will want to install a web server, such as Apache, and of course PHP. You will most likely want to install a database as well, such as MYSQL. Locate a pre-configured package for your operating system operating system (OS) Software that controls the operation of a computer, directs the input and output of data, keeps track of files, and controls the processing of computer programs. , which automatically installs all of these with just a few mouse clicks. It is easy to setup a web server with PHP support on any operating system, including MacOSX, Linux and Windows. On Linux, you may find rpmfind and PBone helpful for locating RPMs. You may also want to visit apt-get to find packages for Debian. Your first PHP-enabled page Create a file named hello, php and put it in your web servevs root directory (DOCUMENT_ROOT) with the following content: Example 2-1. Our first PHP script: hello.php <html> <head> <title>PHP Test</title> </head> <body> <?php echo 'Hello World'; ?> </body> </html> Use your browser to access the file with your web server's URL URL in full Uniform Resource Locator Address of a resource on the Internet. The resource can be any type of file stored on a server, such as a Web page, a text file, a graphics file, or an application program. , ending with the "/hello.php" file reference. When developing locally this URL will be something like http://localhost/hellophp or http://12 7.0.0.1/hello.php but this depends on the web server's configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
Hello World
</body>
</html>
This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo() statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the ".php' extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things. If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured property. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many PHP support options. The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML file like this anywhere you want. A Note on Text Editors- There are many text editors and Integrated Development Environments (IDES) that you can use to create, edit and manage PHP flies. A Note on Word Processors: Word processors such as StarOffice Writer, Microsoft Word A full-featured word processing program for Windows and the Macintosh from Microsoft. Included in the Microsoft application suite, it is a sophisticated program with rudimentary desktop publishing capabilities that has become the most widely used word processing application on the market. and Abiword are not optimal for editing PHP files. If you wish to use one for this test script The instructions in a test program. It defines the actions and pass/fail criteria. For example, if the action is "to enter a valid account number," the expected result is that the data are accepted. Entering an invalid number should yield a particular error message. See test case. , you must ensure that you save the file as plain text or PHP will not be able to read and execute the script. A Note on Windows Notepad The text editor that comes with Windows. It is a very elementary utility, but gets the job done most of the time. See text editor and WordPad. (text, tool) Notepad - The very basic text editor supplied with Microsoft Windows. : If you are writing your- PHP scripts using Windows Notepad, you will need to ensure that your files are saved with the php extension. (Notepad adds a txt extension to files automatically unless you take one of the following steps to prevent it.) When you save the file and are prompted to provide a name for the file, place the filename file·name also file name n. A name given to a computer file to distinguish it from other files, often containing an extension that classifies it by type. in quotes (i.e. 'hello. php"). Alternatively, you can click on the'Text Documents'drop-down menu in the'Save dialog box A movable window that is displayed on screen in response to the user selecting a menu option. It provides the current status and available options for a particular feature in the program. and change the setting to 'All Files". You can then enter your filename without quotes. Something Useful Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string A text field in an HTTP request header that contains the name and version of the Web browser. See HTTP request header, user agent and user agent spoofing. the browser smds as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER ['HTTP_USER_AGENT']. Note: $ SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS VARS Value Added Reseller VARS Vehicle Accounting and Reporting System (US Immigration and Naturalization Service) VARS Vertical and Azimuth Reference System VARS Variant Stem VARS Variables arrays instead, such as $HTTP_SERVER_VARS. Although deprecated See deprecate. deprecated - Said of a program or feature that is considered obsolescent and in the process of being phased out, usually in favour of a specified replacement. Deprecated features can, unfortunately, linger on for many years. , these older variables still exist. To display this variable, you can simply do: Example 2-3. Printing a variable (Array element) <?php echo $_SERVER['HTTP_USER_AGENT']; ?> A sample output of this script may be: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be useful $-SERVER is just one variable that PHP automatically makes available to you. A list can be seen in the Reserved Variables section of the manual or you can get a complete list of them by looking at the output of the phpinfo()function used in the example in the previous section. You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer Microsoft's Web browser, which comes with Windows starting with Windows 98. Commonly called "IE," versions for Mac and Unix are also available. Internet Explorer is the most widely used Web browser on the market. It has also been the browser engine in AOL's Internet access software. you can do this: Example 2-4. Example using control structures and functions
<?php
if (strpos ($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo 'You are using Internet Explorer.<br />';
}
?>
A sample output of this script may be: You are using Internet Explorer.<br /> Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up an introductory PHP book and read the first couple of chapters. The second concept we introduced was the strpos() function call. strpos() is a function built into PHP which searches a string for another string. In this case we are looking for Looking for In the context of general equities, this describing a buy interest in which a dealer is asked to offer stock, often involving a capital commitment. Antithesis of in touch with. MSIE' (so-called needle) inside $_SERVER ['HTTP_USER_ AGENT] (so-called haystack). If the needle is found Hi inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns FALSE. If it does not return FALSE, the if expression evaluates to TRUE and the code within its (braces) is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other Fictions such as strtoupper() and strlen(). Each related manual page contains examples too. We can take this a step further and show how you can 'ump in and out of PHP mode even in the middle of a PHP block: Example 2-5. Mixing both HTNM HTNM Hollinger Telegraph New Media HTNM High-Tech and New Media (Club) HTNM High Titer Non Murine and PHP modes
<?php
if (strpos ($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<h3>strpos () must have returned non-false</h3>
You are using Internet Explorer
<?php
} else {
?>
<h3>strpos () must have returned false</h3>
You are not using Internet Explorer
<?php
}
?>
A sample output of this script may be: <h3>strpos () must have returned non-false</h3> You are using Internet Explorer Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos() In other words Adv. 1. in other words - otherwise stated; "in other words, we are broke" put differently , it depends on whether the string MSIE See Internet Explorer. MSIE - Internet Explorer was found or not. Dealing with Forms One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts. Here is an example HTML form: Example 2-6. A simple HTML form <form action="action.php" method="post"> Your name: <input type="text" name="name" /> Your age: <input type="text" name="age" /> <input type="submit" /> </form> There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action, php page is called. In this file you would write something like this: Example 2-7. Printing data from our form Hi <?php echo $_POST['name']; ?>. You are <?php echo $_POST['age']; ?> years old. A sample output of this script may be: Hi Joe. You are 22 years old. It should be obvious what this does. There is nothing more to it. The S_POST [name] and $_POST [age] variables are automatically set for you by PHP. Earlier we used the $_SERVER autoglobal; above we just introduced the $_POST autoglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST autoglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data. You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them. USING old code with new versions of PHP Now that PHP has grown to be a popular scripting language, there are a lot of public repositories and libraries containing code you can reuse. The PHP developers have largely tried to preserve backwards compatibility backwards compatibility - backward compatibility , so a script written for an older version will run (ideally) without changes in a newer version of PHP. In practice, some changes will usually be needed. Two of the most important recent changes that affect old code are: * The deprecation dep·re·cate tr.v. de·pre·cat·ed, de·pre·cat·ing, de·pre·cates 1. To express disapproval of; deplore. 2. To belittle; depreciate. of the old $HTTP * VARS arrays (which need to be indicated as global when used inside a function or method). The following autoglobal arrays were introduced in PHP 4.1.0. They are: $ GET, $_POST, $_COOKIE, $ SEI?VER Ver personification; portrayed as infantile and tender. [Rom. Myth.: LLEI, I: 322] See : Spring , $_FILES, $_EAW EAW Empire at War (Star Wars video game) EAW European Arrest Warrant (also seen as EUAW) EAW Eastern Acoustic Works, Inc. , $EQUEST, and $ SESSION The older $HTTP _* VARS arrays, such as $HTT HTT Hyper-Threading Technology (Intel) HTT Human Touch Technology HTT Hypertext Template (Microsoft Windows file extension) HTT Holier Than Thou HTT Hyper Transport Technology HTT Human Terrain Team _POST_VARS, stillexist as they have since PHP 3. As of PHP 5.0.0, the long- PHP predefined variable arrays may be disabled- with the register long arrays directive. * External variables are no longer registered in the global scope by default. In other words, as of PHP 4.2.0 the PHP directive register globals is off by default in php. ini. The preferred method of accessing these values is via the antoglobal arrays mentioned above. Older scripts, books, and tutorials may rely on this directive being on. If it were on, for example, one could use $id from the URL http:llwww.example.com/foophp?id=42. Whether on or off, $_GET [id] is available. For more details on thew thew n. 1. A well-developed sinew or muscle. 2. Muscular power or strength. Often used in the plural. [Middle English, individual habit, virtue, strength changes, see the section on predefined variables and links therein. http://uk.php.net/manual/en/print/tutorial.oldcode.php |
|
||||||||||||||||

Printer friendly
Cite/link
Email
Feedback
Reader Opinion