There are many programming languages. Some come, some go, a few stay. One of them is PHP, which is especially important in the environment of web applications. If you want to learn PHP from scratch, here is a compact introduction.
Learn PHP: An introduction to the programming language
PHP is constantly being developed and now offers modern language concepts. The universal usability, the good database support and the availability for many systems speak for the frequent use.
In our basic introduction to programming, which has already been published here at entwickler.de, we were quite neutral in our choice of programming language. Of course we have shown one or the other example and have mostly used C# in combination with the .NET Framework. It is also a good choice for desktop applications, because especially many business applications are created in this way.
In this series of tutorials we would like to highlight another important topic for getting started in programming. It is about the development of web applications, which today are an important way to provide enterprise applications. Multi-user and network operation can be realized very easily with such an application. The actual program runs on the server and the users access it via the network. The front-end is made available on the users’ side using HTML, CSS and other modern browser technologies. The user therefore only needs a connection to the server and an up-to-date Internet browser to work with the application. The installation of additional software is usually unnecessary.
These remarks on the topic should be sufficient for now, because we will come back to the structure and architecture of a web application in more detail later. Basically, there is an almost unmanageable variety of technologies available for the programming of web applications. It is not very helpful to philosophize about the advantages and disadvantages of the individual approaches and concepts. In this tutorial series we introduce PHP. We address two addressees:
Newcomers to programming: You can very well choose PHP as your first programming language, because in PHP you will find all common language concepts. If you later switch to another programming language, you can profitably take what you have learned with you.
Non-language and non-system developers: Has the intention to finally become familiar with the standard language of the web been at the top of your to-do list for a long time? If so, you have the advantage of being able to find a lot of relatives from other languages and can start programming directly in PHP.
Whatever the motivation, in the following text section we will first deal with the basic characteristics of the programming language. Then we will establish the system requirements before we give a compact and systemic overview of PHP. In the following tutorials we will go deeper into the matter. We will also implement one or the other example.
PHP – short and sweet
PHP is the abbreviation for PHP Hypertext Preprocessor. It enables developers to create dynamic web pages that are used to create web applications. Unlike static web pages, the content can change at any time due to user actions or data updates. In particular, PHP supports the simple evaluation of forms that a user can use to send data to a website. It enables the cooperation with many different database systems; MySQL, for example, is particularly well supported. Both components work together in the best possible way. Many content management systems are programmed in PHP and store their data in a MySQL database. Compared to other programming languages, PHP offers many advantages:
- Cross-platform: It supports a variety of platforms, including easy integration with the commonly used Apache web server.
- Easy to learn: Compared to other languages, PHP is relatively easy to learn. Unlike others, this language was developed exclusively for web server programming and therefore contains only the necessary components.
- Price: PHP is free of charge. Neither a compiler nor a development system needs to be purchased. The programming can be done with the help of a simple editor.
At this point we can already go into the basic functionality of a PHP program. The basis for this is a special variant of the client-server model. Figure 1 shows the principle. The user (client) does not need to have any other software installed except a conventional internet browser.
Already at this point the high flexibility of the approach becomes clear. Internet browsers are available for all operating systems and variants. It does not matter whether it is a conventional desktop PC, a laptop or a smartphone or tablet. On the client side you don’t have to deal with PHP, you only have to process HTML files (possibly CSS and JavaScript).
The application itself runs on the server. PHP is integrated into the web server and the actual PHP application is stored and installed on it. The user sends a request via the browser to the server. This request is processed by the PHP program. It may be necessary for the PHP program to retrieve data from a database. The database is also installed on the web server or can be accessed from it. Based on the request and its specific procedure (algorithm), the PHP interpreter now generates the response in the form of an HTML file which is sent to the client. The browser itself always receives an HTML file as a result and only has to display it, i.e. it does not have to have any special properties related to the programming language of the server. The pages can therefore also be displayed by older browsers. Besides, this principle has another security-relevant advantage: the PHP program cannot be viewed or changed by the client. For this, access to the server is necessary.
System requirements
For learning and practicing we set up a minimal working and development environment locally on our PC/notebook. According to figure 1, we first need a web server with integrated PHP. Later on, the web server will not be identical to the client computer in terms of hardware, but for programming and learning, this setup is suitable. First a few basic remarks. The integration of PHP into the web server can be done in two different ways:
- SAPI module: It is a direct connection, but not available for all web servers. For important servers like Apache and Microsoft IIS there are SAPI modules available.
- CGI: The Common Gateway Interface allows the integration of PHP into almost all servers.
The question of the type of installation doesn’t have to concern us so much in the beginning, because the installation of PHP is made much easier by using automatic installers of the web server. This may be interesting if you want to run finished PHP applications on a remote server later. The type of installation and the exact version of PHP is one of the features you have to pay attention to when renting a server. Immediately the question of the suitable web server arises.
Here it depends also on the operating system on which the installation is carried out:
- Microsoft Windows: On the one hand you can use the Internet Information Server (IIS). In current versions of the operating system, this can be activated or deactivated via the Control Panel | Windows Futures Enable or Disable (Fig. 2). An alternative is Apache. This web server is the market leader and available for a variety of systems. Many web servers on the Internet run an Apache, mostly under Linux. For an easy start there are ready-made installation packages.
- Linux: The standard server is also an Apache. Many distributions already include support for Apache and/or PHP. For example, under the distribution Ubunto it only needs to be activated in the so-called runlevel editor.
- Mac OS X: An Apache server is already installed in OS X. Again, it has to be activated (System Preferences | Internet & Networking | Sharing | Personal Web Sharing | Start) .
Server installation
We have it simple and use a ready-made installation package for the Apache web server on Microsoft Windows. A very well known project is XAMPP. It is also available for other operating systems and in different versions or configurations. From the website we choose under Downloads | More Downloads the current version (support of PHP 7) for Windows, specifically the portable version. It has the advantage that the server can be started directly after downloading and unpacking the package.
An installation is not necessary, a minimal configuration is done automatically using the included script. We can therefore install the server on a USB stick and use it flexibly. Changes to the current PC configuration are not necessary. The package contains all necessary components to start programming. After the download (xampp-portable-win32-7.0.3-0-VC14.zip) the file has to be unpacked, in our case on a USB stick. We have renamed the directory to xampp for easy handling. The process may take a few minutes, time for a cup of tea!
Then it is already done. We look into the root directory. First we are interested in the following files:
- setup_xampp.bat: The setup (directories) for the webserver is done. This file has to be executed before the web server is started for the first time.
- xampp_start.exe: The web server is started.
- xampp_stop.exe: The web server can be stopped again.
- xampp_control.exe: The control panel for displaying the status of the web server and for carrying out necessary basic configurations is executed.
So let us start with setup_xampp.bat. The process only takes a few moments. After that we start the web server (xampp_start.exe) for the first time. The start process is displayed in a command line window. For security reasons, the operating system may request the necessary shares during the first start. The status of the server can also be viewed via the control panel.
The question arises as to where the pages are located in the file system when they are accessed via http://localhost or 127.0.0.1. In the default configuration (and we want to keep it that way), this is the htdocs directory below the root directory of the server. In our case everything is on the USB stick. So our own PHP applications have to be stored in this directory as well. For this purpose we have created another subdirectory called my within htdocs.
We do not want to break with tradition and start with a classic “Hello World” program. PHP does not work without HTML. The PHP source code is either included directly in the HTML file or outsourced to external files and a reference is made. In order for the web server to know that it is a file with PHP source code, its extension is set to php, for example test.php. PHP files are basically pure text files, i.e. they can be created and read with any text editor. In fact, an integrated development environment is not necessarily required for programming a dynamic web application, a text editor is sufficient; however, more about this at the end of the tutorial. Table 1 shows the variants for embedding/marking PHP code in an HTML file. Usually the first variant is preferred. It is clear and yet short.
By the way, you can also determine the way the server works by deactivating it and trying to display the page in the browser again. You will then receive an error message. This test is useful because the browser would have processed a pure HTML file even without the server.
In the following we give a compact overview of important language features. The complete language reference can be read under Besides the original documentation in English, it is available in many other languages, including German. Let’s start with essential features of PHP:
- comments: These are introduced by “#” or “//” and are valid until the end of the line.
- statements: These are terminated with a semicolon and grouped by curly brackets.
- Numeric literals: Without a decimal point, these are interpreted if they begin with “0x” or “0X”; in all other cases as decimal integer constants.
- String literals: These are enclosed in single or double quotation marks (‘ or “). Thus, the following are allowed: ‘This is a string’ or ‘This is also a string’.
- Function names: PHP is not case-sensitive, i.e. no distinction is made between upper and lower case.
- Variable names: Valid variable or function names start with a letter or underscore followed by any number of letters, numbers or underscores; variable names are case-sensitive, they start with a $, for example: $set and $set are two different variables.
- Data types: PHP supports the common datatypes;
Data types supported by PHP
PHP has a large number of pre-defined functions from various areas. One such function was the above used function echo “…” which could be used to output a string. The most important function classes are: String functions, functions for arrays, mathematical functions, control functions, time and date functions, file functions as well as class and object functions. There are also a number of functions for the cooperation with a database (connection establishment, data query). These are comprehensively documented in the online help. If you encounter a problem during programming, it is definitely worthwhile to check whether there is already a finished function for the task in question.
Tools
A craftsman is only as good as his tools. For PHP programming there are also a number of development environments available, which differ considerably in their performance. The right or better development environment does not exist, it depends on the individual requirements. As is well known, the desire for more comfort and support grows with project size. Important criteria for the selection are:
- Syntax Highlighting
- Code Completion
- Real-time error analysis
- simple and fast navigation
- Refactoring
- Source code generation
- Integration of versioning systems
A list of PHP development environments would most likely always be incomplete. Important representatives are PhpStorm, Sublime Text, NetBeans, Zend Studio, Eclipse with PHP Development Tools (PDT) [13] and new is Visual Studio Code. The latter development environment is based on Visual Studio and could be interesting especially for developers who come from the Microsoft universe and make their first steps with PHP. Some tools are also executable on different operating systems. We recommend “baking small rolls” for the beginning, i.e. a halfway comfortable text editor, such as TextPad, is sufficient. There is already enough to learn with a new language, so that you can extend the tool support later. PHP is perfect for this kind of gentle introduction.
Conclusion
We are already at the end of the first part of the introduction to programming with PHP. The focus was initially on the basic procedure and the creation of the technical requirements. For this we installed a local web server, created a typical “Hello World” program for testing and learned the first basic basics of the language.
If you already have programming experience in another language, you can actually start now. We will continue our introductory course in the upcoming developer magazine. We will complete the overview of the language features, among other things we will explicitly deal with the object-oriented possibilities of PHP, which have been significantly expanded since version 5.
For all those who do not have a concrete use of PHP at the moment, two hints: PHP is the language of many established content management systems. If you want to configure or extend them, knowledge of PHP is more than useful. Maybe you do not choose a desktop application for the next application, but alternatively create a dynamic web application.