Chap1 1.1 To 1.3
Chap1 1.1 To 1.3
Introduction To PHP
Marks 12
1.1 What is PHP?
● PHP stands for PHP: Hypertext Preprocessor, and it is a server-side scripting
language.
● PHP is the web development language written by and for web developers. The product
was originally named Personal Home Page Tools, and many people still think that’s
what the acronym stands for, but as it expanded in scope, a new and more appropriate
(albeit GNU-ishly recursive) name was selected by community vote.
● Strictly speaking, PHP has nothing to do with layout, events or really anything about the
look and feel of a web page. In fact, most of what PHP does is invisible to the end user.
Someone looking at a PHP page will not necessarily be able to tell that it was not
written purely in Hypertext Markup Language (HTML), because the result of PHP is
usually HTML.
● When your Web browser accesses a URL, it is making a request to a Web server. When
you request a PHP page, something like http://www.yourcompany.com/home.php, the
Web server wakes up the PHP parsing engine and says, “Hey! You’ve got to do
something before I send a result back to this person’s Web browser.” Then the PHP
parsing engine runs through the PHP code found in home.php, and returns the
resulting output. This output is passed back to the Web server as part of the HTML code
in the document, which in turn is passed on to your browser, which displays it to you.
Originally used for tracking visits to his online resume, he named the suite of scripts
"Personal Home Page Tools," more frequently referenced as "PHP Tools."
He eventually combined these tools with a form interpretation (FI) package he had
written, added some database support, and released what was known as PHP/FI.
Then, in the spirit of open source software development, developers all over the world
began contributing to PHP/FI. By 1997, more than 50,000 Web sites were using
PHP/FI to accomplish different tasks—connecting to a database, displaying
dynamic content, and so on.
At that point, the development process really started becoming a team effort. With
primary assistance from developers Zeev Suraski and Andi Gutmans, the version 3.0
parser was created. The final release of PHP 3.0 occurred in June of 1998, when it
was upgraded to include support for multiple platforms (it’s not just for Linux
anymore!) and Web servers, numerous databases, and SNMP (Simple Network
Management Protocol) and IMAP (Internet Message Access Protocol)
PHP 4.0 marked a complete rethinking of the PHP core and a rewrite of the
internals of the scripting language itself, with a long list of new and useful
features.
When PHP 5.0 was released, having new concepts of object-oriented development
and database work.
PHP7 current version During 2014 and 2015, a new major PHP version was
developed, which was numbered PHP 7 with following features.
Many fatal- or recoverable-level legacy PHP error mechanisms were replaced with
modern object-oriented exceptions
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent
back to the browser.
<?php
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a
built-in PHP function "echo" to output the text "Hello World!" on a web page:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
?>
</body>
</html>
Databases are generally useful, perhaps the most consistently useful family of software
products in modern computing. Like many competing products, both free and
commercial, MySQL isn’t a database until you give it some structure and form.
Security
● A privilege and password system that is very flexible and secure, and that enables
host-based verification.
● Password security by encryption of all password traffic when you connect to a server.
Scalability
● Support for large databases. We use MySQL Server with databases that contain 50
million records.
Connectivity
● Clients can connect to MySQL Server using several protocols:
o On Windows systems, clients can connect using named pipes if the server is
started with the named_pipe system variable enabled. Windows servers also
support shared-memory connections if started with the shared_memory system
variable enabled.
o On Unix systems, clients can connect using Unix domain socket files.
The project of MySQL was started in 1979, when MySQL's inventor, Michael Widenius
developed an in-house database tool called UNIREG for managing databases. After that
UNIREG has been rewritten in several different languages and extended to handle big
databases. After some time Michael Widenius contacted David Hughes, the author of
mSQL, to see if Hughes would be interested in connecting mSQL to UNIREG's B+ ISAM
handler to provide indexing to mSQL. That's the way MySQL came in existence.
MySQL is named after the daughter of Michael Widenius whose name is "My".
Milestones
Additional milestones in MySQL development included:
1. Cost
PHP is one of the “P’s” in the popular LAMP stack. The LAMP stack refers to the
popular combination of Linux, Apache, MySQL, and PHP/Perl/Python that runs many
web sites and powers many web applications.
Many of the components of the LAMP stack are free, and PHP is no exception. PHP
is free, as in there is no cost to develop in and run programs made with PHP.
Though MySQL’s license and costs have changed, you can obtain the Community
Server edition for free. MySQL offers several levels of support contracts for their
database server. More information can be obtained at www.mysql.com.
Both PHP and MySQL run on a variety of platforms, including many variants of Linux,
Microsoft Windows, and others. Running on an operating system such as Linux gives
the opportunity for a completely free web application platform, with no up-front costs.
Of course, when talking about software development and application platforms, the
up-front cost of software licensing is only a portion of the total cost of ownership
(TCO). Years of real-world experience with Linux, Apache, MySQL, and PHP in
production environments has proved that the total cost of maintaining these
platforms is lower, many times much lower, than maintaining an infrastructure with
proprietary, non-free software.
2. Ease of Use
When compared to many other programming languages, PHP make it easy to develop
powerful web applications quickly. Many of the most useful specific functions (such as
those for opening a connection to an Oracle database or fetching e-mail from an
Internet Message Access Protocol [IMAP] server) are predefined for you. A lot of
complete scripts are waiting out there for you to look at as you’re learning PHP.
Most advanced PHP users (including most of the development team members) are
diehard handcoders. They tend to share certain gut-level, subcultural assumptions —
for instance, that handwritten code is beautiful and clean and maximally
browser-compatible and therefore the only way to go .
The PHP community offers help and trades tips mostly by e-mail, and if you want to
participate, you have to be able to parse plain-text source code with facility. Some
WYSIWYG users occasionally ask list members to diagnose their problems by looking at
their web pages instead of their source code, but this rarely ends well.
That said, let us reiterate that PHP really is easy to learn and write, especially for those
with a little bit of experience in a C-syntaxes programming language. It’s just a little
more involved than HTML. This small learning curve means that relatively
inexperienced programmers can sometimes make mistakes that turn into large security
issues.
If you have no relational database experience, or are coming from an environment such
as Microsoft Access, MySQL’s command-line interface and lack of implicit structure may
at first seem a little daunting. MySQL has a few GUI (graphical user interface) tools to
help work with databases.
3. HTML-embeddedness
The HTML-embeddedness of PHP has many helpful consequences:
.■ PHP can reduce labor costs and increase efficiency because of its shallow learning
curve and ease of use.
4. Cross-platform compatibility
PHP and MySQL run native on every popular flavor of Linux/Unix (including Mac OS X)
and Microsoft Windows. A huge percentage of the world’s Hypertext Transfer Protocol
(HTTP) servers run on one of these two classes of operating systems.
PHP is compatible with the leading web servers: Apache HTTP Server for Linux/Unix
and Windows and Microsoft Internet Information Server. It also works with several
lesser-known servers. Specific web server compatibility with MySQL is not required,
since PHP will handle all the dirty work for you.
5. Stability
The word stable means two different things in this context:
The software doesn’t change radically and incompatibly from release to release.
To our advantage, both of these connotations apply to both MySQL and PHP.
Apache Server is generally considered the most stable of major web servers, with a
reputation for enviable uptime percentages. Most often, a server reboot isn’t required
for each setting change. PHP inherits this reliability; plus, its own implementation is
solid yet lightweight.
PHP and MySQL are also both stable in the sense of feature stability. Their respective
development teams have thus far enjoyed a clear vision of their project and refused to
be distracted by every new fad and ill-thought-out user demand that comes along.
Much of the effort goes into incremental performance improvements, communicating
with more major databases, or adding better OOP support. In the case of MySQL, the
addition of reasonable and expected new features has hit a rapid clip. For both PHP and
MySQL, such improvements have rarely come at the expense of compatibility.
6. Many extensions
PHP makes it easy to communicate with other programs and protocols. The PHP
development team seems committed to providing maximum flexibility to the largest
number of users.
PHP development is also constant and ongoing. Although there are clearly major
inflection points, such as the transition between PHP4 and PHP5, these tend to be most
important deep in the guts of the parser — people were actually working on major
extensions throughout the transition period without critical problems. Furthermore,
the PHP group subscribes to the open source philosophy of “release early, release
often,” which gives developers many opportunities to follow along with changes and
report bugs.
8. Not proprietary
The history of the personal computer industry to date has largely been a chronicle of
proprietary standards: attempts to establish them, clashes between them, their
benefits and drawbacks for the consumer, and how they are eventually replaced with
new standards.
The past few years the Internet has demonstrated the great convenience of voluntary,
standardsbased, platform-independent compatibility. E-mail, for example, works so
well because it enjoys a clear, firm standard to which every program on every platform
must conform. New developments that break with the standard (for example,
HTML-based e-mail stationery) are generally regarded as deviations, and their users
find themselves having to bear the burdens of early adoption.
Furthermore, customers (especially the big-fish businesses with large systems) are fed
up with spending vast sums to conform to a proprietary standard only to have the
market uptake not turn out as promised. Much of the current momentum toward XML
and web services is driven by years of customer disappointment with Java RMI
(Remote Method Invocation), CORBA, COM, and even older proprietary methods and
data formats.
Right now, software developers are in a period of experimentation and flux concerning
proprietary versus open standards. Companies want to be sure that they can maintain
profitability while adopting open standards. There have been some major legal
conflicts related to proprietary standards, which are still being resolved. These could
eventually result in mandated changes to the codebase itself or even affect the futures
of the companies involved. In the face of all this uncertainty, a growing number of
businesses are attracted to solutions that they know will not have these problems in
the foreseeable future.
The main advantage for most new users is technical support without charge, without
boundaries, and without the runaround. People on the mailing list are available 24/7 to
answer your questions, help debug your code, and listen to your gripes. The support is
human and real. PHP community members might tell you to read the manual, take your
question over to the appropriate database mailing list, or just stop your whining —
but they’ll never tell you to wipe your C drive and then charge you for the privilege.
Often, they’ll look at your code and tell you what you’re doing wrong or even help you
design an application from the ground up.
As you become more comfortable with PHP, you may wish to contribute. Bug tracking,
offering advice to others on the mailing lists, posting scripts to public repositories,
editing documentation, and, of course, writing C code are all ways you can give back to
the community.
Web server is a computer where the web content is stored. Basically web
server is used to hostthe web sites but there exists other web servers also
such as gaming, storage, FTP, email etc.Web site is collection of web pages
whileweb server is a software that respond to therequest for web resources.
Web server respond to the client request in either of the following two
ways:Sending the file to the client associated with the requested
URL.Generating response by invoking a script and communicating with
database.
How Does Apache Web Server Work?
Although we call Apache a web server, it is not a physical server, but rather a software that
runs on an HTTP server. Its job is to establish a connection between a server and the
browsers of website visitors (Firefox, Google Chrome, Safari, etc.) while delivering files back
and forth between them (client-server structure). The Apache software is also compatible
with any operating system, from Windows to Unix.
When a visitor wants to load a page on your website, for instance, the homepage or your
“About Us” page, their browser sends a request to your server, and Apache returns a
response with all the requested files (text, images, etc.).
The server and the client communicate through the HTTP protocol, and the Apache web
server is responsible for the smooth and secure communication between the two machines.
Apache is highly customizable, thanks to its open-source infrastructure. Due to this, web
developers and users can adapt its source code according to the type of website they’re
creating.
In addition, Apache provides plenty of modules that allow server administrators to turn
additional functionalities on and off. The Apache web server has modules for security,
caching, URL rewriting, password authentication, and other features.
Pros:
Cons:
AMP Module in PHP Stand for Apache, MySQL, and PHP. AMP module in PHP is can be
characterized into three component Apache MySQL, and PHP.
Below figure shows the relationship between the Apache, MySQL, and PHP.
Apache
● In AMP module, Apache act as a web server on which your web application are hosted.
● Its main job is to parse the file which requested by the web browser and display correct
result according to the code within that file.
PHP
MySQL
● MySQL is the relational database management system.
● MySQL is the database that required when we want to create dynamic web application.
● In MySQL database the data stored in tabular form.
● MySQL enables PHP and Apache to work together to access and display data in a
readable format to a web browser.
● First user request for the web page through the web browser.
● If the requested page is found on the server then the server will process on that page and
execute the scripting code(PHP code).
After the execution of the scripting code(PHP code), the server sends the output to the user in
the form of HTML.
The users browser process on this page that send by the web server and display to the user.
representing one of the five key components. The software packet contains the web server Apache,
the relational database management system MySQL (or MariaDB), and the scripting languages Perl
and PHP. The initial X stands for the operating systems that it works with: Linux, Windows, and Mac
OS X.
● Apache: ihe open source web server Apache is the most widely used server worldwide for
delivery of web content. The server application is made available as a free software by the
Apache Software Foundation.
● MySQL/MariaDB: in MySQL, XAMPP contains one of the most popular relational database
management systems in the world. In combination with the web server Apache and the
scripting language PHP, MySQL offers data storage for web services. Current XAMPP
versions have replaced MySQL with MariaDB (a community-developed fork of the MySQL
project, made by the original developers).
● PHP: the server-side programming language PHP enables users to create dynamic websites
or applications. PHP can be installed on all platforms and supports a number of diverse
database systems.
● Perl: the scripting language Perl is used in system administration, web development, and
network programming. Like PHP, Perl also enables users to program dynamic web
applications.
Alongside these core components, this free-to-use Apache distribution contains some other useful
tools, which vary depending on your operating system. These tools include the mail server Mercury,
the database administration tool phpMyAdmin, the web analytics software solutions Webalizer,
OpenSSL, and Apache Tomcat, and the FTP servers FileZilla or ProFTPd.
Installing XAMPP
Application areas
An XAMPP server can be installed and used with a single executable file quickly and easily,
functioning as a local test system for Linux, Windows, and Mac OS X. The software packet
contains the same components that are found on common web servers. Developers have the
chance to test out their projects locally and to transfer them easily to productive systems.
But XAMPP isn’t suitable to use as a public server, because many safety features have been
deliberately left out to simplify and speed up the system for testing.
Tip
You need a server for your web project? With IONOS you can easily rent your server online.
From virtual servers for middle to large-sized projects, to cloud servers for optimal flexibility
as well as dedicated servers for particularly complex requirements. Check out the range!
Installing XAMPP
Our XAMPP tutorial will take you through the installation process for the software package on
Windows. If you’re using Linux or Mac OS X, then the steps listed below for the installation
Step 1: Download
XAMPP is a release made available by the non-profit project Apache Friends. Versions with
PHP 5.5, 5.6, or 7 are available for download on the Apache Friends website.
recommended to temporarily pause any antivirus software until all XAMPP components have
writing access to the C: drive, so we recommend you deactivate this too for the duration of
the installation process. To find out how to turn off your UAC, head to the Microsoft Windows
support pages.
User
account control can affect the installation of XAMPP
of the User Account Control, the start screen of the XAMPP setup wizard should appear
XAMPP software bundle from the installation. But for a full local test server, we recommend
you install using the standard setup and all available components. After making your choice,
click ‘Next’.
In the
dialog window entitled 'select components', you can choose the software components before
installation
to be installed. If you opt for the standard setup, then a folder with the name XAMPP will be
created under C:\ for you. After you’ve chosen a location, click ‘Next’.
For the
next step, you need to select the directory where XAMPP should be installed
The setup wizard will unpack and install the selected components and save them to the
designated directory. This process can take several minutes in total. You can follow the
progress of this installation by keeping an eye on the green loading bar in the middle of the
screen.
According to the default settings, the selected software components are unpacked and installed
in the target folder
XAMPP. Use the corresponding check box to enable communication between the Apache
server and your private network or work network. Remember that making your XAMPP server
clicking on ‘Finish’. Click to tick the corresponding check box and open the XAMPP Control
Controls for the individual components of your test server can be reached through the
XAMPP Control Panel. The clear user interface logs all actions and allows you to start or stop
individual modules with a single. The XAMPP Control Panel also offers you various other
buttons, including:
● Config: allows you to configure the XAMPP as well as the individual components
● Netstat: shows all running processes on the local computer
● Shell: opens a UNIX shell
● Explorer: opens the XAMPP folder in Windows Explorer
● Services: shows all services currently running in the background
● Help: offers links to user forums
● Quit: closes the XAMPP Control Panel
In the Control Panel, you can start and stop individual modules
Starting modules
Individual modules can be started or stopped on the XAMPP Control Panel through the
corresponding buttons under ‘Actions’. You can see which modules have been started
because their names are highlighted green under the ‘Module’ title.
An active module is marked in green in the Control Panel
If a module can’t be started as a result of an error, you’ll be informed of this straight away in
red font. A detailed error report can help you identify the cause of the issue.
Setting up XAMPP
A common source of error connected with Apache is blocked ports. If you’re using the
standard setup, then XAMPP will assign the web server to main port 80 and the SSL port 443.
The latter of these particularly is often blocked by other programs. In the example above, it’s
likely that the Tomcat port is being blocked, meaning the web server can’t be started. There
● Change the conflicting port: Let’s assume for the sake of example that the instant
messenger program Skype is blocking SSL port 443 (this is a common problem). One
way to deal with this issue is to change Skype’s port settings. To do this, open the
program and navigate via ‘Actions’, ‘Options’, and ‘Advanced’, until you reach the
‘Connections’ menu. You should find a box checked to allow Skype access to ports 80
and 443. Deselect this checkbox now.
● Change the XAMPP module port settings: Click the Config button for the module in
question and open the files httpd.conf and httpd-ssl.conf. Replace port number 80 in
httpd.conf and port number 443 in httpd-ssl.conf with any free ports, before saving the
file data. Now click on the general Config button on the right-hand side and select
‘Services and Ports Settings’. Customize the ports for the module server to reflect the
changes in the conf files.
Modules that can’t be started will be shown in red. The user will also receive an error report to
help solve the problem
Module administration
You have an ‘Admin’ option located on the Control Panel for every module in your XAMPP.
● Click on the Admin button of your Apache server to go to the web address of your
web server. The Control Panel will now start in your standard browser, and you’ll be
led to the dashboard of your XAMPP’s local host. The dashboard features numerous
links to websites for useful information as well as the open source project BitNami,
which offers you many different applications for your XAMPP, like WordPress or other
content management systems. Alternatively, you can reach the dashboard through
localhost/dashboard/.
By clicking on the 'admin' button of the Apache module, the user will be redirected to the local
dashboard of XAMPP
● You can use the Admin button of your database module to open phpMyAdmin. Here,
you can manage the databases of your web projects that you’re testing on your
XAMPP. Alternatively, you can reach the administration section of your MySQL
database via localhost/phpmyadmin/.
The web project’s databases are managed by the user in phpMyAdmin (accessible via the 'Admin'
button in the database module)
To check whether your test server is installed and configured correctly, you have the option
to create a PHP test page, store them on your XAMPP’s local host, and retrieve them via the
web browser.
● Open the XAMPP directory through the ‘Explorer’ button in the Control Panel and
choose the folder htdocs (C:\xampp\htdocs for standard installations). This directory
will store file data collected for web pages that you test on your XAMPP server. The
htdocs folder should already contain data to help configuration of the web server. But
you should store your own projects in a new folder (like ‘Test Folder’ for example).
● You can create a new PHP page easily by using the following content in your editor
and storing it as test.php in your ‘ test’ folder (C:\xampp\htdocs\test):
<html>
<head>
<title>PHP-Test</title>
</head>
<body>
</body>
</html>
● The last step now is to open your web browser and load your PHP page via
localhost/test/test.php. If your browser window displays the words ‘Hello World’, then
you’ve successfully installed and configured your XAMPP.
The Apache web server is among the most popular web servers in the world. It’s well
documented, has an active community of users, and has been in wide use for much of
the history of the web, which makes it a great choice for hosting a website.
Start by updating the package manager cache. If this is the first time you’re using sudo
within this session, you’ll be prompted to provide your user’s password to confirm you
have the right privileges to manage system packages with apt:
You can do a spot check right away to verify that everything went as planned by visiting
your server’s public IP address in your web browser (view the note under the next
heading to find out what your public IP address is if you do not have this information
already):
http://your_server_ip
The default Ubuntu 22.04 Apache web page is there for informational and testing
purposes. Below is an example of the Apache default web page:
If you can view this page, your web server is correctly installed and accessible
through your firewall.
Step 2 — Installing MySQL
Now that you have a web server up and running, you need to install the database
system to be able to store and manage data for your site. MySQL is a popular
database management system used within PHP environments.
When you’re finished, test whether you’re able to log in to the MySQL console by
typing:
sudo mysql
Exit
You have Apache installed to serve your content and MySQL installed to store and
manage your data. PHP is the component of our setup that will process code to display
dynamic content to the final user. In addition to the php package, you’ll need php-mysql,
a PHP module that allows PHP to communicate with MySQL-based databases. You’ll
also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP
packages will automatically be installed as dependencies.
Copy
Once the installation is finished, run the following command to confirm your PHP
version:
php -v
1.
Copy
Output
Apache on Ubuntu 22.04 has one virtual host enabled by default that is configured to
serve documents from the /var/www/html directory.
Additional points
According to the PHP manual, “The goal of the language is to allow Web developers to
write dynamically generated pages quickly.”
Here are some common uses of PHP, all of which are a part of what you’ll learn in this
book:
• Perform system functions: create, open, read from, write to, and close files on your
system; execute system commands; create directories; and modify permissions.
• Gather data from forms: save the data to a file, send data via e-mail, and return
manipulated data to the user.
• Access databases and generate content on the fly, or create a Web interface for adding,
deleting, and modifying elements within your database.
• Encrypt data.