Pages: [1]   Go Down
Send this topic | Print
Author Topic: [Guide] Beginners PHP Tutorials  (Read 2186 times)
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« on: August 05, 2007, 05:50:22 AM »

What you need to get started with PHP

Before you can write and test your PHP scripts, there's one thing you'll need - a server! Fortunately, you don't need to go out and buy one. In fact, you won't be spending any extra money. That's why PHP is so popular! But because PHP is a server-sided scripting language, you either have to get some web space with a hosting company that supports PHP, like 110mb.com, or make your computer pretend that it has a server installed. This is because PHP is not run on your PC - it's executed on the server. The results are then sent back to the client PC (your computer).

Don't worry if this all sounds a little daunting - we've come across an easier way to get you up and running. We're going to be using some software called "EasyPHP". This allows you to test your PHP scripts on your own computer. Over a million people have downloaded this software. It installs everything you need, if you have a Windows PC (Win9x/Me/NT/2000/XP). We'll explain how to get it installed in a moment, and where to get it from. But just a word for non-windows users.

Apple Users

If you have OS 10, then try this guy's site to get up and running with PHP. The link to click on is "PHP Apache Module (NEW version 5)"
http://www.entropy.ch/software/macosx/

Another useful page is:
http://farm.tucows.com/blog/_archives/2004/7/20/108833.html

You can also have a look at this site:
http://www.phpmac.com/browse.php?type=tut&cat=2

Linux Users

There's quite a few sites out there to help Linux users get up and running with the Apache server and PHP. Here's two sites that are worth checking out:
If you know any better ones, we'd be interested in hearing from you!

Windows Users

OK, back to Easy PHP and Windows. First, you need to download the software. You can get it from here (this site is nothing to do with ours, by the way):
Be sure to click the link for Installation Guide, as well as the link for Downloads. The file you need to download is EasyPHP. Once you have downloaded the file, double click to install.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #1 on: August 05, 2007, 05:50:58 AM »

Installing and Testing Easy PHP

If the installation went well, you should have an new entry on your Start > All Programs menu. It should look something like this:



Click Easy PHP and you'll get this popping up:



If you get a green light for the Apache traffic sign, then your server is up and running! (MySql is a database. We'll be using this in later tutorials. Don't worry about it for now.) When the Apache server is loaded up, you'll see a black letter "e" in your system tray (in the bottom right of your screen, where the clock is). When Apache is running, a red square will be flashing on and off on the letter "e". Right click to the letter "e" to see the following menu:



From here, you can stop the server, exit it, view help files, and see the configuration pages. The Apache option on the configuration menu is an interesting one. From here, you can change where your PHP files are stored. When your scripts are run, they will then come from your location, and not the default one. Look for this line:

DocumentRoot "${path}/www"

To change the location, type a path to a directory of your choosing:

#DocumentRoot "${path}/www"
DocumentRoot "F:\myphp"


In the example above, we've changed the location to a folder on our "F" drive. The hash (#) symbol before the old location means the line will be ignored.

But if you're not that adventurous, then you don't need to change anything!

You do, however, need to test if your PHP pages are displaying OK. To test it out, start up your browser (Internet Explorer, Firefox, Opera, etc). Type the following into the address bar:

http://127.0.0.1/index.php

Hit the enter key, and you should see the  default index page (click to open in a new window 56K).

The address 127.0.0.1, by the way, is the address of your own PC. Whenever you're testing your web pages, type these numbers first. Then type a forward slash, followed by the name of the PHP script you want to run. We'll now create a new folder in the root directory, and create a new PHP page. So, do the following:
  • On your desktop, double click the icon for "My Computer"
  • When you see a list of all your drives, double click the one where you installed EasyPHP to (The F Drive, in our case. Probably "C", for you)



Locate a folder called PHP, and double click it. You should see the following:



Double click the folder called "EasyPHP1-8" to see what's inside of it. You should see a list of files and folders:



The one we're looking for is called "www". This is the root folder mentioned on the index page above. Double click this folder to see the following:



And there, finally, is the index.php file that you ran in your browser! You'll be saving all of your scripts to this directory, or folders in this directory, so it's a good idea to make a note of it. Better yet, click Favorites > Add to favorites. You'll then have a shortcut to this folder on your menu bar.

