Creating Your First PHP File – Part II

PHP-Banner-2

In the previous part of the course, we have learned about the basics of PHP and how to set up a local web server using XAMPP. Now, we are officially ready to create our first PHP file and run it in our web server.

In this part of the course, we will learn in-depth about creating a PHP file, printing data in it and testing it in the web server. It is very important to give your full attention right now, because if the basic foundation becomes weak then everything you try to do later will become much more difficult. We will also learn a bit about the importance of maintaining proper coding standards through commenting and indentation.

5 Simple Steps in Creating Your First PHP File

These are the 5 most important steps you need to follow to create your PHP file. I have tried to keep the discussion as simple and in-depth as possible, by dividing the entire process into 5 interesting steps.

i)    Now, as you have got XAMPP installed and ready in your computer, the first thing you need to do is navigate to the “xampp” folder. The default location of the folder is C:xampp but the location may change depending on where you choose to install the software.

xampp-folder

In the xampp folder, look for another folder named “htdocs”, as it is the root folder and whenever you type http://localhost (as discussed earlier) in your browser, the index file from this folder is loaded first. So every PHP file we create, needs to be placed inside this “htdocs” folder. We can obviously create subfolders inside it if we want to categorize our files.

htdocs-folder

I have created a folder named “PHPCourse” and every PHP file I create for this course will be placed on that specific folder.

ii)    Now, you need an editor to write PHP files and save them. Even the simplest text editors like Notepad is enough to create full-fledged PHP files. But it’s a wiser choice to use editors which supports PHP so that you can enjoy various nifty features, like Syntax Highlighting, Code Indentation, etc. while developing PHP files.

You can head over to this dedicated article on various PHP IDE’s (Integrated Development Environments) available for you to choose from. I personally use ConTEXT (as I am using it for a long time), but you can obviously choose from other better options available.

context-editor

Open ConTEXT and the first thing you would like to do is save your blank file as a “PHP file”. It is a good practice to save your file just at the beginning as it helps ConTEXT recognize the file as a PHP file and enable various PHP related features for it and also help in quicker saving (Ctrl + S) while writing your PHP files.

iii)    Remember to save your file inside the “htdocs” folder and inside any other subfolder if you want to. You can name it with any meaningful name as you desire, but if you use the name “index.php” then you can just type http://localhost or http://localhost/myfolder to open your file. You do not need to type the full name (with the file name and extension) like this, http://localhost/index.php or http://localhost/myfolder/index.php, if you use the name “index”.

localhost-directory

“index.php” or “index.html” is the first file that the browser looks to open, but if you use any other file name and type http://localhost/myfolder then the browser opens up the directory and you need to click on the file name and open it or put the entire name(with the filename and extension) directly.

iv)    Now you need to type your PHP tags and other codes inside you PHP file. Let us look at the code,

<?php?>

If you look carefully, it represents a mirror image with ‘h’ in the middle. Now, you must be confused as how to write codes inside this tag? But you shouldn’t be as things are really simple if you just press the ”Enter” key after the word “php”.

Now it looks something like this,

<?php
// your first comment here
?>

php-tags

There is also a shortcut way for creating PHP files by just omitting the word “php”, like <??> which seems something like this,

&lt;?
// your first comment here
?&gt;

But I personally recommend you to use the first syntax always as it looks better and less confusing.

v)    Now you have successfully created your first PHP file, but unfortunately it’s just a blank page. No problem, as there are lots of things that we are going to learn and put inside our PHP files in the later parts of the course.

Till now we should be happy with our blank PHP file and should move ahead to test it in our local server. I have saved it as “firstfile.php” inside the “PHPCourse” folder. So, just type in http://localhost/PHPCourse/firstfile.php and your first PHP file should be ready in the browser.

blank-php-file

An important thing to note here is that if you try to view the source code of this file in the browser then you will have nothing more than a blank page, just because PHP is a server-side scripting language and is fully interpreted in the server.

Printing or Echoing Data on a PHP file

You have learned quite in-depth about how to create a PHP file and view it in the browser. But unfortunately it was just a blank page with no data being displayed in it. So, now it’s time for us to print some real data in our page.

&lt;?php
echo “Welcome to the PHP for Beginners Course”;
?&gt;

Here we have used “echo” which is a language-construct (not a function) which can be used to print strings (sequence of characters) or variables in our PHP page. We can use either single-quotation marks or double-quotation marks to display data, but we will learn about their proper use as we progress in the course. We must use a semi-colon after the quotation marks as it marks the end of the statement and if you forget to use it, then you will surely run into some errors.

print-data-php

There is another way to print data other than “echo” too, which is by using “print”.

&lt;?php
echo “Welcome to the PHP for Beginners Course”;
print “Only on corePHP.com”;
?&gt;

The “print” is also a language construct and not essentially a function, so you do not need to use brackets with its arguments. But “print” is an older method and is slower than “echo”, but it’s something worth knowing. Always use “echo” if you want to print some data in your PHP file.

print-data-php-2

Proper Coding Standards – Code Indentation and Commenting

Before starting to develop larger PHP files, it is very important for us to learn a bit about the proper standards of coding. It’s not like that your PHP files won’t run properly if you do not follow the proper coding standards. But if you want to be a professional developer, then you must learn, adapt and follow the coding standards.

Code Indentation is something which will make your code look good, less ugly and easily comprehensive to other people. Generally, you will be working in a team with many other developers together and it can become a seriously painful experience for other developers to understand and debug your code if you do not follow proper code indentation. If you are using a good IDE, then it can help to automatically indent your code for you.

proper-code-indentation-php

Commenting is also a very important and good practice that you must follow to attain better coding standards. Suppose you are developing a large application with lots of functions, and later while debugging your own code you forget what a particular function does, then a single line of comment can save you a lot of time and help you understand what your code actually does.

Commenting also helps other developers understand your code better and quicker. So, these are the two most important things that you must put importance on, to attain proper coding standards for your code.

The Final Words

So in this part of the course, you have learned to create a PHP file and run it in your web server. You have also learned about “htdocs” folder and how to put your PHP files there. You have also learned about the two ways of displaying data in your page using “echo” and “print” statement. Finally, you have learned about the importance of maintaining proper coding standards and how should you attain it.

In the next part of the course, we would dive deeper into PHP by creating more interesting and involving PHP scripts. Stay tuned and see you in the next episode. Happy coding!

Aritra Roy
Aritra Roy is a Blogger, Freelance Writer, Designer and Online Entrepreneur who believes in the power of written words to educate, influence and inspire people.