Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,231 members, 7,818,781 topics. Date: Monday, 06 May 2024 at 02:46 AM

An Introduction To PHP - Beginners Base - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / An Introduction To PHP - Beginners Base (3358 Views)

How To Connect Vb.net Windows Application To Php And My Sql Database / Convert My Cms From Cfm To Php / An Introduction To Programming Using C (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: An Introduction To PHP - Beginners Base by hushmail: 9:12am On Sep 12, 2014
pls i will like to know whats happening to dis thread

some1 pls tell
Re: An Introduction To PHP - Beginners Base by ify01: 8:06pm On Sep 12, 2014
hushmail: pls i will like to know whats happening to dis thread

some1 pls tell
u want me to countinue?
Re: An Introduction To PHP - Beginners Base by hushmail: 10:48pm On Sep 12, 2014
ify01: u want me to countinue?

u dey joke?

i thought u were banned? Anyway pls continue.

I hope to learn programming from u.
Re: An Introduction To PHP - Beginners Base by ify01: 6:16pm On Sep 13, 2014
4. More PHP Variable Practice



In the previous section, you started to work with variables. You outputted text to a page. In the next few sections, you'll do some more work with variables, and learn how to do your sums with PHP.

But 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"wink;

?>

</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"wink;

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!
Re: An Introduction To PHP - Beginners Base by hushmail: 1:05pm On Sep 14, 2014
sorry

u must have jumped a section

pls recheck
Re: An Introduction To PHP - Beginners Base by ify01: 2:58pm On Sep 14, 2014
hushmail: sorry

u must have jumped a section

pls recheck
I skipped number 3 and 4 of section One because i was banned why trying to post them
Re: An Introduction To PHP - Beginners Base by ify01: 3:18pm On Sep 14, 2014
5. PHP Concatenation





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.
Re: An Introduction To PHP - Beginners Base by hushmail: 10:52pm On Sep 14, 2014
ify01:
I skipped number 3 and 4 of section One because i was banned why trying to post them

ok is it posible to follow wt out those sections?

Cant u edit to avoid ban?
Re: An Introduction To PHP - Beginners Base by ify01: 5:05pm On Sep 15, 2014
hushmail:

ok is it posible to follow wt out those sections?

Cant u edit to avoid ban?
yes, is possible to follow except if you need to configure your wampserver
Re: An Introduction To PHP - Beginners Base by hushmail: 6:38am On Sep 16, 2014
ify01:
yes, is possible to follow except if you need to configure your wampserver

am lost already
Re: An Introduction To PHP - Beginners Base by ify01: 9:11am On Sep 17, 2014
hushmail:

am lost already
state where u are lost b4 i countinue
Re: An Introduction To PHP - Beginners Base by Shuayb0(m): 9:57am On Sep 17, 2014
You re doing a great job bro, i did everything i ought to do, bt each tym i tried to view d script on my browser, it say d url cannot b found nd my php script is save inside d www directory. Also my script is saved wth .php, dou it's still been recognized as a text document. what do i do?. Gracias
Re: An Introduction To PHP - Beginners Base by hushmail: 2:24pm On Sep 17, 2014
ify01:
state where u are lost b4 i countinue

isn't it obvious?

D sections u skipped
Re: An Introduction To PHP - Beginners Base by ify01: 1:18am On Sep 20, 2014
Shuayb0: You re doing a great job bro, i did everything i ought to do, bt each tym i tried to view d script on my browser, it say d url cannot b found nd my php script is save inside d www directory. Also my script is saved wth .php, dou it's still been recognized as a text document. what do i do?. Gracias
Bro, i think you're encountering error because of the parts i skipped. You can read it here - Installing and Testing Wampserver
Re: An Introduction To PHP - Beginners Base by ify01: 1:20am On Sep 20, 2014
hushmail:

isn't it obvious?

D sections u skipped
Bro, am sorry i can't post those sections to aviod being banned. You can read it here - Installing and Testing Wampserver
Re: An Introduction To PHP - Beginners Base by Valentinooo: 10:12pm On Sep 23, 2014
The poster at the original site has stopped posting too
Re: An Introduction To PHP - Beginners Base by luckynzo(m): 9:03am On Sep 24, 2014
Op. Great job u are doing. I hav AMPPS. Can i use it as i already have it installed on my system instead of downloading the WAMP server?
Re: An Introduction To PHP - Beginners Base by ify01: 9:03pm On Sep 24, 2014
luckynzo: Op. Great job u are doing. I hav AMPPS. Can i use it as i already have it installed on my system instead of downloading the WAMP server?
If u are sure that it can cope with the tutorial
Re: An Introduction To PHP - Beginners Base by ify01: 9:58pm On Sep 24, 2014
6. Addition 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.



In the few next parts, you'll learn how to Subtract, Divide and Multiply.
Re: An Introduction To PHP - Beginners Base by ify01: 6:21am On Oct 01, 2014
7. Subtraction in PHP


I Am 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! As we'll find out right now in the next part - Multiplication.
Re: An Introduction To PHP - Beginners Base by ify01: 7:41am On Oct 01, 2014
8. Multiplication in PHP

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.



In the next part, we'll take a look at division.
Re: An Introduction To PHP - Beginners Base by ify01: 8:12am On Oct 01, 2014
9. Division in PHP

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.



In the next part, we'll take a look at how PHP handles floating point numbers.
Re: An Introduction To PHP - Beginners Base by ify01: 12:46pm On Oct 01, 2014
10. floating point numbers

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.

In our next section, we'll take a look at Conditional Logic.

LOST? Drop your queries b4 i countinue
Re: An Introduction To PHP - Beginners Base by Xionglu(m): 3:20pm On Feb 18, 2015
Shuayb0:
You re doing a great job bro, i did everything i ought to do, bt each tym i tried to view d script on my browser, it say d url cannot b found nd my php script is save inside d www directory. Also my script is saved wth .php, dou it's still been recognized as a text document. what do i do?. Gracias
save it as all files,just down the file name
u see a drop down box that says save as type and u pick all files,that should work bro

1 Like

Re: An Introduction To PHP - Beginners Base by Possessedkid: 3:02am On Nov 09, 2023
Interesting

(1) (2) (Reply)

Why Do Young Startup Founders Drop Out Of School / Why Is Python Programming So Hard To Understand / Am I Nigeria's First Scea ?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 57
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.