To create a new folder, do the following (in Windows):
  • Click File from then menu bar
  • From the File menu, click New > Folder
  • From the File menu, click New > Folder
  • Rename the folder to "test" (without the quotation marks)

You now have a new directory (folder) in your root directory (the www one). Copy and paste the following script into a text editor. (Something like Notepad will do, if you're a Windows user. If you've never used Notepad, click Start > All programmes > Accessories, then click Notepad.)

<html>
<head>
<title>PHP Test</title>
</head>
<body>

<?php phpinfo(); ?>

</body>
</html>


Most of the script, except one line, is just plain HTML. The PHP code just displays some information about PHP. You don't have to puzzle out what it all means. The important part is, Does it work? Save your new script to the "test" folder you created. Call the file info.php. You should now have something like this in your folder view:



If you can't see your new script in the test folder, then try again: it means you didn't save it to the correct location.

To test to see if it works, type the following into your browser, and hit the enter key:

http://127.0.0.1/test/info.php

The /test means "look in a folder called test". The /info.php part means "look for a file called info.php". If all went well, you should be looking at the following page:

The info.php page (click to open in a new window 66K)

If you saw the above page, then congratulations! Your PHP server is up and running, and you can make a start scripting PHP pages. Incidentally, another way to refer to your own PC is with "localhost". Try replacing this address:

http://127.0.0.1/test/info.php

with this one:

http://localhost/test/info.php

You should still see the same page. You can use either 127.0.0.1 or localhost, from now on - it's up to you.

We'll assume that everything is now up and running, though.
« Last Edit: August 05, 2007, 06:00:38 AM by Thinker_ » Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #2 on: August 05, 2007, 05:57:08 AM »

What is a Variable in PHP?

A variable is just a storage area. You put things into your storage areas (variables) so that you can use and manipulate them in your programmes. Things you'll want to store are numbers and text.

If you're ok with the idea of variables, then you can move on. If not, think of them like this. Suppose you want to catalogue your clothing collection. You enlist two people to help you, a man and a woman. These two people are going to be your storage areas. They are going to hold things for you, while you tally up what you own. The man and the woman, then, are variables.

You count how many coats you have, and then give these to the man. You count how many shoes you have, and give these to the woman. Unfortunately, you have a bad memory. The question is, which one of your people (variables) holds the coats and which one holds the shoes? To help you remember, you can give your people names! You could call them something like this:

mr_coats
mrs_shoes


But it's entirely up to you what names you give your people (variables). If you like, they could be called this:

man_coats
woman_shoes


Or

HimCoats
HerShoes


But because your memory is bad, it's best to give them names that help you remember what it is they are holding for you. (There are some things your people balk at being called. You can't begin their names with an underscore (_), or a number. But most other characters are fine.)

OK, so your people (variables) now have name. But it's no good just giving them a name. They are going to be doing some work for you, so you need to tell them what they will be doing. The man is going to be holding the coats. But we can specify how many coats he will be holding. If you have ten coats to give him, then you do the "telling" like this:

mr_coats = 10

So, the variable name comes first, then an equals sign. After the equals sign, you tell your variable what it will be doing. Holding the number 10, in our case. (The equals sign, by the way, is not really an equals sign. It's called an assignment operator. But don't worry about it, at this stage. Just remember that you need the equals sign to store things in your variables.)

However, you're learning PHP, so there's something missing. Two things, actually. First, your people (variables) need a dollar sign at the beginning (people are like that). So it would be this:

$mr_coats = 10

If you miss the dollar sign out, then your people will refuse to work! But the other thing missing is something really picky and fussy - a semi-colon. Lines of code in PHP need a semi-colon at the end:

$mr_coats = 10;

If you get any parse errors when you try to run your code, the first thing to check is if you've missed the semi-colon off the end. It's very easy to do, and can be frustrating. The next thing to check is if you've missed out a dollar sign. But back to our people (variables).

So the man is holding ten coats. We can do the same thing with the other person (variable):

$mrs_shoes = 25;

So, $mrs_shoes is holding a value of 25. If we then wanted to add up how many items of clothes we have so far, we could set up a new variable (Note the dollar sign at the begining of the new variable):

