Opposite: The internet connects the world. Programming languages like PHP make it possible.
PHP is what is known as a server-side scripting language. This means that it runs on an internet web server, a computer at a remote location that will process web requests and send web pages back to the user, rather than processing them on the user’s own computer. So, in order to appreciate how PHP functions, it is important to have some basic knowledge of how the internet itself works.
All over the world, computers and computer networks are linked through the global network known as the internet. This network is connected in a variety of ways, including telephone wires, fiber optic land lines, satellites, microwave links, and cables laid under the ocean. Despite all the different ways in which the parts of this vast network are connected, it handles all data in the same way. Within this uniformity of data transfer though, the internet can pass on many different types of data, including emails, web pages, and messages, from one computer to another and one network to another. One familiar form of data exchange that provides document storage and access, usually in the form of web pages, happens on what is known as the World Wide Web, a system of servers on the internet. One of the major uses of PHP is the creation of many of those web pages.
Web pages themselves are documents that have been specially formatted in what is called a mark-up language, the most common being HTML. Markup languages provide extra information about the visual appearance of text in a document, including how it should be formatted for display or printing. When a user types in a web address, or URL (Uniform Resource Locator), usually in the form www. webaddress.suffix, the user is connected to the web server at that location.
The web page located at that address on the server computer is transferred to the user’s computer, and the document, formatted using HTML, can be viewed by the user. Most documents use HTML or similar markup languages. However, when a document is transmitted using only HTML, it does not contain most of the features now expected—such as interactivity and audio and graphics. Something more has to happen with a web page so that users are connected and engaged in a variety of ways and not just reading the content. Rather than the static (unchanging) web pages created with only HTML, web designers create dynamic web pages with content that can change based on the interests or responses of the user. These dynamic web pages are generated in real-time through the use of scripting languages. The essential feature of a scripting language is its ability to manage the activities of other programs, telling them what to do, in the same way that a play script indicates what an actor should do to play a part.
There are two ways a website can be made dynamic. In order to create dynamic web pages, web applications may contain code that is processed on the user’s browser (the user’s browser is known as the client), or on the web server.
The method known as client-side, or front-end, scripting is run directly on the user’s computer and the processing usually takes place on the browser. Whatever client-side scripting language is being used, such as JavaScript, must first be enabled on the user’s computer in order for the scripting to function. The front-end scripts are embedded in the HTML of the user’s site. The scripts select site elements and manipulate them, providing the user with an interactive experience. Client-side code is best suited for situations in which the page elements may be changed without the need to contact a database. Client-side scripting can be used, for example, to move elements on a page, change colors and fonts, or display a pop-up window based on the position of the mouse.
Server-side or back-end scripting is the alternative way to build dynamic websites and has different uses than front-end scripting. In server-side scripting, the scripting application is run directly on a web server, rather than on the user’s own computer using the client browser. Software using server-side scripts takes a user’s request, processes it, and generates dynamic HTML pages that are sent back to the user’s browser. Unlike client-side scripting, server-side scripting can interface with databases on the server.
The two methods may be used in conjunction with one another. A site such as Amazon uses both types of scripting. Server-side scripting may be used for logins, entering personal information, responding to user inquiries, and handling new data storage. Client-side scripting, on the other hand, displays and sorts data in a variety of ways, changing the appearance of a page in response to the user’s actions.
Another significant distinction between the two scripting methods involves the ability to hide the code that generated the website. The website owner can protect their code and conceal the instructions from the client when using server-side scripting, since the script itself is never transferred from the server. Only the results of the processing are transferred. In client-side scripting, however, the script resides on the user’s computer and is fully accessible.
Server-side scripts, like those written in PHP, make a website useful and functional. The term “server” refers to both hardware (the actual machine) and software (the coded instructions that tell the server what to do). At a minimum, a web server understands web addresses (URLs). Whenever a browser needs a file that is hosted on a web server, the browser requests the file via HTTP (Hypertext Transfer Protocol), and the server sends the file back using the same protocols. This standardized method enables the user’s browser to view a web page.
The 1s and 0s of binary code are the simplest form of computer coding. PHP is far more complex, though it took time before it was recognized as a language.
When Rasmus Lerdorf first developed PHP, it was not thought of as a programming language. It was simply code written to help Lerdorf create a dynamic web page. However, what was useful to Lerdorf turned out to be useful to other programmers, and PHP's popularity grew.
Many programming languages, like Java and C, were developed through specific projects intended to create those languages. Even Ruby, the object-oriented scripting language originally developed for a Japanese market, was thought of as a new programming language from the beginning. Lerdorf himself, though, called his efforts a "tool box," not a language. He said, "There was never any intent to write a programming language. .. I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way. "
However, the energy and direction of the open-source community led to the adoption of PHP as a widespread and well-liked method for creating dynamic web pages—whether it was called a language or not. Although some claimed it couldn't be a "real" language because it was not consistent and it was not compiled, others claimed that it met the minimum requirements for being considered a language: using syntactical constructs, it instructed the computer to perform certain operations.
PHP also lacked any strong design principles or solid theoretical principles and was thought of, even by those who considered it to be a language, as messy and lacking rigor. Its implementation was dependent upon user documentation. Although that documentation was extensive, its extensiveness led to problems. Furthermore, some basic features, like correct handling of foreign language and Unicode characters, were add-ons that were not cleanly integrated.
As previously mentioned, in 2014, PHP finally got a complete definition of its semantics and its syntax in a document called a draft specification. The draft specification was intended to "evolve over time with the help of everyone who cares about the PHP language," just like other open-source products do. But, although it would be a document that would continue to change and mature, it provided, for the first time, a structure for the future development of PHP However PHP was initially created, and however it had evolved, after twenty undefined years PHP had finally become a full-fledged computer programming language.
The dynamic servers involved in server-side scripting, however, are more complex. These servers not only contain the software that controls HTTP file transfer, but also, most commonly, extra software applications and related databases. The software applications on dynamic servers process the requested web pages, updating the information on the web page as well as the information on the databases, and transfer the updated page back to the user’s computer.
This means that the web page and its content reside on the server and not on the user’s computer. The server-side process follows certain steps:
1. A user requests a web page by entering the URL for that page or by following a link to the page.
2. The scripting application on that server (for example, a PHP application) is interpreted by the server and creates or changes the content of the requested page “on the fly” based on current information, such as user login input or other available data.
3. The newly generated page in its final form is sent back to the user in HTML format.
Server-side scripting provides for a high level of personalization. Its ability to access databases is very powerful. Applications written in server-side languages like PHP provide the basis for e-commerce sites, social networking, and even Massively Multiplayer Online Role-Playing Games (MMORPGs). The HTML pages with server-side scripts are easy to maintain because they do not have to be edited every time on the local computer with newly written HTML and can be updated right on the server. Information used in updates is stored in a database, which may be located directly on the web server or may be on additional servers linked to it.
The PHP application can access different types of databases, and some of the most frequently used include the following:
• MySQL: This continues to be the most frequently used database with PHP applications, and it is the world’s most popular open-source database. MySQL is used on enormous websites such as Wikipedia, Facebook, and Google. It provides a web-based user interface in order to manage it.
• Oracle: The Oracle database is one of the most widely used databases. It is equally popular for servers based on Windows and Linux operating systems. Many major business organizations use this database.
• DB2: This database was created in 1983, although its development began in the 1970s. It was originally used extensively in conjunction with mainframe systems. DB2 is now being used in many large-scale Enterprise Resource Planning (ERP) and e-commerce systems.
• PostgreSQL: Compared to MySQL, PostgreSQL has many advanced features that compare well with the advanced features in Oracle. It is, however, believed to be a bit slower than MySQL.
• Sybase: This database solution is a popular choice for enterprise database management, and it can operate well when a large database is needed.
Although these are the most frequently implemented databases with PHP applications, PHP can support a wide variety of less popular databases, from Mongo to Tokyo Tyrant.
A server-side script, then, creates the communication link between a user, the web server, and the databases. It requests, edits, adds to, and deletes items in the database. In providing this link, the server-side script has many functions:
• It collects data from forms.
• It processes any user input, such as username and password, and it interacts with databases.
• It queries the databases in order to accomplish tasks like password validation.
• It updates the database.
• It sends and receives cookies.
• It encodes the generated web page into HTML.
• It transfers the requested web pages for display on the client browser.
• It encrypts data for security.
Some examples will help illustrate how this server-side scripting works.
E-commerce sites are frequently written using server-side scripting. Let’s imagine that a customer wants to purchase replacement toner for a laser printer. She enters a Google search for companies that sell laser toners, and the request is transmitted to the Google web server, via HTTP. The web server uses a scripting application that resides on the server, in order to process this request. The application sends a query to the Google database, looking for matches. When a match is found, the application generates a web page in HTML that contains the matches found on the database. Often, this web page is created simply by inserting the matching data in an HTML template, and the process is simplified and speedy. Now the newly created web page, responding to the customer’s inquiry and providing a list of toner sales companies, is transmitted back to the customer’s client browser, again using the HTTP protocols.
Online shopping is a great example of server-side scripting at work.
The customer next selects the website she wants to use from the list of companies on the web page that is now displayed on her browser. She clicks on the link, which connects to the chosen company’s web server. The company’s web page, which uses a scripting application written in PHP, is transferred from the company’s server to the user’s browser. This web page contains a number of input forms for the user to complete. She is asked to identify her printer make and model and the color(s) of toner she needs. When she has filled in the forms, she clicks on the submit button on the web page. Now the information is transferred back to the company’s web server. There, the scripting application connects to the database that contains information about printers, matches with the correct toners, and lists the price of each toner by color. The scripting application, once again, creates a web page “on the fly,” containing the information extracted from the toner database. This new web page is transmitted back to the user’s browser.
This page contains the information about the available toners and also has a form for the customer to complete if she wants to place an order. Filling in the form that requests that an order be placed and clicking to submit it once again transmits this information back to the company web server. The scripting application processes this request and sends a page back to the client for the customer to fill out with billing and shipping information. When the company web server receives this new information, the scripting application matches it to a database of customers that is also on the company’s server. If she is a new customer, the information will be added to the company’s customer database. If the information is a partial match, a web page that requests more information, for example an updated shipping address, will be generated. If the customer responds with new information, the database will be edited to include it. When the information has been verified, the customer will be requested to review the order details on a web page that was generated at the server with all the current data. If the customer accepts this information and completes the transaction, the database will be updated and the order will be placed.
Server-side scripts provide the interactivity on social media sites. Facebook, for example, uses PHP as its basic programming language. So, if a user wants to edit the information that is on his profile, he will first log in to his Facebook account with his username and password. This login information will be transferred to the Facebook server (actually one of the over sixty thousand Facebook servers). The server-side script on the server will access the database containing the user’s information and generate the personal Facebook page that the user will see, transmitting it back to the user’s browser.
In order to change the profile information, the user will hover over the Intro section below his profile picture and click on the pencil graphic. He then can edit or add to his profile. When he clicks on “update your information” and “save,” the new profile is sent back to the Facebook database on the web server, and the scripting application updates the user information that is stored there.
Websites that primarily provide informational content, like Wikipedia, have very simple scripting applications. If a user types in a search term on the Wikipedia website and clicks on the search button, the request is transmitted to the Wikipedia server. The scripting application finds the matching database and fills in the information it provides on an HTML template. The HTML file, containing all the database information, is then sent back to the user’s browser.
Some informational websites, however, have more complex scripts. When a user requests the front page of a news site, the web server application may retrieve news stories from a database on the server combined with news stories retrieved from another website to generate a page that includes the most current information. The page sent back to the client’s browser may, in turn, be automatically updated by the web server application.
Programmers don't want to keep reinventing the wheel. They often look for a piece of reusable code that can be dropped into an application to perform frequently utilized functions, such as user authentication or database management. This reusable code is contained in libraries that programmers can access when they are looking for an efficient way to insert a certain function into the application they are writing.
Frequently, these libraries are collected together in groups called packages. The libraries in a package are either built upon each other or use each other in order to function most effectively. For example, library "A" may use code from library "D" in order to run effectively, so the programmer needs to download both "A" and "D" in order to run code "A. " This relationship is called a dependency.
Managing dependencies can be a complex and sometimes messy problem, particularly if a project has multiple programmers calling upon a variety of packages to perform a variety of tasks. PHP Extension and Application Repository (PEAR) was an early system for distributing reusable PHP components. But PEAR was hard to search, hard to add new components to, and generally difficult to use, so many PHP programmers abandoned its use.
In 2012, Nils Adermann and Jordi Boggiano, two European developers, announced the release of Composer, a package dependency management tool for PHP Unlike PEAR, Composer manages dependencies on a project-by-project basis rather than forcing a programmer to install packages on the entire system. It will download all the required libraries and packages and handle them in a meaningful and logical way. Its main package repository is "Packagist. " Composer has now become an essential programming tool for most PHP projects.
Composer is a package dependency management tool.
Websites built with server-side scripting, such as PHP, have some significant advantages when the user wants to update the site, particularly if the site consists of a large number of pages. If the website is created with a static code such as HTML, each page would have to be changed manually and uploaded when an update was needed. If, however, the site is created using PHP and MySQL as a linked database, the content could be updated for the entire site using the database. In order to update or alter the design, it would only require altering a small amount of PHP script.
PHP code can look very much like the HTML code that is used to produce web pages. Although PHP scripting can accomplish a great variety of tasks, it is deceptively simple to learn and use. At its most basic level, it can be embedded in an HTML page and simply create a static document, as though the code were completely HTML. All embedded PHP code is identified by beginning with <?php and ending with ?>.
For example:
<html> THIS SECTION IS HTML
<head>
<title> My first PHP script </title>
</head>
<body>
<?php EMBEDDED PHP CODE BEGINS
echo “Hello, Programmers!";
?> EMBEDDED PHP CODE ENDS
</body> HTML CODE
</html>
This embedded PHP code will produce a static page with the message “Hello, Programmers!” This could also be achieved by using only HTML code. If, on the other hand, the code included a PHP variable, it would look like this:
<html> HTML CODE
<body>
<?php EMBEDDED PHP CODE
$txt1 = “Learn PHP";
$txt2 = “online";
echo $txt1 “;
echo “Study PHP $txt2";
?>
</body> HTML CODE
</html>
The output produced by this code would be: Learn PHP Study PHP online Since this message is created using the variables identified as $txt1 and $txt2, the output can be changed by changing the variables. This is a step closer to a dynamic web page. If, however, the user fills out a form created in HTML and clicks the submit button, the PHP code that can process that request does much more. The PHP code will refer to the HTML code that created the form, as in the following example:
<html> HTML CODE
<body>
<form action="identity.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
This HTML code will create a form for the user to complete that looks like this:
Name:
E-mail:
The user completes this form, clicks submit and the data is sent to a PHP file named “identity.php” so that it can be processed. The “identity.php” file (embedded in HTML) looks like this:
<html> HTML CODE
<body>
Welcome <?php echo $_POST["name"]; ?><br> PHP CODE BEGINS WITH <?php
POST["email"]; ?> </body>
PHP CODE ENDS WITH ?>
HTML CODE
</html>
If the user’s name was Harry and the email address entered was harry@mywebsite.com, the output on the user’s computer would be:
Welcome Harry
The email address you gave is: harry@
mywebsite.com
The PHP application on the web server took the data transmitted to it from the web form and generated a web page using that information. If the user was named Sally and her email was sally@mywebsite.com, the output would have been:
Welcome Sally
The email address you gave is: sally@ mywebsite.com
This example is a simple demonstration of one of the ways in which PHP can personalize a user’s web experience using simple code. More complex operations, including querying databases for information, validating input, and updating the databases, can all be accomplished with straightforward PHP code. PHP can do a lot with a little.