Hrvatski

printer friendly version

Introduction to PHP

If you want your website to become dynamic, you should consider learning a web programming language. PHP (Hypertext PreProcessor) is such a language, and if you have a little affinity to programming, you will find learning PHP very easy. This is a basic tutorial that will teach you how PHP works, and how to write your first PHP script.

For completing this tutorial you must have Apache server and PHP installed on your computer or if you don't want or can't do that, you will need to upload the .php files (naturally, files containing PHP script must have the extension .php) to the remote server (host) that supports PHP and try out if they work. Even some free hosts support this feature. If you wish to install Apache and PHP, you can find preconfigured installations here.

Structure

Each PHP script begins with a sign <?php and ends with ?>. Everything you write between those two signs will be executed by the server.
There are also brackets { } that separate parts of code into blocks. This is an example script that does nothing.

<?php
	{
	}
?>

The comments (parts that will not be executed) are marked with // or /* */

<?php
	// This is an example
	// script that does
	// nothing.

	/* these lines won't be parsed,
		either */
?>

When we want to display something on the screen, we use the echo function. We can also use print() because they are very similar. If we want to display a word, we must say to the parser (if you don't know what parser is, here's what WordWeb says for the word parse: Analyze syntactically by assigning a constituent structure to (a sentence)) that what we are trying to print out is our custom text, so we put it inside quotes ' or " (there is a slight difference), like this:

<?php
	echo 'cute';
?>

This is the equivalent using print() function:

<?php
	print('cute');
?>

Note the semicolon ; on the end - it tells to the parser that this is end of our instruction. If we write more instructions (lines), each must end with a semicolon.

<?php
echo 'Kittens are cute'; echo ':)'; ?>

You probably think the output will look like this:
Kittens are cute
:)

but it won't. It will look like this: Kittens are cute :)
To insert a new line, you must do exactly what you do in HTML — insert a line break.

<?php
	echo 'Kittens are cute<br>:)';
?>

If you check out the source, you will see that it looks like this: Kittens are cute<br>:). But what if you want a nice look of source code where you will not get lost, with each line of text in new line? You will need to write \n so PHP will know where to insert an end of line marker.

<?php
	echo "Roses are red<br>\n
		Violets are blue<br>\n
		God made me pretty<br>\n
		But what happened to you?";
?>

Now the source code will look neat and tidy like this:

Roses are red<br>
	Violets are blue<br>
	God made me pretty<br>
	But what happened to you?

See that I'm using different quotes now? It's because these quotes allow parsing of special characters and variables. If we wrote it using single quotes, it would accept the characters \n like ordinary letters, and the output would be:
Roses are red\n
Violets are blue\n
God made me pretty\n
But what happened to you?

But let's suppose you want to write a bit advanced HTML code, with quotes. Surely it wont work if you just write the quotes, because you will close the string by opening a new quote. So, you must put the escape character — backslash \ — before the quote that has to be printed out, so the parser knows that it's not part of the code, but part of the string.

<?php
	echo "<div class=\"box\">
		Roses are red<br>\n
		Violets are blue<br>\n
		God made me pretty<br>\n
		But what happened to you?
		</div>";
?>

The alternative to this is using single quote ' that is in hierarchy higher that the double quote.

<?php
	echo '<div class="box">
		Roses are red<br>
		Violets are blue<br>
		God made me pretty<br>
		But what happened to you?
		</div>';
?>

Variables

Variable is a symbol (like x or y) that is used in mathematical or logical expressions to represent a variable quantity. In PHP, variables are marked with a dollar sign $, like this: $number, $letter, $fruit, $color ...
This is how we give variable it's value, and then print it out.

<?php
	$number = 1;
	$fruit = "apple";
	echo $number $fruit;
?>

The above code will display 1 apple

If we want to combine our text with variables, here's how we do it:

<?php
	$fruit = "apple";
	echo "My favourite fruit is $fruit";
?>

This code will display My favourite fruit is apple

Operators

There are two kinds of operators, mathematical and logical. Mathematical operators (you learned them in school) have a certain numeric value as a result. Logical operators have values true or false. In PHP there is one more operator, a dot . - dot is used in connecting strings.
If we write this:

<?php
	$fruit = "apple";
	$fruit = "and strawberry"
	echo "My favourite fruit is $fruit";
?>

...this will print out My favourite fruit is and strawberry. But if we write this:

<?php
	$fruit = "apple";
	$fruit = $fruit . "and strawberry"
	echo "My favourite fruit is $fruit";
?>

...this will print out My favourite fruit is apple and strawberry. Note that this can be written shorter:

<?php
	$fruit = "apple";
	$fruit .= "and strawberry"
	echo "My favourite fruit is $fruit";
?>

So, that's how the dot works. Mathematical operators work the same way as in math you've learned, so I won't write much about that, I will only give one example:

<?php
	$num1 = 1;
	$num2 = "3";
	$num3 = 5;
	$result = ($num1 + $num2) * $num3;	/* (1 + 3) * 5 = 20 */
	echo "The result is $result.";
?>

The result is 20. Note that it's the same whether we put the number in quotes or not.

Logical operators are useful when we need to check the values. Here is how we translate logical operators into "normal" language:

!not
&&and
||or
==equal to
!=different than
<smaller than
>larger than

We cobine those operators with if() and while() functions. I will describe the if() function now, but I will tell you more about while() function in my next tutorial, about loops.

<?php
	$num1 = 1;
	$num2 = 5;
	if(($num1 > 3) && ($num2 > 3)) {
		echo "Both numbers are larger than 3.";
	}	/* end if */
	elseif(($num1 > 3) || ($num2 > 3)) {
		echo "One of the numbers is larger than 3.";
	}	/* end elseif */
	else {
		echo "None of the numbers is larger than 3.";
	}	/* end else */
?>

First it checks if both numbers are larger than 3. If not, it skips the block inside the brackets and checks if at least one of the numbers is larger. If not, it skips the block and prints out that none of the numbers is larger. This script will print out One of the numbers is larger than 3.

I hope this tutorial was helpful. I wrote some other tutorials too, so now you're ready to check them out. If you want to find out more, feel free to visit the PHP homepage.


Share this tutorial with your friends!





My new book is out!

The Human Centered Brand by Nela Dunato


Branding & web design by Nela Dunato

Facebook