$total_clothes

We can then add up the coats and the shoes. You add up in PHP like this:

$total_clothes = $mr_coats + $mrs_shoes;

Remember, $mr_coats is holding a value of 10, and $mrs_shoes is holding a value of 25. If you use a plus sign, PHP thinks you want to add up. So it will work out the total for you. The answer will then get stored in our new variable, the one we've called $total_clothes. You can also add up like this:

$total_clothes = 10 + 35;

Again, PHP will see the plus sign and add the two together for you. Of course, you can add up more than two items:

$total_clothes = 10 + 35 + 7 + 38 + 1250;

But the idea is the same - PHP will see plus signs and then add things up. The answer is then stored in your variable name, the one to the left of the equals sign.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #3 on: August 05, 2007, 05:59:26 AM »

Putting Text into Variables

In the previous section, you saw how to put numbers into variables. But you can also put text into your variables. Suppose you want to know something about the coats you own. Are they Winter coats? Jackets? Summer coats? You decide to catalogue this, as well. You can put direct text into your variables. You do it in a similar way to storing numbers:

$coats1 = "Winter Coats";

Again, our variable name starts with a dollar sign ($). We've then given it the name coats1. The equals sign follows the variable name. After the equals sign, however, we have direct text - Winter Coats. But notice the double quotation marks around our text. If you don't surround your direct text with quotation marks, then you'll get errors. You can, however, use single quotes instead of double quotes. So you can do this:

$coats1 = 'Winter Coats';

But you can't do this:

$coats1 = 'Winter Coats";

In the above line, we've started with a single quote and ended with a double quote. This will get you an error.

We can store other text in the same way:

$coats2 = "Jackets";
$coats3 = "Summer Coats";


The direct text will then get stored in the variable to the left of the equals sign.

So, to recap, variables are storage areas. You use these storage areas to manipulate things like text and numbers. You'll be using variables a lot, and on the next posts you'll see how they work in practice.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #4 on: August 05, 2007, 06:10:20 AM »

Variables - Some Practice

In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll get some practice using variables. Off we go.

Testing variables with PHP

First, we'll take a look at how to display what's in your variables. We're going to be viewing our results on a web page. So see if you can get this script working first, because it's the one we'll be building on. Using a text editor like Notepad, or your PHP software, type the following. (You can copy and paste it, if you prefer. But you learn more by typing it out yourself - it doesn't really sink in unless you're making mistakes!)

<html>
<head>
<title>Variables - Some Practice</title>
</head>
<body>

<?php print("It Worked!"); ?>

</body>
</html>


When you've finished typing it all, save the page as variables.php. Then Run the script. Remember: when you're saving your work, save it to the WWW folder, as explained here. To run the page, start your browser up and type this in the address bar:

http://127.0.0.1/variables.php

Or this:

http://localhost/variables.php

If you've created a folder inside the www folder, then the address to type in your browser would be something like:

http://127.0.0.1/FolderName/variables.php

If you were successful, you should have seen the text "It worked!" displayed in your browser. If so, Congratulations! You have a working server up and running! (If you weren't successful, make sure that your server is up and running. If you're using easyPHP, you should see an icon in the bottom right of your screen. It will be the letter "e". A red square will be flashing, if the server is running.)
The PHP script is only one line long:

<?php print("It Worked!"); ?>

The rest of the script is just plain HTML code. Let's examine the PHP in more detail.

We've put the PHP in the BODY section of an HTML page. Scripts can also, and often do, go between the HEAD section of an HTML page. You can also write your script without any HTML. But before a browser can recognise your script, it needs some help. You have to tell it what kind of script it is. Browsers recognise PHP by looking for this punctuation (called syntax):

<?php ?>

So you need a left angle bracket ( < ) then a question mark ( ? ). After the question mark, type PHP (in upper or lowercase). After your script has finished, type another question mark. Finally, you need a right angle bracket ( > ). You can put as much space as you like between the opening and closing syntax.

