NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Webmaster Tutorials
Reload this Page Tutorial: Getting started with PHP (The Basics)

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-12-2003, 12:12 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Tutorial: Getting started with PHP (The Basics)


Code:
        //
       //  
      //Tutorial by deadserious - © http://www.webdesigntalk.net
     //© 2003 http://www.webdesigntalk.net 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //webmaster@webdesigntalk.net 
  //
 //
//
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 Code:
<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?
If you look at the source code for that page it will look like this:
PHP Code:
<html>
????: NamePros.com http://www.namepros.com/webmaster-tutorials/14637-tutorial-getting-started-with-php-basics.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.

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 Code:
<?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 Code:
<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 Code:
<?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. 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.
<?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) {
????: NamePros.com http://www.namepros.com/showthread.php?t=14637
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? 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 by deadserious; 06-19-2005 at 11:34 PM.
deadserious is offline  
Old 03-12-2003, 12:19 AM THREAD STARTER               #2 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Tutorial: Getting Started with PHP (The Basics) Continued


Code:
        //
       //  
      //Tutorial by deadserious - © http://www.webdesigntalk.net
     //© 2003 http://www.webdesigntalk.net 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //webmaster@webdesigntalk.net 
  //
 //
//
Including Files
You can also add PHP to your HTML pages by including files.
For example a file called header.inc contatins:
<html>
<head>
????: NamePros.com http://www.namepros.com/showthread.php?t=14637
<title>Title of your page</title>
</head>
<body>
<h7>Header Text</h7>

and your main page named index.php contains:
All of your usual html content here.

And a file called footer.inc contains:
Coypright yoursite, All Rights Reserved. and what ever else you want in your footer here.

Okay so now we've got three files: header.inc, index.php, and footer.inc

Putting it all together.
In your index.php you include the header.inc and footer.inc files like this:

File index.php
<?php
include("header.inc");
?>
All of your main html content here.
<?php
include("footer.inc");
?>
</body>
</html>

Now with the examples above the source code would like this:
<html>
<head>
<title>Title of your page</title>
</head>
<body>
<h7>Header Text</h7>
All of your usual html content here.
Coypright yoursite, All Rights Reserved. and what ever else you want in your footer here.
</body>
</html>

You get it now right?

More info on including files in an upcoming tutorial. Stay tuned!

Alright that's all for now. I'll add on to this tutorial in the coming days, weeks, and months. Some things to come will be Getting data from forms, using loops, and a bit more. Feel free to point out any mistakes I've made or ask any questions.
Last edited by deadserious; 06-19-2005 at 11:34 PM.
deadserious is offline  
Old 03-13-2003, 03:38 PM   #3 (permalink)
New Member
Join Date: Mar 2003
Posts: 6
Keith[imported] is an unknown quantity at this point
 



Re: Tutorial: Getting started with PHP (The Basics)


Quote:
Originally posted by deadserious
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';
}
?>
I'd suggest changing the if elseif statement around slightly.

Instead of using
PHP Code:
if ($day == 'Monday') { 
????: NamePros.com http://www.namepros.com/showthread.php?t=14637
you should use
PHP Code:
if ('Monday' == $day) { 
This way, if you accidentally use one = sign instead of two, you won't replace the value of the variable and PHP will throw up an error message.
__________________
Web Developers - Build your knowledge, then your website!
[ Help Forums : Information : Links Directory ]
Keith[imported] is offline  
Old 03-13-2003, 11:49 PM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Hey nice suggestion! That's a good way to check if you've used one = sign where you should have used two.

If you were only to use one though, you would usually find something wrong becasue you wouldn't get the results you expect in the testing process of your script, but the way you suggested would probably help you solve the problem alot faster.
????: NamePros.com http://www.namepros.com/showthread.php?t=14637

I wonder though is there a difference between the two?

$day == 'Monday' and 'Monday' == $day

Could that cause problems in certain situations or would using either method produce the same results in any situation?

Anyways nice tip!
Last edited by deadserious; 06-19-2005 at 11:35 PM.
deadserious is offline  
Old 03-14-2003, 01:50 PM   #5 (permalink)
New Member
Join Date: Mar 2003
Posts: 6
Keith[imported] is an unknown quantity at this point
 



It should work fine under any circumstance. If you use that method and you do accidentally use the one = sign, you will get a parse error for that line, which makes it soo much easier to debug.
__________________
Web Developers - Build your knowledge, then your website!
[ Help Forums : Information : Links Directory ]
Keith[imported] is offline  
Old 03-14-2003, 02:59 PM   #6 (permalink)
NamePros Regular
Join Date: Sep 2002
Location: Canada
Posts: 481
DarkDevil is an unknown quantity at this point
 



in vb the order of which the variables are listed are very important.. but i think in php it doesn't matter so much. But i like having the variable first, dunno why
__________________
Sometimes I lay awake at night and I ask "Where have I gone wrong?" Then a little voice says "This is going to take more than one night"
DarkDevil is offline  
Old 03-15-2003, 07:35 AM THREAD STARTER               #7 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



I'm used to using the variable first and I've never had a problem with entering one = sign when I should have two. I can see how you could though, especially when your just beginning. It kind of seems backwards to me if you do it the opposite, but I learned Perl before PHP so the two == signs was already a habbit for me for the most part. Although in Perl you usually compare strings with eq instead of ==
deadserious is offline  
Old 02-24-2004, 10:17 AM   #8 (permalink)
NamePros Member
Join Date: Nov 2003
Posts: 168
happyyo is an unknown quantity at this point
 



Thanks guys !!! I find your tutorials really useful.

Laura
__________________
5WQ.com for SALE.
happyyo is offline  
Old 02-24-2004, 08:30 PM THREAD STARTER               #9 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Quote:
Originally posted by happyyo
Thanks guys !!! I find your tutorials really useful.

Laura
You're Welcome! Thanks for your comments.
Last edited by deadserious; 06-19-2005 at 11:35 PM.
deadserious is offline  
Old 02-24-2004, 08:36 PM   #10 (permalink)
Senior Member
 
Sohil's Avatar
Join Date: May 2003
Location: Ohio
Posts: 2,337
Sohil is a name known to allSohil is a name known to allSohil is a name known to allSohil is a name known to allSohil is a name known to allSohil is a name known to allSohil is a name known to allSohil is a name known to all
 



Very nice tutorial deadserious!
__________________
•• PolurNET Communications LLC
=Avoid the Freeze...Enjoy the Breeze!=
Sohil is offline  
Old 03-15-2004, 10:15 PM   #11 (permalink)
NamePros Member
 
Cjpixel's Avatar
Join Date: Mar 2004
Location: Cebu City
Posts: 35
Cjpixel is an unknown quantity at this point
 



uuuh, this is my first time to encounter PHP, I am just trying to learn this just now, so I just want to ask more questions.

Now, you stated that there are comments to help the user remember, will that always appear when you're using notepad?
Moreover, is it always before the body tag?

I hope you can give me an insight about this Deadserious. Thanks.
__________________
How would my life be without you?
Where would I be without you.....
Cjpixel is offline  
Old 04-05-2004, 03:09 PM   #12 (permalink)
Melinda
Join Date: Mar 2004
Posts: 1,933
emeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud ofemeri has much to be proud of
 

Member of the Month
July 2004
Breast Cancer
I like your tutorial
emeri is offline  
Old 04-17-2004, 02:46 AM   #13 (permalink)
Senior Member
 
matrics's Avatar
Join Date: Apr 2004
Posts: 1,672
matrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of light
 



nice tutorial, was very useful for me
__________________
█ █ █ My T-shirt Addiction!!!
Hero's Gold
matrics is offline  
Old 03-06-2005, 10:54 AM   #14 (permalink)
First Time Poster!
Join Date: Mar 2005
Posts: 1
chaos_theory is an unknown quantity at this point
 



thanks for sharing
__________________
Urban Art
chaos_theory is offline  
Old 03-06-2005, 12:04 PM   #15 (permalink)
This mute print lies
 
Petter's Avatar
Join Date: Jan 2005
Location: Bergen, Norway
Posts: 4,761
Petter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud ofPetter has much to be proud of
 



Very nice tut! I've wanted to learn PHP for long and now i have desided i will!

Thanks for the share!

Petter
Petter is offline  
Old 03-31-2005, 08:33 PM   #16 (permalink)
Senior Member
 
snoopi's Avatar
Join Date: May 2004
Location: E.U.
Posts: 1,038
snoopi is just really nicesnoopi is just really nicesnoopi is just really nicesnoopi is just really nice
 



Looking forward the more advanced tutorial Nice job
__________________
WebProxy.cc - Browse what & when you want!

:kickass: Brand New Media Site Script MyMediaScript.com:kickass:
snoopi is offline  
Old 03-31-2005, 11:22 PM   #17 (permalink)
Senior Member
 
majinbuu1023's Avatar
Join Date: Jan 2005
Location: New Zealand
Posts: 3,747
majinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to beholdmajinbuu1023 is a splendid one to behold
 



thanks...are there anymore tutorials coming..
btw WOOOOT I did something in php http://kwacker.com/php/test.php
majinbuu1023 is offline  
Old 04-02-2005, 05:12 AM   #18 (permalink)
Senior Member
 
ammo's Avatar
Join Date: Dec 2004
Location: Cornwall!
Posts: 2,251
ammo is a name known to allammo is a name known to allammo is a name known to allammo is a name known to allammo is a name known to allammo is a name known to allammo is a name known to allammo is a name known to all
 



very informative, keep them coming!
ammo is offline  
Old 05-25-2005, 07:06 PM   #19 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
Nice tutorial
Thanks for shairing...lookin forword for more tutorials from u
unknowngiver is offline  
Old 06-27-2005, 10:42 AM   #20 (permalink)
NamePros Regular
 
Element's Avatar
Join Date: Jun 2005
Location: United States
Posts: 647
Element has a spectacular aura aboutElement has a spectacular aura about
 



Thanks for the great tutorial Deadserious! I found this very useful when I first started php which wasnt long ago.
Element is offline  
Old 07-01-2005, 02:27 AM   #21 (permalink)
New Member
Join Date: Jun 2005
Location: ID
Posts: 3
myengines is an unknown quantity at this point
 



great articles, its usefull for me!
myengines is offline  
Old 07-04-2005, 11:39 PM   #22 (permalink)
NamePros Member
Join Date: Mar 2004
Location: Vancouver
Posts: 161
krazyvan05 is on a distinguished road
 



That is a great tutorial. I have a question tho. I have learned java, it seems very similar to PHP. What is the difference between these two codings.
__________________
www.webhostforum.org
www.searchengineforum.org
www.webdesignerforum.org
krazyvan05 is offline  
Old 07-05-2005, 12:33 AM THREAD STARTER               #23 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



PHP is more of procedural and/or somewhat Object Oriented language and Java is an Object Oriented language. PHP is also an interpreted language and Java is a complied language, although in some cases it could be considered interpreted such as when using servlets. They are a lot a like in a lot of ways though, since PHP takes so much from Java.
deadserious is offline  
Old 07-09-2005, 09:18 AM   #24 (permalink)
NamePros Regular
Join Date: Jul 2005
Posts: 247
coolpix is an unknown quantity at this point
 




Splendid tutorial! Thanks for sharing!
Last edited by coolpix; 07-09-2005 at 09:22 AM.
coolpix is offline  
Old 07-09-2005, 11:17 AM   #25 (permalink)
se
Senior Member
 
se's Avatar
Join Date: Dec 2004
Location: E://my computer/namepros/post.exe
Posts: 1,685
se is a name known to allse is a name known to allse is a name known to allse is a name known to allse is a name known to allse is a name known to allse is a name known to allse is a name known to all
 



thanx for sharing
se is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 11:02 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger