Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 2

How to Program PHP
Tutorial, Part 2

Welcome to part 2 in this PHP tutorial tutorial! In the previous tutorial, we explored the basic "echo" statement, and some pretty fundamental concepts in PHP statements, such as the semicolon at the end of every statement. In this tutorial, we'll explore variables, which are a pretty fundamental concept that we'll be using later on for interactive applications.

When many people see "variables" in the context of computer programming, algebra quickly comes to mind. Don't worry, you won't need to brush up on your mathematical equations. Variables are a simple concept in programming. Tongue

What is a variable?

A variable is essentially just a container that has a value. Yes, these variables have real world applications in algebra, but we won't be getting into sines, cosines, quadratic equations, derivatives, or anything else of that sort. As far as we're concerned, a variable simply holds a value for later use.

Variables in PHP are destroyed as soon as the script is finished executing, so they don't store data long term. If you need to process factory orders or user registrations, a database will be required for that. Nevertheless, variables are very much an important concept that will be foundational to every PHP application. Tongue

$var1 = 5;
$var2 = 9;
echo "var1 + var2: ";
$result = $var1 + $var2;
echo $result;

Following yet? Yes, that looks like giberish. Tongue

Variables always start with a dollar sign in PHP ($), so you should be able to recognize where the variables are just by looking at the above code. As you can see, in the first two lines of this code, we simply define $var1 to be 5, and $var2 to be 9. These are the values that these variables contain. We could have named these variables anything. We did not have to name then $var1, $var2, etc... We could have called them $apple, $banana, etc... and they would have functioned exactly the same way.

The next statement is just an echo statement. You may be tempted to think that this first echo statement will add var1 and var2, sending 14 to the browser for you to see on the page, but remember that because the text is in quotes, this is just a text string. Since we're only sending a text string, you will literally see "var1 + var2: " in the browser from this statement.

The next statement adds $var1 and $var2, then puts the answer into the $result variable. Remember, we defined $var1 to be equal to 5, and $var2 to be equal to 9. When we add them, they will equal 14, and that answer goes into $result. As stated before, these variables could have been named anything. It's always wise to keep variable names as descriptive as possible in programming, as it does help a lot when you start writing larger scripts confidently. To be honest, $var1 and $var2 aren't the most descriptive names, but you see the idea.

The next statement echoes $result, which of course equals 14 and is displayed in the browser. I encourage you to try this script yourself on your own server or WAMP. Try playing around with it some to get a feel for it. Variables are an important concept in PHP, and they are very simple once you get the hang of it.

More examples

There is quite a lot that is possible with variables, well beyond simply adding numbers. Here is another example. I encourage you to try this script out on your own, playing around with the different numbers and math operators to get a feel for how it works.

$apples = 5;
$oranges = 995;
$cucumbers = 3;
$result = $apples + $oranges + $cucumbers;
echo "total fruits: ";
echo $result; 
echo "<br>"; // the <br> tag is the HTML tag for a new line. 
$good_fruits = $result - $cucumbers; // we don't like cucumbers. 
echo "Since we don't like cucumbers, the number of good fruits is: ";
echo $good_fruits;

As you can see, we've played around with the math in these statements, using some addition and subtraction. I encourage you to try these scripts out on your own to get a feel for how variables work in use.

Text in variables

In algebra, variables always contained numbers. In programming, variables can contain practically anything. Some kinds of variables (which we'll get to later) can even contain files. Often, you'll see variables with text in them. PHP deals with text variables the same way that it deals with variables containing numbers. (Just don't try to perform a "text string" + "text string #2" addition operation. I've never really tried that to see what exactly PHP says about that, but I'm sure you will get some sort of complaint about attempting a math operation on text. Tongue )

$mytext = "this is a text string inside of a variable";


So that's more or less the basics, but there is more to variables that needs to be said. An array is a concept we'll be using very shortly that makes it very simple to organize data if you have a lot of data to organize. Essentially, an array is a variable that contains more than one value. Here is how arrays look in code:

$fruits['apples'] = 5;
$fruits['oranges'] = 9;
$total_fruits = $fruits['apples'] + $fruits['oranges'];
echo $total_fruits;

As you can see, both the number of apples and the number of oranges are stored into the "fruits" variable, but we've made use of an array. In an array, each individual "value" that it stores is called an element. Here, 'oranges' and 'apples' are both elements of the array that is called $fruits. $fruits['oranges'] = 9 is a statement that sets the "oranges" element in the array to 9. It won't affect the "apples" element since "apples" is in a different part of the array. Also, notice how 'oranges' and 'apples' are in brackets. Arrays always have to structured like this so that PHP knows that you are referring to an array in code. Arrays may not seem necessary in this particular situation, but they will become very useful later.

It turns out arrays don't just have to have text in the brackets. We could have used numbers as well.

$fruits[1] = 5;
$fruits[2] = 9;
$total_fruits = $fruits[1] + $fruits[2];
echo $total_fruits;

The code above functions exactly the same way as the original arrays example does. If you run this script in your browser, you will see the exact same result. The only difference is that we identified the different "elements" of the $fruits array with numbers instead of with text strings. Either way is valid in PHP, although for this particular example, it is much more practical for us to use text strings instead, for obvious reasons. Tongue

Arrays can be incredibly useful when it comes to some applications. Two variables, to be specific, are very important to dealing with HTML forms. We'll be dealing with $_GET and $_POST arrays later on, but these are basically arrays that contain the values of URL fields and form fields, and we'll be getting to those later on.

The next tutorial...

In the next tutorial, we'll be building on more concepts putting these variables to use. We'll introduce the function. stay tuned!

Next Tutorial: Introduction to Functions
Directory of PHP Tutorials - Our next project...
That was easy. Going to part 3 and I'll probably leave part 4 for tomorrow. Thanks again for these Darth-Apple! Smile
No problem, glad to see they are of use. I'm planning to post part five tonight. Big Grin - Our next project...

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 6 Darth-Apple 2 5,056 November 12th, 2013 at 5:31 PM
Last Post: Damian B.
  PHP - Part 5 Darth-Apple 5 8,627 August 28th, 2013 at 10:10 PM
Last Post: Darth-Apple
  PHP - Part 1 Darth-Apple 4 7,977 August 19th, 2013 at 7:40 PM
Last Post: Damian B.
  PHP - Part 4 Darth-Apple 0 3,477 August 13th, 2013 at 4:29 PM
Last Post: Darth-Apple

Users browsing this thread: 1 Guest(s)

Makestation Theme/Design Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2021
Forum design by Makestation Team © 2020 - a modern day time capsule | Makestation Ajax Chat Hosting