In this tutorial you learn basics of the following:if() tutorial part1, Sessions tutorial part2, array()tutorial part3, function() tutorial part4; I will not be going over definitions only the implementation.

All parts of the tutorial will be added to the same topic form, so to not clutter the tutorial form page, and to not be accused of massing tutorials to gain contribution points. Please do not respond to this post for questions regarding problems with your code, those post should go to the correct help form. Also do not put issues you find with this tutorial in a response, rather please contact me via PM. Thanks for your understanding

Through out the tutorial we will be working to a fully working simple game. In this game the User will have to guess a number between 1-100 (This number will be generated randomly). Once the user submits the number we in our program will check to see if it is the right number if not... We will output the correct response and have them guess again.

In this game we will also add a score, which will increase by one every time the user guess's right. At the end of this tutorial we will come out with something that looks like this: Guess Game

What you will need for this tutorial:IDE for codeing your PHP. (Free one you can download is Notepad++)
WebHost with PHP ( Free one is www.000webhost.com)
Basic Xhtml knowledge (as I will not be going over the xhtml)

On with the Tutorial! First we will need to learn how to start our php... There are a few different tags that you can use for php, but the problem is they are not cross platform compatible. Or in other words will not work on all hosting servers. So we will be using the opening tags that will work for all...

<?php
//All php code goes here...
?>

Bingo... there we go that is all you need to do to start php coding, but wait it dose not do any thing yet...

Lets have our php out put Hello DIC!, which is simple use of a echo:

<?php
echo "Hello DIC!";
?>

Output:Hello DIC!

Congratulation you first php script! After we right a command that we want php to do we have to use a ";" to tell the php that this is the end of the particular command. If we forget this little detail php will send us a syntax error.

Lets learn how to use variables now. A variable can hold data for later use, so you can kind of think of a variable as a box that holds what ever you want... The thing to learn is how to put these things inside of the box. We will do this now:

<?php
$string="Hello DIC!";
echo $string;
?>

Output:Hello DIC!

As we can see all we did was put the "Hello DIC!" into a variable called $string. There are some rules for variables you should know. The main one I will tell you now is that you HAVE to start all variables with a "$", also a variable name can only start with a Number, Letter, or a "_", they variable names can NOT! have any white spaces. This means "$this variable" is wrong and will create a error. How ever we can use this variable if we take away the space like this "$this_variable". Also very important variables are CASE SENSITIVE! so "$this_Variable" and "$this_variable" are not the same.

Now we know a little about syntax, and variables we will now start to create our game! First we will need to create a quick xhtml form.

This is our basic form, with css. When you create your xhtml file please save it as: "index.php". In the form make sure you method and action is as shown <form action="check_num.php" method="post">

With this form we will be submitting our user input to our php script (check_num.php). So lets continue by creating that file now. We will be using a if (); statement to see if our user input is below or above a pre-defined number. We will also put the user input into a variable.

The first line if(isset($_POST['user'])) is checking to see if the form was submitted, and if they user did in fact submitted, do the following. When using a if() statement you need to put all the code you want done from that if statement between the "{}" tags. As which brings us to our next line of code.

$input=$_POST['user'] This line of code is clear as since we learned all we are doing here is taking the user input that was submitted via-post into a variable. All user input from a form can be submitted by Post or Get you can obtain both in there respected ways: $_POST[''] or $_GET['']. You can also get both by using the request.$_REQUEST['']. The ['user'] part of the Post is taken from our form name="user" of the particular input.

In this if statement we are checking if the user input is greater then 10. The grater then sign is what we use the see if something is this is grater then that, like this:this > that. If indeed the input is grater we going down to our rest of code which is saying echo"You guessed to high, please try again. Then a new php pre-defined function called "include". This function pretty much says also write what every is in "index.php", which also prints out the content of the index.php file. That is it for this group of code... lets continue.

Now the start of this code is quite similar to the last one, but one small deference instead of checking for grater then input we check for lower than input. We do this by using the "<" , and we also since we started with a if we add a else in front of the if. This is saying if the above statement was not true try this. So if our first statement is not true check if the user input was less then 10. If this is true echo You guessed to low,please try again. We also have a include function here as well; we add these includes so the user can try to guess again.

This block of code is saying Else if no other above statement is true do this... Then as we see again we echo a output to the user saying Alright you got it right! it was:Hmm... you see something different here? We added the "$input" value. How do we do this uh? Well we first have to close the "" that surrounds the first line of text that we are outputting. Next is we add a ".", then right a variable that we want to output. Then yet again we add the include function. The output for the whole string here will be Alright you gor it right! it was: 10.

So today we have learned how to wright php, use the echo, if.. else if, else statement, and how to include a file to be shown. Next time I come back we will add a Session which will give us a random number that the user has to guess, and we will also be adding a score when the user guess is right. Till next time practice practice and practice. Good Luck and have fun programing. Good Night DIC!

Replies To: Basic PHP part 01

Welcome to Basic php part 2!From our last tutorial we have a working guessing game which takes user input and test it to see if it is in fact the number we have preset for the user to guess.

Now this dose not sound fun right? Because If you get it right once you now know which number to pick every time. So to fix this we will add two new things into our program. One will be Sessions, and the next will be the rand() function.

First lets learn how to use then rand() function. This can take two different variables (e.g. rand(a, ). So what are these to variables and how do we use them. Well the first variable "a" would mean pick a number from A too the next variable which is "b". So lets put numbers in there

rand(1, 5);

This will pick a random number from 1 through 5. That is easy right. So we can just put our rand number in there change our if statements and we are done right... Wrong!

Each time a person guess's a number the form they use to submit their guess goes to the php program then it sends back the data and also a new page. Now every time this happens; say they get the number wrong on the first try, then it will come back saying wrong number(Or what every you want it to say). So they guess again, but if we just put the rand() it will generate a rand number every time they guess.

So what do we need to do... Well if we can store the number some how that is saved from getting changed every time we load a page if would work, and this is where our friend session comes in. We can use this little guy to store our random number until the user guess's it right, and then generate a new number and store the new number in our session. While getting ride of our old number.

This may seam like a lot to some people but it is pretty simple. Lets go first to our index.php page and start our sessions for when a user visitors our page.I will be showing only snip-its of code, but I will tell you where to put these snip-its so don't freak out when I don't show whole code.

//At the start of our code we will insert our php tags before any other text in the page.
<?php
session_start();
if(!isset($_SESSION['number'])){
$_SESSION['number']=rand(1,100);
}
?>
// All other html goes here...

OK, lets look at this one step at a time. Every time you need to start a session on a page you have to use the "session_start();". This is required to use the sessions. This also needs to be started before any other code or html in the file. If you do not start this before every thing else you will get a header already sent error.

So we started our session so we need to set our rand value into the session we will use "$_SESSION['number']". So to do this we first check if the session[number] is not already set. We do this with our if() statement. In the above code we are saying IF the $_SESSION['number'] is not set create session[number] with the value rand(1, 100), or a random number from 1 through 100. This also stops us from creating session[number] every time we visit the page which would defeat the purpose because it will over ride the first rand number and give us a new one.

********** This Part of the Tutorial is still Under development It will be completed soon. Please check back at a later time.***********