Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PHP Development Part 1 – Variables

Saturday, January 14th, 2012 by Servage

The Servage support team seems a lot of requests involving programming issues. Mostly involving the PHP language. Many of the issues could easily be resolved with more programming knowledge, so I’ll try to spread the basic concepts of PHP programming a little further, by publishing a little PHP course.

The first part will be about the absolute basics on how to get started with PHP. Consider the following example PHP file:

<?php
$job_title = 'Developer';
$annual_income = 60000;
$income_per_month = $annual_income / 12;
echo 'Monthly income for a '.$job_title.' is '.$income_per_month;
?>

We will go through this line by line.

The <?php statement declares that this file is a PHP file. This is necessary for the system to know, which interpreter to use here. Beware that some tutorials may include abbreviated <? declarations. You should avoid that and use the full <?php version.

$job_title = ‘Developer'; declares a variable called “job_title” with the value “Developer”. Variables are always used with a dollar sign ($) in front. The value “Developer” is a word (string), and must therefore be enclosed in quotes. In PHP you can enclose text-strings in ‘ and ” quotes. Beware that the line is ended with a semicolon. Every line in PHP must be ended this way, like you see in the other lines – unless one statement uses multiple lines. You’ll see examples of that later on.

$annual_income = 60000; declares another variable. The difference from $job_title is that this one is a number, therefore the number is written without quotes. If you would use quotes for numbers, the number would be interpreted as a text-string – and then PHP would not be able to perform calculations with the variable, like we do in the next line.

$income_per_month = $annual_income / 12; declares another numeric variable, which is the result of $annual_income divided by 12. This is the basic syntax for math operations in PHP. You can use the basic operators like plus, minus, division and multiplication, as well as brackets and a plethora of math functions like sin() and cos(). More about that later.

echo ‘Monthly income for a ‘.$job_title.’ is ‘.$income_per_month; writes output on the screen, including some text and the value for the $income_per_month variable. This is the basic way to display output in PHP.

See you in part 2 of the PHP development course!

PHP Development Part 1 - Variables, 5.0 out of 5 based on 3 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.