A friend of mine replied of Twitter when I said “Read programming books everyday”, he said “don’t forget to code as well”. Yes, you can’t be a senior guy without practicing. You can tell everyone what PHP has, what is Zend, what is Laravel, how they’re compared together, but when you try do real thing, you will be confused. So, I decide change my way in learning PHP, and I will share to you.
This is the first article of the series “Tumi learns PHP”. But please consider:
- You can’t learn everything about PHP by reading my article. It’s just my way in practicing PHP.
- You should skim through some PHP fundamentals book to learn PHP syntax or visit http://www.w3schools.com/php/ to learn.
- I’m Vietnamese, English is my 2nd language, and it means that I can have a lot of typo/grammar mistakes. So please correct me if anything wrong.
- And the most important one, I’m just a PHP newbie, please help to make me a better PHP programmer.
Write a web app which sums the terms of the harmonic series 1 + 1/2 + 1/3 + 1/4… + 1/(n-1) + 1/n up to a limit supplied by the user. (n > 0, n is an integer)
Currently I do not have any idea at all. But I have questions:
- What is PHP, anyway?
- Yes, I know PHP is a language for web development, I need an editor for writing PHP code, and which editor should I use?
- I know that code can run by itself, it need to be compiled, how to compile PHP code and deploy it so that we can use it?
- Environment for PHP development? How we setup that?
What is PHP?
WikiPedia.org define: “PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.” and “PHP code is interpreted by a web server with a PHP processor module, which generates the resulting web page: PHP commands can be embedded directly into an HTML source document rather than calling an external file to process data. It has also evolved to include a command-line interface capability and can be used in standalone graphical applications.”
PHP is a server-side scripting language and it’s also an interpreted language. It means that, PHP doesn’t need to be compiled before like Java, C# or C++, it is parsed and performed directly by an interpreter at runtime.
How can I develop web app with PHP?
It’s quite easy to getting started with PHP. I will introduce of how to install PHP on Windows first. At the moment, I just want to have PHP install on my computer, and can write some PHP code as soon as possible. I have heard about:
- WAMP (Windows – Apache – MySQL – PHP)
- LAMP (Linux – Apache – MySQL – PHP)
- MAMP (Mac OS – Apache – MySQL – PHP)
- Or NginX ( a web server that support PHP)
Apache is a well-known web server for PHP, it is open source like PHP and it’s free. MySQL is a database management system, is also an open source. We will learn about them in later articles.
Right now, we go to http://windows.php.net/download/, my computer is a Windows 8 x64 one, so I choose PHP 5.5.3 VC11 x64 Non Thread Safe (php-5.5.3-nts-Win32-VC11-x64.zip). I download it and unzip it at E:\PHP\ (you can extract anywhere you want to).
Now open the Command Line (or PowerShell), and try to check PHP version:
Figure 1.1: Check PHP version
On the above screenshot, I use the –V parameter to see installed PHP version, it is 5.5.3.
Ok, we already have PHP inside our computer, but how to start programming with it?
Don’t worry, first thing first you should add your PHP directory in to PATH variable of Windows, then you can call PHP everywhere you want. Follow this guide (http://www.computerhope.com/issues/ch000549.htm) to add PHP to Windows PATH variable.
Now, we use PHP.exe command to print the result of 5 + 3:
Figure 1.2 – First PHP sentence
You can see the command and its result in figure 1.2. However, PHP is for web development, how can we do it without Apache or Nginx? Luckily, PHP has a built-in web server, and you can host your website for study purpose without installing Apache or NginX.
Create a folder that will contain you PHP files (I choose E:\_Websites\ex_01). Inside that folder, create hello.php, by call the below command:
Figure 1.3 – Create hello.php
Then type the content for hello.php using Notepad.
Listing 1.1 – Hello.php On the above code, the hello.php includes PHP code and HTML code, and PHP code is wrap inside <?php .. ?>. Now, back to the command line, and host the ex_01 folder as a website by using PHP built-in server: Figure 1.4 – Host a website using PHP built-in server Now open your browse and navigate to http://localhost:8089/hello.php Figure 1.5 – My first PHP web page So, we have our first PHP page, but writing PHP code with Notepad program is not a good idea. You need a more powerful editor that has PHP color syntax, code auto completion and much more. I prefer to choose Sublime Text, but Notepad++ is good as well. And there are many more powerful editor that support PHP like NetBeans, Eclipse…, most of them are free.
Now we back to the problem that we write out in the beginning of the article “Write a web app which sums the terms of the harmonic series 1 + 1/2 + 1/3 + 1/4… + 1/(n-1) + 1/n up to a limit supplied by the user. (n > 0, n is an integer)” In the root folder of the website, I create a PHP file called harmonicseries.php, we will implement our code to solve the problem inside it. Let consider the situation:
- User will request the address http://localhost:8089/harmonicseries.php, and will get a web page that show a text box and a submit button; user will input the value of n and click submit button to send n value to web server.
- The web server will calculate the harmonic series total and send back the result to end user.
Let start coding! Now we add some HTML code into the file:
Listing 1.2 – HTML form for inputting the value of N
When try to browse, we will get a web page like below:
Figure 1.6 – The harmonicseries.php UI
You can click on Submit button, but nothing happen because there is no server side code for processing it.
When you try to browse http://localhost:8089/harmonicseries.php, you‘ve just created a GET request to server and web server parse PHP code and generate HTML content and return back to the browser. Then, you input the value of N to the textbox, and click Submit button. The browser will send a POST request to web server when Submit is clicked. This is because we set the method of HTML form to POST. When receiving the POST request, we need to have PHP code to retrieve the value of N and calculate the total number and then return the result back to client.
To determine if a request is POST or GET, you need to access a global variable $_SERVER like below:
Listing 1.3 – Detect request method
After thinking for a while, I decide to implement the entire code like below listing:
Listing 1.4 – Implement the harmonicseries.php
if you’re new to web development, you will have many questions for the above code. But I do not explain anything. You should try and to answer by yourself.
Now try to run the web page, and enter an invalid value to the textbox and click Submit:
Figure 1.7 – Try to add an invalid value
You will receive the error message:
Figure 1.8 – Result when sending an invalid value to web server
When you enter an valid value, you will receive the result:
Figure 1.9 : Result when sending a valid value to web server
Congratulation, you’ve done it.
Anyway, the above code is not good enough, and as an experience .NET developer, I ask myself how to improve the code, because:
- This code is not easy for unit test, and how to unit test with PHP?
- This code is not well designed, maybe we should apply some OOP principles.
- Validation is not well enough, some invalid case is not checked, e.g. user enter decimal/negative value.
And I decide to learn how to make my code better by applying unit test and redesign the existing code in the next article.