To display things on the page, we've used print( ). What you want the browser to print goes between the round brackets. If you're printing direct text, then you need the quotation marks (single or double quotes). To print what's inside of a variable, just type the variable name (including the dollar). Finally, the line of code ends as normal - with a semi-colon (Wink. Another way to display things on the page is to use an alternative to print() – echo( ).

Now let's adapt the basic page so that we can set up some variables. We'll try some text first. Keep the HTML as it is, but change your PHP from this:

<?php print("It Worked!"); ?>

To this:

<?php

print("It Worked!");

?>


OK, it's not much of a change! But spreading your code out over more than one line makes it easier to see what you're doing. Now, it's clear that there's only one line of code - Print. So add this second line to your code (the one in red):


<?php

$test_String = "It Worked!";

print("It Worked!");

?>


We've set up a variable called $test_String. After the equals sign, the text "It Worked!" has been added. The line is then ended with a semi-colon. Don't run your script yet. Change the Print line to this:

print($test_String);

Then add some comments ...


<?php
//--------------TESTING VARIABLES------------

$test_String = "It Worked!";
print($test_String);
?>


Comments in PHP are for your benefit. They help you remember what the code is supposed to do. A comment can be added by typing two slashes. This tells PHP to ignore the rest of the line. After the two slashes, you can type anything you like. Another way to add a comment, is like this:

<?php

/* --------------TESTING VARIABLES------------
Use this type of comment if you want to spill over to more than one line.
Notice how the comment begin and end.
*/

$test_String = "It Worked!";

print($test_String);

?>


Whichever method you choose, make sure you add comment to your code: they really do help. Especially if you have to send your code to someone else!

But you can now run the script above, and test it out.

How did you get on? You should have seen that exactly the same text got printed to the page. And you might be thinking - what's the big deal? Well, what you just did was to pass some text to a variable, and then have PHP print the contents of the variable. It's a big step: your coding career has now begun!

Exercise
Change the text "It Worked!" to anything you like. Then run the script again. Try typing some numbers in between your double quotes, instead of text.

Exercise
Change the double quotes to single quotes. Did it have any effect? Put a single quote at the beginning of your text, and a double quote at the end. What happens when you run the code?

Exercise
Delete the dollar sign from the variable name. Then run your code. What error did you get? Put the dollar sign back, but now delete the semi-colon. Run your code again? What error did you get, this time? It's well worth remembering these errors - you'll see them a lot when you're starting out! If you see them in future, you'll be better able to correct your errors.

 

Now that you're up and running, we'll do some more variable work.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #5 on: August 05, 2007, 06:11:35 AM »

More Variable Practice

Now that you can print text to a page, let's try some numbers. Start with the basic PHP page again, and save your work as variables2.php:

<html>
<head>
<title>More on Variables</title>
</head>
<body>

<?php

print ("Basic Page");

?>

</body>
</html>


We'll now set up a variable and print it to the page. So change your code to this:

<?php

$first_number = 10;
print ($first_number);

?>


All the code does is to print the contents of the variable that we've called $first_number. Remember: if you're printing direct text then you need quotation marks; if you're printing a variable name then you leave the quotes out. To see why, run the first script above. Then change the print line to this:

print ("$first_number");

In other words, add double quotation marks around your variable name. Did it make a difference? What did you expect would print out? Now change the double quotes to single quotes. Run your script again. With double quotes, the number 10 still prints; with single quotes, you get the variable name!

 

TIP: We recommend you use single quotes for your direct text, and NOT double quotes - there's fewer hassles if you do!
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #6 on: August 05, 2007, 06:14:13 AM »

Concatenate

You can join together direct text, and whatever is in your variable. The full stop (period or dot, to some) is used for this. Suppose you want to print out the following "My variable contains the value of 10". In PHP, you can do it like this:

<?php

$first_number = 10;
$direct_text = 'My variable contains the value of ';

print ($direct_text . $first_number);

?>


So now we have two variables. The new variable holds our direct text. When we're printing the contents of both variables, a full stop is used to separate the two. Try out the above script, and see what happens. Now delete the dot and then try the code again. Any errors?

You can also do this sort of thing:

<?php

$first_number = 10;

print ('My variable contains the value of ' . $first_number);

?>


