Choose fontsize:
Welcome, Guest. Please login or register.
Did you miss your activation email?

 

Pages: [1]   Go Down
  Print  
Author Topic: Basic PHP Tutorial  (Read 7515 times)
0 Members and 1 Guest are viewing this topic.
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« on: March 19, 2006, 06:28:01 PM »
[h1]Basic PHP Tutorial[/h1]
By: ssjskipp

What?
Okay, so here's the general idea of what I'm going to do in this tutorial:
Teach you the basics of PHP.
Simple as that…nothing more, nothing less…well, maybe a little more…


Okay, let's get started!
If you have a server, then great! If not…well, get one! Ask a friend, look online, anything!  If it has PHP installed on the server, then you're set!
Once you have a server and an FTP account to transfer the files, we can start by making our first PHP program!  To make a program with PHP, there are a few things you need to understand first:
  • Every PHP program MUST start and end with <? and ?>, respectively.  You will see examples of this later.
  • All expressions must end with a semicolon ( ; )
  • A PHP file (.php) with HTML scripts in it WILL be read as HTML, unless inside <? and ?> tags. (examples are explained further down)

So, now that you're confused with all of that, let me clear something up with an example script.  First, open notepad, or anything that can save a .php, in need of a good one?[/color]
Quote
Check out a couple of my favorites:
Araneae
Notepad 2
Code:
<?php //<-- the "//" indicates a one-line comment, and only everything after and on the same line as the "//", will be counted as a comment.
// Please note that the "<?php" part can simply be "<?", the php is not required.
echo "Hello World!"//"echo" is a basic PHP function that displays data in the form of a string.  If you notice, I added the ‘;' at the end, and I put my string (Hello World!), in quotes.  This means that it will echo it as a string.
/*  This is an example of a Multi Line comment.  It will only stop with the break symbol -->*/
?>

Okay, confused? Good.  Go upload that file to the server (save it as test.php).  Now, go you your server, and view the file.  Notice how it displays "Hello World!"? Good, catching on? Nice!

Now, a little bit harder...

Code:
<?php
$my_string 
"This is a variable set as a string.";
$my_number 0;
// Here, I set two variables.  In PHP, a variable is defined with a $, then a string.  IE: My two variables are called my_string, and my_number, because they each start with a $.

/* Now that the two variables are set, let's do something with them.
First, I'll make it easy and just display what they equal */
echo "$my_string = ".$my_string."<br/>$my_number = ".$my_number;
/* Notice how I used a ‘.' To append separate strings and variables together, this makes the echo function display multiple strings
and variables, with one execution…IE: Instead of doing this:
echo "$my_string = ";
echo $my_string;
echo "<br/>";
echo "$my_number = ";
echo $my_number;
I just put it all in one echo. */
?>

That's the basic overview of variables…they're simple once you get the hang of them.  Now, to perform a function, such as add(+), multiply(*), divide(/), and subtract(-), you simply do this:
Code:
<?php
//Set Initial Number
$num 10;
//Make a new variable, and perform +
$add $num+5;
//Make a new variable, and perform -
$sub $num-5;
//Make a new variable, and perform *
$multiply $num*5;
//Make a new variable, and perform /
$divide $num/5;
//Echo all my variables
echo "$num =".$num;
echo 
"<br/>$sub =".$sub;
echo 
"<br/>$multiply =".$multiply;
echo 
"<br/>$divide =".$divide;
?>


If you're noticing, it's a LOT like Flash (if you know flash).  It's just a bit more work (because it's all through text, no drawing and such).  You set your variables (with the $, to define a variable), add a ; at the end of a function, and display data.  Simple.
Now, how about some if() statements?  Let's make it easy:

Code:
<?php
//set number variable
$num 5;
//run if() statement
if ($num == 5){
//if the if() statement is true, echo string
echo "$num is = to 5.";
} else {
//else, echo another string
echo "$num does not = 5.";
}
?>
Now, that example is pretty pointless, because I set $num to 5 to start, so it will echo "$num is = to 5.", however, if you change $num to anything else, it will echo "$num does not = 5.".  Now, how about some user input? If you haven't noticed from above, I can put some HTML scripts in the echo function, and it will work (the
, made a line break) IE:

Code:
<?php
echo "This is an example of an HTML line break:<br/>Top<br/>Bottom";
?>

