Getting Started with PHP Coding


In this article, we get started with coding in PHP. Read this article to know its syntax, coding rules, how PHP fits in with HTML and how PHP code can be integrated into HTML pages.

We assume that you have already installed and configured the required tools for PHP development on your system. For PHP coding, we are using a Windows machine with the WAMP Server package installed on it. Detailed instructions on installing, configuring the package and required components are provided in an article on this site. The WAMP Server package we are using gives us PHP version 5.4.3, Apache version 2.2.22 and MySQL version 5.5.24.

Overview of PHP structure and syntax


A text editor such as Notepad or WordPad can be used to view and edit PHP files. We recommend using Notepad++. This program can be downloaded from internet for free and can be used to edit various types of files including HTML, CSS and PHP. If in case you are using a paid program like Dreamweaver to edit your files, you may continue using the same to edit PHP files as well.

PHP pages end with a .php extension which basically tells the web server that it needs to parse the PHP code before showing the resulting HTML in the browser. PHP integrates with HTML in such a fashion that it is invisible to the users visiting a web site. If a user tries to view the source code of the page, he or she will only be able to see the resulting HTML and not the PHP code. Thus, PHP ensures security and flexibility in coding.

PHP code for a web page can be written within the HTML of that page or it can be written as a stand-alone program. Generally, a stand-alone program is used to include external functions and variables required for functioning of the page. We will see this later.

Common rules of PHP syntax


One of the many benefits of PHP is that it is straightforward and simple. As with any programming language, there are multiple ways to achieve the same result. To start with, we need to be aware of two very basic rules of PHP syntax.
PHP code is always written within a set of opening and closing tags as follows:

PHP Tags

Statements in PHP always end with a semicolon.

PHP Statements

Comments can be added to the code by using double slashes (//) for single line statements or /* and */ for opening and closing comment tags that extend over multiple lines of code.

Hello World with PHP


We now get to do some coding by writing the customary Hello World program in PHP. The purpose of writing this piece of code is to ensure that we have configured our development settings correctly. Before we proceed, please make sure that WAMP Server is installed and configured correctly on your system and that you can access the WAMP Server page by typing http://localhost in your browser.

There is just one more step we need to take before we start coding. Browse the folder where you installed WAMP Server. By default, this will be C:\wamp. Open the 'www' folder and create a new folder here called 'php_testbed'. This folder will have all the PHP codes we write henceforth. Now, we are ready to start coding.

Open Notepad++ and create a new file. Save this file as index.php in php_testbed folder and type the code given below.

Hello World in PHP
In the above code, we can see that the page has the default HTML start-up code with the page title and body. The PHP code is written within the body tag of HTML. The 'echo' is a PHP function that basically sends text, values and a variety of other things to the browser window. In our case, we have asked to print the words 'Hello World' by enclosing them within double quotes. The statement ends with a semicolon.

Now, open your browser. Type http://localhost/php_testbed in the address bar and press Enter. You should see an output like this.

PHP HelloWorld Output

Congratulations! You have just written your first piece of code in PHP and also executed it successfully in a browser.

Integrating HTML with PHP


HTML can be used within a PHP block of code to add some styling to the page. Here is a practical example of this.

HTMLwithPHP
In the above code, we are using H2 tag to stylize our 'Hello World' line. This is followed by an echo statement that creates a new line by using BR tag. Finally, we use H3 tag to stylize 'Hello World Again' line. The output of this program looks like below in a browser.

HTML with PHP Output
In this way, we can see how HTML codes can be integrated with PHP codes to stylize our pages. If you view the source of the page in browser, you will notice that none of the PHP codes is visible and that the content is formatted to the corresponding HTML tags.

So, this was the first step towards writing PHP codes. In the next article, we will tell you how constants and variables are defined and used in PHP. Also, we will tell how values can be passed between web pages.

Read Set up your own web server using WAMP


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: