TheTAZZone - Internet Chaos

Tutorial – PHP for Newbies 1

Tutorial – PHP for Newbies 1

This series will be aimed at helping people who are brand new to programming get into PHP. As I’ve researched, I’ve found that good tutorials for people new to programming are few and far between.

Prerequisites

Before we can begin, PHP requires that you have a web server, a database, and the language itself all running locally. Accomplishing this could be another full tutorial itself, but I’ll attempt to give the crash-course version.

By far the most popular platform for programming using PHP combines Apache (the web server), MySQL (the database), and PHP. In fact this is often abbreviated as “AMP”. When this is installed on a Windows machine it’s called “WAMP” and when installed on a Linux machine it’s referred to as “LAMP”. To install all of these at once on an Ubuntu machine, you have two options. You can either type:

Code:
sudo tasksel install lamp-server

or

Code:
sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server

See also: viewtopic.php?f=31&t=11912

If you want to keep things extremely simple on a Windows machine, try http://www.easyphp.org. I installed easyphp on a jump drive for when I’m on the go or when working on Windows. Just extract, install, start the program (the .exe file), and you’re good to go. Make sure to save your .php files in the www folder within the easyphp directory (wherever you installed it).

Once you have each installed and somewhat configured, you will save all of your PHP work in the Apache root directory (linux default /var/www) and to display it you will navigate in a browser to http://localhost/filename.php.

Introduction

Unfortunately, learning the basics involves a lot of memorization at first. There’s no way around that, but I’ll try to apply as much of it as possible as I go.

What is PHP?

PHP stands for PHP: Hypertext Preprocessor. Yes, it is an acronym contained within another acronym. PHP is a programming language that is most often found integrated with HTML to create dynamic, interactive, and secure web sites. PHP is completely free and open source. PHP is parsed (executed) by a web server and then delivered to the user’s web browser, which serves to hide your code and making it inherently more secure than HTML or Javascript.

What does PHP look like?

PHP code usually (but not always) begins with <?php and ends with ?>. Everything contained within these brackets is interpreted as PHP code by the web server. The rest (variables, functions, etc) will be covered later.

How do I go about creating a PHP file and integrate it into a web page?

There are a variety of ways, but the easiest to demonstrate is to embed PHP within HTML. PHP is at home embedded within HTML. For example, the following is perfectly legal:

Code:
<html>
<body>

<?php
echo “Hello World”;
?>

</body>
</html>

You could save the above in a file called helloworld.php inside the web server’s (apache’s) root directory (default is /var/www) and browse to http://localhost/helloworld.php and you should see “Hello World” at the top of the page.

Comments

Comments are used inside the <?php ?> brackets to signal to the parser that one or many lines should be ignored. Comments allow you to leave notes for yourself or others. You would denote one line as a comment with // and a block with /* and */ The below examples are all valid comments:

Code:
<html>
<body>

<?php
//This is a comment

/* All of the following is a comment too: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.. */

echo “Hello World”; //This comment is at the end of a line.
?>

</body>
</html>

Keeping Clean Code