So, we know we can run HTML scripts, let's make HTML interact with PHP…yes, that's it! Here we go:   
Code:
<?php
$name 
$_POST['name'];
if (!
$name){
echo "Please enter a name: ";
?>

<html>
<form name="test_form" method="post" action="">
<input name="name" type="text" id="name">
<input type="submit" name="Submit" value="Submit">
</form>
</html>
<?php
} else {
echo "Hello ".$name.", welcome!";
}
?>
So, there's a VERY basic overview of PHP.  As you can see above, I used "$_POST[‘post_var'];" to retrieve the variable from the HTML form.  That's my quick PHP tutorial, and I apologize for being confusing…I did my best, and I hope you understand it.  Any questions about this tutorial, post here, and if you need to know more about PHP functions, visit http://www.php.net

If you like this tutorial, please Click here to register.
« Last Edit: March 20, 2006, 04:11:17 AM by SKETCHi » Logged
Graphic Moderator
*

Karma: 16
Gender: Male
Posts: 2,181

...


View Profile WWW
« Reply #1 on: March 19, 2006, 08:12:17 PM »
good tut. it would of been really helpful to me but i just learnt all that stuff like 2 days ago on w3schools. But when you put the 2 variables together in the one echo like so:
Code:
<?
$my_string = "This is a variable set as a string.";
$my_number = 0;
echo "$my_string = ".$my_string."<br/>$my_number = ".$my_number;
?>
you can just do this:
Code:
<?php
$my_string 
"This is a variable set as a string.";
$my_number 0;
echo 
$my_string .""$my_number ;
?>
this might be wrong but im pretty sure its not
Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #2 on: March 19, 2006, 08:56:32 PM »
good tut. it would of been really helpful to me but i just learnt all that stuff like 2 days ago on w3schools. But when you put the 2 variables together in the one echo like so:
Code:
<?
$my_string = "This is a variable set as a string.";
$my_number = 0;
echo "$my_string = ".$my_string."<br/>$my_number = ".$my_number;
?>
you can just do this:
Code:
<?php
$my_string 
"This is a variable set as a string.";
$my_number 0;
echo 
$my_string .""$my_number ;
?>
this might be wrong but im pretty sure its not


Yeah, but if you look, I'm also echoing a string that says which variable is which, I am also doing a line break in there
Code:
(<br/>)
Whoa, type that w/o code tags, and it works XD


cool
« Last Edit: March 19, 2006, 10:01:08 PM by ssjskipp » Logged
Graphic Moderator
*

Karma: 16
Gender: Male
Posts: 2,181

...


View Profile WWW
« Reply #3 on: March 19, 2006, 09:24:45 PM »
o okay im a noob at php and didnt notice that Tongue
Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #4 on: March 20, 2006, 03:28:40 PM »
o okay im a noob at php and didnt notice that Tongue

lol, so'kay, XD! Just as long as you understand what I did, and BTW, you're right, you can just do that, but what you did isn't what I wanted to do =o
Logged
Member
*

Karma: 3
Gender: Male
Posts: 151


View Profile WWW
« Reply #5 on: March 23, 2006, 10:36:41 PM »
Nice tutorial dude, I was planning on doing the same thing, but I guess you beat me to the punch. Great job!
Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #6 on: March 24, 2006, 05:17:43 PM »
Nice tutorial dude, I was planning on doing the same thing, but I guess you beat me to the punch. Great job!

heh...thanks =]
Logged
Newbie
*

Karma: 0
Posts: 14

1+1 = 11


View Profile WWW
« Reply #7 on: September 15, 2006, 05:52:59 AM »
Nice tutorial. ^_^
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:      

Flawsome [ In: 446 // Out: 1726 ] MickM [ In: 2403 // Out: 2118 ] BioRUST [ In: 2951 // Out: 2428 ] SMF Topsites [ In: 0 // Out: 2021 ] Graphic Addicts Topsites [ In: 2435 // Out: 3389 ] Globex Designs [ In: 116 // Out: 1458 ] SaberFusion [ In: 553 // Out: 1622 ] Cosmo Designs [ In: 1151 // Out: 1621 ]
Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC
Phobos design by Bloc | XHTML | CSS


Google visited last this page Today at 12:05:22 PM