Easy PHP I: Introduction

Introduction

I’ve been involved with PHP since I got into the web design and development world. This is because since in my beginnings I chose to use WordPress as a CMS to build websites with and since that day I have been needing PHP to build WordPress themes (and plug-ins lately). I have learned a lot of PHP tips and tricks, but I have not yet been able to study it regularly and eventually an advanced programmer as I aim. Hence I would love to be fluent in PHP and I know that it would open a whole new business world in front of me. So, in order to push myself into the academical learning process, I will be writing down my learning experience into tutorials. In this way I hope to kill two birds with one stone: learn deeper PHP for myself and let others learn faster and hopefully better through these tutorials too. So, let’s get the hands dirty.

What the Heck is PHP

To get started we need to know what we are talking about. There are two different opinions as where PHP derives from. The first one tells us it stands for “Personal Home Page Tools” and the second one stands for “Hypertext Preprocessor”. No matter where it derives as a acronym, PHP is server-side programming language. It is the one responsible for taking our requests to the server and bringing to us what the server gives it. PHP can be embedded within your HTML and vice versa which is something that makes it a great language. PHP file extension end in “.php”. PHP supports a ton of databases such as: MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc, but it seems to have chosen the first one, MySQL, to marry with. The last but not the least, PHP is an open source software and is free to download and use.

Why Should I Use PHP

PHP is free, open source and has a great community after it. PHP is now mature and can fulfill your every wish. Furthermore, it’s aimed for the web and it gets to work done. PHP is compatible with almost all servers used today (Apache, IIS, etc.) and almost every hosting company offers PHP enabled web hosting service. PHP is also easy and fun to learn as these tutorials will prove it. Moreover, PHP is so popular that if you’re looking for a career in the web design/web development industry then you just have to know it. Final word: it has proven itself over the years to be one of the best options for dynamic web applications.

Am I Ready to Learn PHP

Off course you are! However, you need to have a basic understanding of (X)HTML. That is all you need. However, knowing some JavaScript and CSS would make the situation a lot more fun.

How to Start with PHP

You need a developing environment to write and run PHP code. Basically you need to have Apache Server, MySQL and PHP installed on your computer. You can install them separately, but it is not a wise go. The best option to start with whether you are a Linux, Mac, Windows or even Solaris user is XAMPP. XAMPP is a compilation of Apache server, MySQL and PHP and a bunch of other applications like PHPMyAdmin, all-in-one. As I mentioned, it is cross-platform and really easy to use. I use it myself and I love it. You can download at ApacheFriends.org. However, there are other options that you can consider like : WAMP specifically for Windows found at WampServer.com. MAMP specifically for Mac found at Mamp.info. Pick and choose!

Basic PHP Syntax

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. Below, we have an example of a simple PHP script which sends the text “PHP is cool, right?” to the browser:

<html>
<body>
<?php echo "PHP is cool, right?"; ?>
</body>
</html>

Each code line in PHP must end with a semicolon (;). The semicolon is a separator and is used to distinguish one set of instructions from another. Also, as you can see the text is wrapped with double quotes. We can use single quotes in its place too. There is a slight difference between double and single quotes that we are going to discuss in the next tutorial. Also, there are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text “My PHP Script”. Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.

Comments in PHP

Commenting your code is a programming virtue. It is a practice of best programmers and one of principles of programming engineering. Commenting your code helps you to understand what you have been writing in the past as you might forget programming tricks and functions once in a while, also, it help others to expand and improve your code easily. So, always comment your code.

In PHP, we use // and # to make a single-line comment or /* and */ to make a multi-line comment block. Have a look at the example below.

<html>
<body>
<?php 

//This is a single line comment 

#This is a singe line comment too 

/* This is a comment block. 
Works the same way as commenting
 CSS code. */ ?>

</body>
</html>

This is how PHP looks like, but this is only the first step. To learn more fun things about PHP, read the next tutorial. Note: If the tutorial name does not have an active link, it means it is not posted yet, so stay tuned.

What is next:

  • PHP Introduction
  • PHP Variables
  • PHP Operators
  • PHP Arrays
  • PHP Loops
    • PHP If … Else
    • PHP While
    • PHP Foreach
    • PHP For
    • PHP Switch/Case
  • PHP Functions
  • PHP Forms
    • $_GET
    • $_POST
    • $_REQUEST
  • PHP Cookies
  • PHP Sessions

Resources:

  1. PHP Introduction: http://www.w3schools.com/PHP/php_intro.asp
  2. PHP Syntax: http://www.w3schools.com/PHP/php_syntax.asp
  3. Learn PHP from Scratch: A Training Regimen: http://net.tutsplus.com/tutorials/php/learn-php-from-scratch-a-training-regimen/

Case