Thus far you may have noticed that I tend to break the code up into a lot of different lines. The PHP parser (as well as HTML) treats everything as one single line of code, so it actually makes no difference to the parser if you have zero line returns or a hundred between each word (with the exception of line comments, which require you to comment out each individual line with “//”). The reason I do this is because it’s extremely helpful to the coder to break it up so it is easier to find errors and generally understand the code.

Variables

Variables serve as “containers”, much the same as they are in algebra for example. For example, in algebra x may equal 5. In PHP, you could make x = “This sentence” or (8 * 15) + 27. You can define variables to be whatever you want them to be, so long as PHP syntax is not broken. PHP variables are denoted by placing a $ in front of the name of the variable you are assigning. Variables names can be anything so long as they begin with a letter or an underscore “_”, contain only letters (upper or lowercase), numbers, and underscores. They cannot contain spaces. An example might be $My_Variable. Variables are case sensitive, so $My_Variable is not the same as $My_variable. For example, it would be perfectly legal for $My_Variable to equal the number 7 and $My_variable to equal the number 57 (or a bunch of text, or pretty much whatever you want). This is useful because when programming you often refer back to the same thing over and over again, so to save keystrokes you can simply assign something to a variable and then type the variable in its place.

Example:

Code:
<html>
<body>
<?php
$variable=”Hello World…again!”;
echo $variable;
?>
</body>
</html>

Constants

I’ll not delve into these much here, but constants perform the same function as variables except they are usually written in all capital letters and cannot be changed once they have been defined.

Save this as variablehelloworld.php (or something) and browse to http://localhost/variablehelloworld.php and you should again see “Hello World…again!” at the top of the page just as before.

Strings

The word “string” is another word for “text”. Anything contained within quotes “ “ is considered by the parser as regular text as opposed to an integer (or…anything else). Let’s modify our recent example to demonstrate:

Code:
<html>
<body>
<?php
$variable=”Hello World…again!”;
echo $variable;
echo “$variable”;
?>
</body>
</html>

You would see in your browser the following: Hello world…again!$variable Let me explain. Although in the php code echo $variable and echo “$variable” were on separate lines, the parser treats everything as one single line (as I said before). This explains why there are no spaces or line breaks in what you see displayed in the browser. Also, “$variable” is a string and $variable is a variable referencing another string. The difference may only be quotation marks to the human eye, but it makes all the difference to the parser.

Concatenation

Bonus points if you were able to pronounce the word on the first try. Concatenation is a big word for “join together”. The concatenation operator in PHP is a “.” (a period). When you need to join two separate things together, you would use a “.”. For an example, we will again edit our previous example.

Code:
<html>
<body>
<?php
$variable=”Hello World”;
$variable2=”…again!”;
echo $variable . $variable2
?>
</body>
</html>

When displayed in the browser, you will see: Hello World…again!

Operators

These are generally used to perform mathematical functions. The basic operators are the following:
• + : addition
• – : subtraction
• * : multiplication
• / : division
• % : modulus (division remainder)
• ++ : increment (adds one)
• — : decrement (subtracts one)
• = : same as
• == : is equal to
• === : is exactly equal to (including type, ex: string, Boolean, integer, etc)
• != : is not equal to
• > : is greater than
• < : is less than
• >= : is greater than or equal to
• <= : is less than or equal to
• && : and
• || : or
• ! : not

I should give an example…but you might cry from boredom.

Conditional Statements

if : use this statement to execute code only if the specified condition is true.
if / else : use this when you want to allow an alternative in case the condition is not true.
If /elseif /else : use this when you have more than one alternative. These commands are case sensitive. The syntax is as follows:

if (condition)
code to execute if condition is true;
elseif (condition)
code to be executed if second condition is true;
else
code to be executed if condition is false;

Example:

Code:
<html>
<body>

<?php
$variable = 1;
if ($variable===”1”)
echo “The variable is a string!”;
elseif ($variable===1)
echo “This variable is a number!”;
else
echo “This variable is dysfunctional!”;
?>

</body>
</html>

Note that the “===” is actually three “=”. Save that as a .php and navigate to the page and see what happens.

Arrays

Arrays store and arrange multiple variables. For example, you can name an array $planets and have entry $planets[0] be “Mercury”, $planets[1] be “Venus”, etc. You again use the $ prior to the name as you would for variables. Arrays always number beginning with “0”, which is important to remember because if you have five arrayed values, the last one of the five would be [4]. Example:

Code:
<html>
<body>
<?php
$planets=array(“Mercury”, “Venus”, “Earth”, “Mars”);
Echo $planets[0] . “ and “ . $planets[1] . “are the two nearest planets to the sun!”;
?>
</body>
</html>

Wrap up

The above is intended simply to be an introduction to PHP. Unfortunately the basics are boring and useless for the most part. Tune in to part two for some more useful stuff you can do using PHP! Part two will cover loops, and subsequent tutorials will cover various methods of passing information from one web page to another, HTML forms, and simple (non-secure) authentication.

ORIGINALLY POSTED BY KEEZEL FOR THETAZZONE/TAZFORUM HERE

Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to use, retain, and publish submitted work within it’s Network

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertise

If you'd like to advertise on The Mutt ( aka TheTAZZone.com ) feel free to contact us at: administration[at]thetazzone.com

TheTAZZone is a non-commercial entity. We do not sell any products or services ourselves. Our revenue comes from advertising and donations only.

We appreciate your support! Your advertising revenue ( or donations ) helps us to continue to upgrade, improve, and offset the costs of maintaining this site.

Donations can be made through the page ' Donate '.