NameSilo

Tutorial: Getting started with PHP (The Basics)

Spaceship Spaceship
Watch
Impact
6
Code:
        //
       //  
      //Tutorial by deadserious - © [url]http://www.webdesigntalk.net[/url]
     //© 2003 [url]http://www.webdesigntalk.net[/url] 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //[email protected] 
  //
 //
//
This is a simple introduction tutorial to help you get started as well as give you some ideas on what you can do with PHP. PHP is a server side html embedded scripting language that can be used to create dynmaic web pages and many other things. Some benefits of using PHP are it's free, it's fast, it's fairly easy to learn, and you can embed it into your regular html pages.

To use PHP you'll need to have access to a web server with PHP installed on it. Most Web Hosts offer PHP support. You can also install PHP and run it on your own system which can make it easier to learn and test PHP scripts. CLICK HERE for a tutorial on setting up PHP to run on your own computer.

Okay we'll begin with a simple PHP script for an example.
1.
PHP:
<html>
<head>
<title>PHP TEST</title>
</head>
<body>
<?php 
echo 'PHP is easy!'; 
?>
</body>
</html>
Copy that code into your favirote text editor, note pad will work just fine. Save it as test.php. Make sure it ends with a .php extension. Upload it to where you store your web accessible files and call it up in your browser. If you see "PHP is easy!" then you know PHP is installed and working on your server. Notice the <?php and the ?> these are the start and end tags, they say hey parse up some PHP and hey stop parsing now. You can just use <? for the start tag if the short tag is enabled on your server. Also notice the semicolon at the end of the echo statement. This tells the parser where one line of code ends and the next one begins. You could do this just as easily with regular html, but we needed an example to show you how it works right? :D
If you look at the source code for that page it will look like this:
PHP:
<html>
<head>
<title>PHP TEST</title>
</head>
<body>
PHP is easy!
</body>
</html>
You can't view the sever side scripts source code from your browser. It will be removed by the time it gets to your browser for viewing. The only code you'll be able to see is the regular html that was included in the script and/or generated by it. :D

PHP Comments
Comments are used for helping the user and/or the developer remember and understand what the program does and/or provide instructions. You can use single or multiline comments. Comments will be stripped out of the program and ignored by the interpreter.
Example:
PHP:
<?php
// This is a single line comment.
$var=1;
/* This is a multiline 
comment. */
echo $var; // Another single line comment.
?>
PHP Variables.
You can define a variable to store for later use. Variables start with the $ sign and are assigned with the = operator . To use a variable you need to assign a value to it.

$snowboarding = 'fun';

The name of the variable is on the left of the = sign and the value is on the right. In this case $snowboarding has the value of fun. Variables can contain numbers, letters, and underscores, but may not begin with a number.

Some examples of using variables.
PHP:
<html>
<body>
<?php
$snowboarding = 'fun and sometimes painful';
?>
<font color="green">Snowboarding is really <?php echo "$snowboarding"; ?></font>
</body>
</html>
The output of this code would be:
Snowboarding is really fun and sometimes painful

echo prints the output to the browser. You can also use print, but there is slight difference between the two. Also notice how you can jump in and out of PHP mode when ever you wish, and how the variable contains it's value through out the script, but only when you are in PHP mode. Okay so now you get the idea of mixing PHP in with HTML.

More examples of using variables.
PHP:
<?php
$snowboarding = 'Snowboarding is really fun and sometimes painful';
?>
<font color="green"><?php echo $snowboarding; ?></font>
The out put of this code would be the same as above:
Snowboarding is really fun and sometimes painful

If you are just echoing a variable you don't need to surround it with quotes.

The difference betweeen single and double quotes

If you want to print out the value of a variable within a string you need to surround the string with double quotes otherwise the actual variable name will be printed out.

<?php
$name = 'Web';
$lastname = 'Design';
echo "My first name is $name and my last name is $lastname";
?>
The output of this would be:
My first name is Web and my last name is Design

If we do the same with single quotes:
<?php
$name = 'Web';
$lastname = 'Design';
echo 'My first name is $name and my last name is $lastname';
?>
The output would be:
My first name is $name and my last name is $lastname

The same thing is true when assigning variables.
<?php
$name = 'Web';
$lastname = 'Design';
$fullname = "My first name is $name and my lastname is $lastname";
echo $fullname;
?>
The output would be:
My first name is Web and my last name is Design

If we assign the vaules to $fullname with single quotes:
<?php
$name = 'Web';
$lastname = 'Design';
$fullname = 'My first name is $name and my lastname is $lastname';
echo $fullname;
?>
The output would be:
My first name is $name and my last name is $lastname

In summary variables aren't replaced with their value unless they are surrounded by double quotes with a few exceptions.

Using Basic Operatros

Comparison operators compare two values.
== is equal to
!= is not equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to

Arithmetic Operators
Just like on a calculator.
+ Addition
- Subtraction
* Multiplication
/ Division

Logical Opertators
&& and (The first and the second is true)
|| or (The first or the second is true)

Some basic math.
<?php
$monthly = 10;
$yearly = 20;
$total=$monthly+$yearly;
echo $total;
?>
The output would be 30.

<?php
echo 2+2;
?>
The out put would be 4.

<?php
$monthly = 10;
$yearly = 20;
$monthly+=10; //Same as $monthly=$monthly+10;
$yearly+=10;
$total=$monthly+$yearly;
echo $total;
?>
The out put would be 50.

