Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,722 members, 7,809,747 topics. Date: Friday, 26 April 2024 at 02:12 PM

My Php Tutorials For Newbies - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / My Php Tutorials For Newbies (1018 Views)

How Do I Upgrade My Php Version From 5.2.3 To 5.2.4 Or Higher / Check Out My Php Code / How To Market My PHP And MySQL Skills (2) (3) (4)

(1) (Reply)

My Php Tutorials For Newbies by qcode(m): 4:53pm On Apr 07, 2008
Hi Everyone, this is going to be a series of tutorials that will teach how to program in PHP from stratch. So I will continue to add a new chapter to it so keep this page in your bookmarks.

In this first chapter I will explain you what PHP is, and how to start with it. At the end of this tutorial you will know how to:

- Create PHP files
- Load PHP files
- Starting PHP
- Create variables
- Use the IF function


Actually this is quite much for a first chapter, but I know that learning a language isn't fun if you can't do nice things fairly quickly. So after some basics will use the IF function for some fun stuff.


This How to create PHP files

Creating a PHP file isn't that hard; just create a text file with .php as extension. What you do with PHP is actually creating a HTML file, but you just post some parts of PHP between the HTML.

What's more interesting to know, is how to edit a PHP file. Well, you can use many editors. I even started coding in plain notepad! But there are editors who are nicer to work with, the best free editor is notepad++, you can get at http://notepad-plus.sourceforge.net/uk/site.htm

With Notepad++ you will get syntax highlighting, this basically means the editors will use colors to highlight the PHP code. So a function will get a different color than some plain text, this makes it a lot more easy to read.


How to you load a PHP file?

You need a server with PHP support for this. Most paid host support it, but you won't find PHP on your free website. Wink

There are two things you can do.

One is installing Apache plus PHP on your home computer, but that can be quite a hassle.

Two is just get a webhost with PHP support, there are plenty of them online. So there is no need to install it on your own computer. I recommend you to get your own domain-name, so you can test all you want.

If you have a host with PHP support you just need to upload the PHP file the server.

Now Starting PHP

To start telling the server you are going to use PHP isn't that hard. You use the following tags to indicate PHP:

Code:
1
<?php
2

3
// And here some PHP code
4

5
?>


The stuff with the "//" is comment, so no PHP code.

As you can see you only need to write a beginning PHP tag, and a ending tag. You can just put these in your HTML like this:

Code:
1
<html>
2
<head>
3
<title>Nairaland Free PHP tutorial!</title>
4
</head>
5
<body>
6

7
<?php
8

9
echo "Hello world!";
10

11
?>
12

13
</body>
14
</html>


This makes it quite easy to use PHP! Smiley

And check out that part about "echo". This is a function, this tells PHP to put the text between the quotes into the HTML.

So the result of that code would be:

Code:
1
<html>
2
<head>
3
<title>Nairaland Free PHP tutorial!</title>
4
</head>
5
<body>
6

7
Hello world!
8

9
</body>
10
</html>


This is what your browser would receive from the server. Quite easy to understand right?

Create variables

Now lets move on to real PHP programming.

PHP uses much temporary information, lets say some test you've written in a form is also some temporary information PHP must be able to save. To save this information you need to create a variable. A variable basically is a piece of the computers RAM memory.

Let's see how you create a variable shall we?

Code:
1
<?php
2

3
$person = "Lagosian";
4

5
?>


First you should notice the dollar sign ($). This is used to let PHP know you are using a variable. Next is the name of the variable, so we've created a variable called "$person". After the name you write the content of the variable, in this example the variable "$person" contains the text "Lagosian";

The difference between text and numbers.

In PHP (and all programming languages) there is quite a difference between numbers and text. First of all in PHP you call a number an "Integer", and you call some text a "string".

Of course this is not the only difference, the main difference is how you use them in PHP. Like you've seen in the example above, to store some text into a variable you need to put quotes around them ("wink. This is where the difference kicks in, when working with numbers you do not add quotes around them.

So let's say if I want to store my length in a variable I will wont use quotes: (in centimeters of course)

Code:
1
<?php
2

3
$length = 172;
4

5
?>


Now what if I want to combine a number with text? Well that's simple, it turns into text. You only omit the quotes when it's a pure number without some text.

Code:
1
<?php
2

3
$length = 172 cm; // Wrong
4

5
$length = "172 cm"; // Right
6

7
?>


Now let's echo the value of the variable into the HTML code.

Code:
1
<?php
2

3
$length = "172 cm";
4

5
echo "My length is: " . $length;
6

7
?>


This looks weird doesn't it?

What I did here is placing the $length variable in the echo. You can do this by closing the text with a quote ("wink and placing a point followed by the variable.

This is not so hard right? Let's move on to some real programming.

The if function

The most used function within every programming language is the If function, it's a function that compares values.

It looks overwhelming, but believe me it's very easy.

Code:
1
<?php
2

3
$person = "Lagosian";
4

5
if($person == "Lagosian"wink {
6

7
echo "The person is a Lagosian!";
8

9
}
10

11
?>


A lot of new stuff, but don't worry you will understand it soon Smiley

The first part is if($person == "Lagosian"wink. This part compares "$person" with the plain text "Lagosian". So basically it's checks if the value of $person is "Lagosian".

Our variable $person contains the text "Lagosian", so this check is true. Whit this function, if it's true the code between the accolades ({ , }) is executed.

So the output of this code is:

The person is a Lagosian!

But what if the variable does not contain "Lagosian"?

Code:
1
<?php
2

3
$person = "Ijebu";
4

5
if($person== "Lagosian"wink {
6

7
echo "The person is a Lagosian!";
8

9
}
10

11
?>


Now the if function isn't true anymore. This means that the code between the accolades ({…}) won't be executed.

This piece of code won't return any output, that's kind of ugly. I want to know it when the $person does not contain "Lagosian".

To realize this we'll need to add some extra code.

Code:
1
<?php
2

3
$person = "Ijebu";
4

5
if($person == "Lagosian"wink {
6

7
echo "The person is a Lagosian!";
8

9
} else {
10

11
echo "The person is not a Lagosian!";
12

13
}
14

15
?>


I've added else to the code. The code between the accolades of else is executed when the if is not true.

So in this example the part between else's accolades are executed. Returning the following output:

The person is not a Lagosian!

This example should be pretty clear, but I highly recommend you to write your own pieces of code to test all of your new knowledge.

The end

This is end our of part one. In a short time I will write the second chapter of this series. In the next series we'll talk about form input. So all the input that's send via a simple HTML form.
Re: My Php Tutorials For Newbies by adewaleafolabi(m): 8:20pm On Apr 07, 2008
Thanks i found this easy to understand, its similar to other languages only for some few differences. Although am reading from a mobile phone, i've learnt some meat tricks like the . issue
Re: My Php Tutorials For Newbies by my2cents(m): 2:18pm On Apr 09, 2008
gcode,

In the future, is it possible to indent (and remove the line numbers) for easier readability? If the tab key doesn't work, you can always use either the "code" tags here or just press the space bar 4 times for each indent. Besides, it will also help compact your post wink

(1) (Reply)

TRY OUT MY "HOME-MADE" SOCIAL BOOKMARKING WEB BROWSER! / Onigbongbo Local Government Website Hacked / Firefox 13 - Now Web Developers Can Be Really Happy

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.