This time, the direct text is not inside a variable, but just included in the Print statement. Again a full stop is used to separate the direct text from the variable name. What you've just done is called concatenation. Try the new script and see what happens.

Adding up in PHP

OK, let's do some adding up. To add up in PHP, the plus symbol (+) is used. (If you still have the code open from the previous page, try changing the full stop to a plus symbol. Run the code, and see what happens.)

To add up the contents of variables, you just separate each variable name with a plus symbol. Try this new script:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;

$direct_text = 'The two variables added together = ';

print ($direct_text . $sum_total);

?>


In the above script, we've added a second number, and assigned a value to it:

$second_number = 20;

A third variable is then declared, which we've called $sum_total. To the right of the equals sign, we've added up the contents of the first variable and the contents of the second variable:

$sum_total = $first_number + $second_number;

PHP knows what is inside of the variables called $first_number and $second_number, because we've just told it in the two line above! It sees the plus symbol, then adds the two values together. It puts the answer to the addition in the variable to the left of the equals sign (=), the one we've called $sum_total.

To print out the answer, we've used concatenation:

print ($direct_text . $sum_total);

This script is a little more complicated than the ones you've been doing. If you're a bit puzzled, just remember what it is we're doing: adding the contents of one variable to the contents of another. The important line is this one:

$sum_total = $first_number + $second_number;

The addition to the right of the equals sign gets calculated first ($first_number + $second_number). The total of the addition is then stored in the variable to the left of the equals sign ($sum_total =).

You can, of course, add up more than two numbers. Try this exercise.


Exercise
Add a third variable to your code. Assign a value of 30 to your new variable. Put the sum total of all three variables into the variable called $sum_total. Use concatenation to display the results. (In other words, add up 10, 20, and 30!)


You don't have to use variable names to add up. You can do this:

print (10 + 20 + 30);

Or even this:

$number = 10;

print ($number + 30);

But the point is the same - use the plus symbol (+) to add up.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #7 on: August 05, 2007, 06:18:49 AM »

How to Subtract in PHP

We're not going to weigh things down by subjecting you to torrents of heavy Math! But you do need to know how to use the basic operators. First up is subtracting.

To add up using PHP variables, you did this:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;

print ($sum_total);

?>


Subtraction is more or less the same. Instead of the plus sign (+), simply use the minus sign (-). Change your $sum_total line to this, and run your code:

$sum_total = $second_number - $first_number;

The s$sum_total line is more or less the same as the first one. Except we're now using the minus sign instead (and reversing the two variables). When you run the script you should, of course, get the answer 10. Again, PHP knows what is inside of the variables called $second_number and $first_number. It knows this because you assigned values to these variables in the first two lines. When PHP comes across the minus sign, it does the subtraction for you, and puts the answer into the variable on the left of the equals sign. We then use a print statement to display what is inside of the variable.

Just like addition, you can subtract more than one number at a time. Try this:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number - $second_number - $first_number;

print ($sum_total);

?>


The answer you should get is 70. You can also mix addition with subtraction. Here's an example:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number - $second_number + $first_number;

print ($sum_total);

?>


Run the code above. What answer did you get? Was it the answer you were expecting? Why do you think it printed the number it did? If you thought it might have printed a different answer to the one you got, the reason might be the way we set out the sum. Did we mean 100 - 20, and then add the 10? Or did we mean add up 10 and 20, then take it away from 100? The first sum would get 90, but the second sum would get 70.

To clarify what you mean, you can use parentheses in your sums. Here's the two different versions of the sum. Try them both in your code. But note where the parentheses are:

Version one
$sum_total = ($third_number - $second_number) + $first_number;

Version two
$sum_total = $third_number - ($second_number + $first_number);


It's always a good idea to use parentheses in your sums, just to clarify what you want PHP to calculate. That way, you won't get a peculiar answer!

Another reason to use parentheses is because of something called operator precedence. In PHP, some operators (Math symbols) are calculated before others. This means that you'll get answers that are entirely unexpected!
Logged

Computer Science: Solving today's problems, tomorrow!
Mop (Gb)
Loyal 110MB Member
*******
Online Online

Posts: 4297


Don't Panic!


WWW
« Reply #8 on: August 05, 2007, 08:30:20 AM »