<?php
$monthly =10;
$monthly++; //Adds one to monthly. The same as $monthly=$monthly+1;
echo $monthly;
?>
The output would be 11.

The same would work with - and --

Note: ++ and -- are Incrementing/Decrementing Operators and += is an Assignment Operator just like the = sign.

Easy enough right?

Yes it's that easy. :D For the basics anyways.


String Operators.
. concatenate (put two strings together) Returns the concatenation of its right and left arguments.
.= (concatenate and assign) Appends the argument on the right side to the argument on the left side.

<?php
$name = "Web";
$fullname = $name . "Design";
echo $fullname;
?>
Now $fullname contains WebDesign. The . is used to put two strings together.
The output of this would be WebDesign

If we wanted to put a space in there we could do it like this:
<?php
$name ="Web";
$fullname = $name ." " ."Design";
echo $fullname;
?>
Now the output would be Web Design. To make it even easier we could write the variable like $name = "Web "; so that the space is already there. :D
<?php
$name = "Web ";
$name .= "Design";
echo $name;
?>
Now $name would contain Web Design. So the output would be Web Design.

PHP Control Structures
Using if, elseif, and else, to check for certain conditions.

Examples:
<?php
$howmuch = 10;
if ($howmuch == 10) {
print 'how much is 10';
}
?>
This checks to see if the variable $howmuch is equal to 10. If it is it goes on and executes the code between the curly braces. In this case the result would be how much is 10.

Sometimes you may want to make the script do something if the if condition is not true.
<?php
$howmuch = 5;
if ($howmuch >= 10) {
print 'how much is 10 or greater';
} else {
print 'how much is not enough';
}
?>
This checks to see if the variable $howmuch is greater than or equal to 10 and if it is then it prints how much is 10 or greater. If $howmuch is not greater than or equal to 10 then it bypasses the code in in the first set of curly braces and executes the code within the else statements curly braces. In this case the result would be how much is not enough.


You can also check for mutltiple conditions with elseif.
<?php
$howmuch = 10;
if ($howmuch <= 7) {
print 'how much is less than or equal to 7';
} elseif ($howmuch == 6) {
print 'how much is 6';
} else {
print "how much is actually $howmuch";
}
?>
Scince $howmuch is not less then or equal to 7 and it doesn't equal 6 the result would be how much is actually 10.

Now you get it right? :D You can add as many elseif statements as you need. You use the else statement for when all else fails.

Using Switch

First an example of an if elseif statement.
<?php
if ($day == 'Monday') {
echo 'Monday';
} elseif ($day == 'Thursday') {
echo 'Thursday';
} else {
echo 'Not today';
}
?>

Now we can get the same results using switch.
<?php
switch ($day)
{
case "Monday":
echo 'Monday';
break;
case "Thursday":
echo 'Thursday';
break;
default:
echo 'Not today';
break;
}
?>
You can use switch as an alternative for if elseif statements. It's good to use switch when you're checking for alot of conditions rather than having huge elseif statements.
 
Last edited:
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
nice man good for beginners :)
 
0
•••
Very informative...Thanks for sharing!
 
0
•••
Nice informative guide. Making use of it right now!
 
0
•••
0
•••
makes php actually look appealing. shall bookmark this tutorial and try learning another day. my eyes are kind of killing me since I've been using the com for 10 hours already.
 
0
•••
Thanks!

I had only a vague idea of what PHP is and have been putting off learning it for many months.
Learning the basics of PHP through your tutorial was painless. Thanks for taking the time!

-traderone
 
0
•••
Thank you for this. I am starting to learn PHP and right now I had used this reference to look up "!= is not equal to". Thanks.
 
0
•••
0
•••
thanks alot! it cleared up some things that I didnt understand when i tried learning it myself!
 
0
•••
Thanks, very nice tutorial. :)
 
0
•••
thanx man, this works good for me cause im tryin to learn PHP :)
 
0
•••
Nice tutorial i will link to it from a few of my sites!
 
0
•••
nice tutorial, was very useful for me .....thanks again
 
0
•••
Well! I've just registered, and it seems that everybody in here likes sharing! Ok, I've always wanted to learn PHP coding, and upto now I just could modify existing PHP files. This, obviously, was a great job and a hard-to-do thing. But now I've read the tutorial, I begin to understand many useful things which are usually seen on PHP scripts.

Thanks for sharing, and I look forward to seeing more things like this!
 
0
•••
thanks for sharing hope theres more to come
 
0
•••
very nice tut, and really helpful.... thx for the great share my man! :)
 
0
•••
Can the INCLUDE statement be placed within a logical branching statement? In particular, can I write PHP code that uses a SWITCH statement to INCLUDE different external files in CASE the month is January, February, March, etc.?

Is this an appropriate way to implement an "article of the month" where the text of the articles can be set up and maintained outside of the pages that present them?

Thanks.

---J.
 
0
•••
it would be great if u could put all the php codes in PHP tag :)
 
0
•••
thanks for sharing this info
 
0
•••
0
•••
0
•••
Deadserious=my 15 min idol :) php is so easy so excited so excited aaaaaaaahhhhh can hold back my burning passion to learn php ><
 
0
•••
Excellent tut. Ive recently come back to PHP and its an excellent way for me to start over!
 
0
•••
Thanks very much... Looking at this now :D Love it!!!
 
0
•••
absolutly great!!! nice tutorial, im gonna come back to see more tutorials :)
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back