Introduction and Basics of PHP

PHP

PHP is a very popular web programming language used for building dynamic websites. When combined with HTML, CSS and MySQL databases it becomes a very useful language for building websites. If you want to develop websites, then PHP becomes a basic requirement.

I have been using PHP, MySQL, HTML and CSS for developing websites for a really long time. But in those years, I never took my time to take proper classes. All I did was downloaded scripts, assumed what the codes did and Googled my way to modifying those scripts to fit my needs. I have been doing this for years now and I got very good at it. I even understood basic PHP.

I’ve finally become motivated enough to learn this programming language properly, step-by-step. I will be posting a series of blog posts from now on which will be my notes in learning PHP. I am sharing those here so that someone else might find it useful.

The Basics

To learn PHP, you must install XAMP, WAMP or LAMP in your machine. XAMP is the best one considering the fact that it supports multiple platforms. Download and install XAMP like you would install any other program in your machine. Then start the service and you are good to run PHP scripts in your laptop or desktop. If you plan to run PHP scripts for your websites, then get a webhosting that supports PHP. You will find lots of those.

To Test Your Local PHP Server, just type:

http://localhost/ on your browser’s address bar

If you don’t get an error and see a website or some information, then you’ve successfully installed PHP. It does not require you to have an active internet connection.

To Write Down Your PHP Code

Just use a text editor like Notepad++ or Sublime Text 2. They are free and are great for PHP programming. Text editors like these highlight your code making them easier to read, auto-complete and intend your code. Overall, these advanced text editors make it easier for you to navigate through your code.

Writing Down Your First Code

I think it is a trend to write down a hello world code for any programming language. Here’s how you do it in PHP:

<?php
echo "Hello World";
?>

Save a file containing the code above as “helloworld.php”. Your PHP files must end in .php. Filename doesn’t matter. Save it in your localhost htdocs folder (For XAMP) and hit http://localhost/helloworld.php in your browser. You will see Hello World and that tells you that your code has worked. You can replace echo with print but echo is more common in programming.

Also, from the basic code above, we can tell that php codes start in <?php and ends in ?>. Depending upon your PHP configuration, you can also start with just <? or <?= and end in ?>. Other supported format include <% and %> or <%= and %>. Not all of these work by default. You can have multiple blocks of such code in a single php file.