I already posted a PHP Guide... Yours has installation though.
Logged




Support conficker! Add dino to your sig!
Cool Dude
Member
*
Offline Offline

Posts: 40


« Reply #9 on: August 05, 2007, 10:35:40 AM »

WOW....i didnt read all of it but it does sound interesting....
Logged
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #10 on: August 05, 2007, 09:21:25 PM »

Before continuing, I should make clear that this guide is for beginners in programming in general. Gb, I'm sorry if it seemed as if I was doubting your guide... Actually I would appreciate it if you posted a link in your previous post, so that people will have an alternative when they see this topic. Thanks for mentioning it.


PHP and Multiplication

To multiply in PHP (and just about every other programming language), the * symbol is used. If you see 20 * 10, it means multiply 20 by 10. Here's some code for you to try:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $second_number * $first_number;

print ($sum_total);

?>


In the above code, we're just multiplying whatever is inside of our two variables. We're then assigning the answer to the variable on the left of the equals sign. (You can probably guess what the answer is without running the code!)

Just like addition and subtraction, you can multiply more than two numbers:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number * $second_number * $first_number;

print ($sum_total);

?>


And you can even do this:

$sum_total = $third_number * $second_number * 10;

But try this code. See if you can guess what the answer is before trying it out:

<?php

$first_number = 10;
$second_number = 2;
$third_number = 3;

$sum_total = $third_number + $second_number * $first_number;

print ($sum_total);

?>


What answer did you expect? If you were expecting to get an answer of 50 then you really need to know about operator precedence! As was mentioned, some operators (Math symbols) are calculated before others in PHP. Multiplication and division are thought to be more important that addition and division. So these will get calculated first. In our sum above, PHP sees the * symbol, and then multiplies these two numbers first. When it works out the answer, it will move on to the other symbol, the plus sign. It does this first:

$second_number * $first_number;

Then it moves on to the addition. It doesn't do this first:

$third_number + $second_number

This makes the parentheses more important than ever! Use them to force PHP to work out the sums your way. Here's the two different version. Try them both:

Version one
$sum_total = $third_number + ($second_number * $first_number);

Version two
$sum_total = ($third_number + $second_number) * $first_number;



Here's we're using parentheses to force two different answers. PHP will work out the sum between the parentheses first, and then move on to the other operator. In version one, we're using parentheses to make sure that PHP does the multiplication first. When it gets the answer to the multiplication, THEN the addition is done. In version two, we're using parentheses to make sure that PHP does the addition first. When it gets the answer to the addition, THEN the multiplication is done.

PHP and Division

To divide one number by another, the / symbol is used in PHP. If you see 20 / 10, it means divide 10 into 20. Try it yourself:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $second_number / $first_number;

print ($sum_total);

?>


Again, you have to be careful of operator precedence. Try this code:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number - $second_number / $first_number;

print ($sum_total);

?>


PHP won't work out the sum from left to right! Division is done before subtraction. So this will get done first:

$second_number / $first_number

And NOT this:

$third_number - $second_number

Using parentheses will clear things up. Here's the two versions for you to try:

Version one
$sum_total = $third_number - ($second_number / $first_number);

Version two
$sum_total = ($third_number - $second_number) / $first_number;


The first version will get you an answer of 98, but the second version gets you an answer of 8! So remember this: division and multiplication get done BEFORE subtraction and addition. Use parentheses if you want to force PHP to calculate a different way.
Logged

Computer Science: Solving today's problems, tomorrow!
Thinker_
Active Member
**
Offline Offline

Posts: 56


WWW
« Reply #11 on: August 05, 2007, 09:28:59 PM »

Floating Point Numbers in PHP

A floating point number is one that has a dot in it, like 0.5 and 10.8. You don't need any special syntax to set these types of numbers up. Here's an example for you to try:

<?php

$first_number = 1.2;
$second_number = 2.5;
$sum_total = $second_number + $first_number;

print ($sum_total);

?>


You add up, subtract, divide and multiply these numbers in exactly the same way as the integers you've been using. A warning comes with floating point numbers, though: you shouldn't trust them, if you're after a really, really precise answer!

 
Some Exercises

