Category : Code

Code

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

Read More
Code

Terminal Tip #1 – Aliases

This is my first Ubuntut, and for it I’d like to start off with a productivity tip. We’re going to create a bunch of aliases (shortcuts) for commands we use often.

The first step is to open up your user’s .bashrc file, conveniently located in your home directory with all the other configuration files. The bashrc file is a hidden file and its determines the behavior of interactive shells. For those of you who are adept at copying and pasting here is the code to get back to your home directory and open the file in Gedit.

Read More
Code

Practical Python Programming

Welcome to my Practical Python Programming post, hopefully this will be the first of many. If you have been following my  ‘An Introduction to Python Programming’ posts series, then you may already know that in this post we are going to be having some fun. We are going to be creating a text based ‘Guess the Number’ game. All of the knowledge needed to create this game is contained in the  ‘An Introduction to Python Programming’ posts, so as long as you have read those you should be able to follow this with relatively ease.

Read More
Code

An Introduction to Python Programming #4

Welcome to my forth Python Programming introduction post. In this post we are going to be looking at lists and random numbers.If you have used another programming language before, then you have probably heard of an array, well, a python list is just an array, and in the same way you can have multi dimensional lists just as you can have multidimensional arrays.

Read More
Code

An Introduction to Python Programming #3

Welcome to my third Python programming introduction post. In post #2 we looked at the basics of flow control using conditional statements. In this post we will be taking a look at another fundamental aspect of flow control; loops. A loop is a way of running a particular block of code more than once based upon a conditional. For example, we could have a loop ask a user for input continuously until they input “Exit”. That may not seem very useful but its just an example, loops are an extremely useful tool and definitely should not be overlooked. The two types of loops that we will be looking at are the for loop and the while loop.

First off, the for loop. The for loop is usually used for iterating a particular number of times. The usual first demonstration of a python for loop uses the range(number) function. The range function when run outside of a loop returns a list object containing (depending on the arguments given) numbers from (unless stated otherwise) 0 through to number. So, lets take a look at a for loop in action. In the following code we will be printing out the numbers 1 through to 100.

j = 1
for i in range(100):
    print j
    j += 1

This will print out, on separate lines, the numbers 1 through to 100. Lets pick this code apart. Firstly we have the variable declaration, pretty simple. The next line is the loop, and this is the slightly confusing part.

for i in range(100)

What this means is, for every object in the list, assign that object to the variable i, then iterate over the following code block. After the code block is complete, the next object in the list is assigned to the variable i and the code block is run again. This process is repeated untill there are no more objects in the list, thus it iterates 100 times. The next 2 lines are the code block.

print j
j += 1

This should make sense, first print the variable j then increase j by 1. Hopefully this makes a bit of sense, if it doesn’t, don’t fret, play around a bit, try different numbers and variables.

Next we are going to be taking a look at the while loop. The while loop, unlike the for loop, will iterate continuously until its condition is met. This type of loop often causes problems for new programmers because it can easily be used to create infinite loops, loops where its conditional will never be met, we will look at an infinite loop after we have looked at a basic implementation of a while loop.

So, the while loop, below is an example of a while loop performing the same task as our previous for loop

i = 1
while i != 101:
    print i
    i += 1

As you can see, there is not much difference. The main difference is in the line while i != 101 what this means is that while the variable i does not equal 101, print the variable then increase it by 1. Now, as promised, lets take a quick look at a slight variation of this loop, an infinite loop.

i = 1
while i != 101:
     print i

That will continuously print 1 until stopped by the user. Another, more interesting infinite loop would be this

i = 1
while True:
    print i
    i += 1

This loop will continuously print the variable i incrementing it by 1 each time. The reason this loop is infinite is because the loops conditional is True which is what a positive conditional returns, thus, an infinite loop.

Infinite loops are not all bad, there are many very valid reasons for using them, but be careful. In some compiled languages your program will segfault when you write an infinite loop.

I hope this post has helped you in your journey to becoming a python programmer. I will continue to write more introductory posts. Also, look out for my upcoming ‘Practical Python Programming Post’ which will be taking a more advanced approach (Suitable for beginner > intermediate programmers) looking at how python can be useful in everyday Ubuntu life.

Thanks for reading,

Read More
1 2 3 4 5 6 8