How to Code PHP Part 1

Do you want to learn How to Code PHP? Get your thinking caps on and let’s start cranking the PHP Engine.

PHP is a programming language that allows you to process and react to user actions on your website. While JavaScript allows you to process actions directly on a visitors computer, PHP requires that actions are processed on the server in which your website resides.

PHP is most commonly used to access databases on the server that hosts your website. Please read my SQL Tutorial before you read this article, or you may get lost. This tutorial will cover most all you can accomplish with PHP, but will very specifically cover how to interact with a database with PHP.

The Basics of PHP

Like my other tutorials, I will provide you with working PHP Sources you can use, but for now I’ll focus on the basics.

You can embed PHP code directly in your web pages by surrounding the code with an opening <?php and closing tag ?>. You also want to give your web page the PHP extension .php, when you embed PHP code in a web page.


You define variables, storage areas with a defined name, by starting the name with a dollar ($) sign and then a name. The first character following the dollar sign must be a letter or an underscore(_). There after you can use a series of letters, numbers, or under scores. It is considered good for to separate multiple words in a variable name with underscores. Ex:

  • $text_for_output // Good Variable Name
  • some stuff // Bad Variable Name: Missing $ and No Spaces Allowed
  • $_other_stuff // Valid Variable Name
  • $123abc // Bad Name: Can’t start the name with a number, name not descriptive either

You assign values to the newly created variable with the equals operator (=). $my_name = “New Think Tank”;

If you want to define a variable with a value that will never change (Constant), you would do that with the define() function. define (‘pi’, ‘3.14’);

Printing Information in the Browser

You can easily output text to a web page by using the echo or print functions. Here is an example:

  • echo ‘This text brought to you by the echo function.’;


  • print “Brought to you by the print function.”;

You can use single or double quotes with both of these functions, but the data contained between them is handled differently.

  • Single Quotes: Are used when you want exactly what is typed to be displayed. If you place a variable within single quotes, the name and not the value of the variable will be displayed. For example echo ‘Show Variable Value $some_number’, would print to screen: Show Variable Value $some_number. Also single quotes ignore escape characters, which I’ll describe in a moment.
  • Double Quotes: Double quotes don’t blindly print what is within them. echo “Show Variable Value $some_number”, would print: Show Variable Value 3.14. When double quotes are used, variables are spotted and their values automatically replace them before they are printed to the screen. Double quotes also pay attention to Escaped Characters.

Escaped Characters

Sometimes you are going to want to echo text to the screen that is protected. If you want to echo a quote ( ‘ ) to the screen, you may confuse the browser if you place that quote between two other quotes. The browser won’t know when you text block ends. So, how do you use these protected characters? You follow them with a back-slash ( \ ). Here is a list of protected (escaped) characters:

  • \\ : Backslash
  • \r : Carriage Return
  • \” : Double Quote
  • \$ : Dollar Sign
  • \n : Newline
  • \t : Tab
  • \’ : Single Quote

Now you can place these protected characters in your double quoted strings, for use with the echo and print functions. Just remember these only work when you use double quotes


It is a very good idea to start your code with a full description of what the program does. You should then follow up by writing comments that describe each function and any statements you might find confusing a year from now. This is so that you and others will be able to easily jump in years later to make any changes that are needed. Believe me there will be changes and you will forget!

You can comment out notes on your code for a full line by proceeding the comment with two forward slashes ( // ). If you need to make a multi-line comment start of with a forward slash, star ( /* ) and end the comment with a star, forward slash. You can also comment out a single line with the pound symbol (#), but this isn’t often used.

Just so you are aware, you create comments in HTML, with a starting <!–, and end it with a –>. For a full tutorial on HTML see my HTML W3C Tutorial.


Strings are just a quoted chunk of letters, numbers, punctuation and white space. The information you were outputting to the screen with the echo and print functions were strings. You create and assign strings to variables with the equals operator ( = ).

You can join two strings with the dot (.) operator like this:

  • $house = ‘Windows’ . ‘ Bricks’; // $house is now equal to ‘Windows Bricks’
  • $location = $city . ‘, ‘ . $state; // $location is equal to ‘Chicago, Illinois’ if that was the value of the variable $city and $state

If you want to find the number of characters in a string, you can use the strlen() function. $chars_in_string = strlen($location); The value returned would be 17, the number of characters in ‘Chicago, Illinois’.

I’ll show you some other useful string functions later in the article.

  • Working with Numbers

There are numerous functions you can use to work with numbers in PHP. You define them in the same way as other variables: $pi = 3.14; You can also perform these basic operations on them:

  • + : Addition
  • – : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus : Returns the remainder of a division. 5 % 2 = 1
  • ++ : Increment : A shorthand way to add one to a value. $stuff ++; // This statement would add one to the value of $stuff
  • — : Decrement : A shorthand way to subtract 1 from a variable.
  • += : Shorthand Addition : $stuff += $dog; // This statement would add the value of $stuff to the value of $dog and assign it to the variable $stuff
  • -= : Shorthand Subtraction : $stuff -= $dog; // This statement would subtract the value of $dog from the value of $stuff and assign it to the variable $stuff

PHP Math Functions

PHP has many built in math functions. Here are the most commonly used:

  • abs(x) : Returns the absolute value of a number
  • ceil(x) : Returns the value of a number rounded up to the next highest integer
  • exp(x) : Returns the value of E^x
  • floor(x) : Returns the value of a number rounded down to the nest lowest integer
  • is_nan(x) : Returns true if the value passed is a number and false otherwise
  • lcq_value() : Returns a random number between the values of 0.0 and 1.0
  • max(x,y) : Returns the number with the highest value of the two numbers you send it
  • min(x,y) : The opposite of max()
  • pow(x,y) : Returns the value of x^y
  • round(x, decimal_place) : Returns the value of x rounded to the defined number of decimal places
  • sqrt(x) : Returns the square root of a number
  • There are also many inverse trigonometric functions. If you don’t know what that means don’t worry about it. If you do then you know how to use them.


An Array is a variable that can hold multiple separate values. If you think of a variable as a box that holds one value. An array is like a big box with many boxes with values in it.

Arrays are structured as a series of key value pairs. The key part is like a note on a box that says Underwear. In the case of an array you could give each box a name like Underwear and other things like that. By default the first value you enter has the key value of 0 and proceeds upwards numerically.

You assign a value to an array like this $dogs[3] = “German Shepard”; Now the 4th box in the array has the value “German Shepard”. Remember the boxes start with the value of zero that is why the 3rd box is actually the fourth box. Get it?

To output the value of an array: echo $dogs[3]; That’s it.

You can cycle through and output the values of an array with the foreach function, like this:

foreach ($dogs as $dog_name) {
echo $dog_name;

The foreach function will keep adding one to the value of the array and then the echo function prints them to the screen. In this example the name of the array $dogs is followed by the keyword as and then the variable you want to temporarily use to hold the current value being issued by the array $dog_name. When placed in the foreach function, every statement called between the opening and closing curly braces { } will be called.

Multidimensional Arrays

You could also create arrays of data inside of other arrays. Let us say you want the dogs array to be part of your animals array. This is how you would do that:

$animals = array ( ‘DOGS’ => $dogs, ‘CATS’ => $cats);

Here we assigned keys to two arrays named $dogs and $cats and then passed them into the array named $animals. If I wanted to echo a value from the $dogs array I’d do it this way:

echo $animals[‘DOGS’] [3]; // This would print out the value German Shepard, that I assigned to it above.

Working with Arrays and Strings

It is common to use Arrays and Strings together, so here are a few commonly used functions :

explode(separator, $string): Turns a string into an array. You define what character in the string you want to separate the values that will be inserted into the array. Then also pass the string to separate. The separator is normally a comma, space, tab, semicolon, or other white space or punctuation mark.
implode(separator, $array): Turns an array into a string. Here you define what you want to separate the array values that will go in the new string. The separators used are the same as I just described.

Sorting Values in Arrays

It is also common to sort your array values. Just understand that when you do your keys will stay the same and just the values will be sorted. That is why most people allow their keys to remain the default numbers, like I described above.

The sort($array) function will sort the values in ascending or alphabetical order. The rsort($array) will sort the values in the opposite direction.

That’s All Folks

We’ll we came to the end of my first PHP tutorial. If you have any questions leave them in the comments section below. Prepare yourself for a ton of code examples in future articles.

Think Tank

3 Responses to “How to Code PHP Part 1”

  1. cna training says:

    Great information! I’ve been looking for something like this for a while now. Thanks!

  2. kuldeep says:

    Hi Derek,

    Which IDE shall I use for PHP development. Moreover, how can I integrate it to zend framework ?



  1. How to Code PHP: New User Registration Scripts | new think tank - [...] How to Code PHP Tutorial [...]

Leave a Reply to cna training Cancel reply

Your email address will not be published.