To round up this section on number variables, here's a few exercises (In your print statements, there should be no numbers – just variable names):

Exercise
Write a script to add up the following figures: 198, 134, 76. Use a print statement to output your answer.

Exercise
Write a script to add up the following two numbers: 15, 45. Then subtract the answer from 100. Use a print statement to output your answer.

Exercise
Use variables to calculate the answer to the following sum:
(200 * 15) / 10
Use a print statement to output your answer.


If Statements in PHP

You saw in the last section that variables are storage areas for your text and numbers. But the reason you are storing this information is so that you can do something with them. If you have stored a username in a variable, for example, you'll then need to check if this is a valid username. To help you do the checking, something called Conditional Logic comes in very handy indeed. In this section, we'll take a look at just what Conditional Logic is. In the next section, we'll do some practical work.

 
Conditional Logic

Conditional Logic is all about asking "What happens IF ... ". When you press a button labelled "Don't Press this Button - Under any circumstance!" you are using Conditional Logic. You are asking, "Well, what happens IF I do press the button?"

You use Conditional Logic in your daily life all the time:

"If I turn the volume up on my stereo, will the neighbours be pleased?"
"If spend all my money on a new pair of shoes, will it make me happy?"
"If I study this course, will it improve my web site?"


Conditional Logic uses the "IF" word a lot. For the most part, you use Conditional Logic to test what is inside of a variable. You can then makes decisions based on what is inside of the variable. As an example, think about the username again. You might have a variable like this:

$User_Name = "My_Regular_Visitor";

The text "My_Regular_Visitor" will then be stored inside of the variable called $User_Name. You would use some Conditional Logic to test whether or not the variable $User_Name really does contain one of your regular visitors. You want to ask:

"IF $User_Name is authentic, then let $User_Name have access to the site."

In PHP, you use the "IF" word like this:

if ($User_Name = = "authentic") {
//Code to let user access the site here;
}


Without any checking, the if statement looks like this:

if ( ) {

}


You can see it more clearly, here. To test a variable or condition, you start with the word "if". You then have a pair of round brackets. You also need some more brackets - curly ones. These are just to the right of the letter "P" on your keyboard (Well, a UK keyboard, anyway). You need the left curly bracket first { and then the right curly bracket } at the end of your if statement. Get them the wrong way round, and PHP refuses to work. This will get you an error:

if ($User_Name = = "authentic") }
//Code to Let user access the site here;
{


And so will this:

if ($User_Name = = "authentic") {
//Code to Let user access the site here;
{


The first one has the curly brackets the wrong way round (should be left then right), while the second one has two left curly brackets.

In between the two round brackets, you type the condition you want to test. In the example above, we're testing to see whether the variable called $User_Name has a value of "authentic":

($User_Name = = "authentic")

Again, you'll get an error if you don't get your round brackets right! So the syntax for the if statement is this:

if (Condition_or_Variable_to_test) {
//your code here;
}


In the next part, we'll use if statements to display an image on the page.
We'll use the print statement to "print out" HTML code. As an example, take the following HTML code to display an image:

<IMG SRC =church.jpg>

Just plain HTML. But you can put that code inside of the print statement:
print ("<IMG SRC =images\church.jpg>");

When you run the code, the image should display. Of course, you'll need an image called church.jpg, and in a folder called images.
Copy the images folder to your www (root) directory. Then try the following script:

<?PHP

print ("<IMG SRC =images\church.jpg>");

?>


Save your script to the same folder as the images folder (though NOT inside the images folder). Now fire up your server, and give it a try. Hopefully, you'll see the church image display.
Logged

Computer Science: Solving today's problems, tomorrow!
mywar
Member
*
Offline Offline

Posts: 2


« Reply #12 on: January 10, 2008, 06:06:36 PM »

thanks this helped me alot
Logged
frendz
Web Designer
Member
*
Offline Offline

Posts: 39

Pay me and i will make ur site!


WWW
« Reply #13 on: January 13, 2008, 09:35:50 PM »

thanks but i have already read those on free tutorials sites  Duh
Logged

http://frendz.110mb.com

Leave comments in my guestbook and chat with me in my shoutbox
Pages: [1]   Go Up
Send this topic | Print
Jump to: