post icon

Day 3: Getting Started with Dynamic Web Development (PHP & MySQL) – PHP For Beginners

Welcome back everyone!  By now most of you have become pretty comfortable with the basic concept of how PHP works (at least in the simplest sense) and have had your share of fun with ‘echoing’ all kinds of neat little messages all over your pages!  Most of you are also probably wondering how this exercise is even remotely helpful to you since you are adding a bunch of code to something that can easily be accomplished in plain old HTML… AND with far less work!  Patience young grasshoppah… lets walk over that hill and find the answer to your troubles… Namaste.

BEYOND ‘ECHO’

Now, ‘echoing’ some text onto a page might not seem incredibly fascinating, but let’s take a stab at making it a bit more interesting than simply displaying some text, by taking a look at variables.

Most of your entire PHP ‘existence’ will revolve around the concept of variables.  For you algebra grads out there, I don’t have to remind you what variables are, but for those of us who have successfully blocked out most of high school (traumatic experiences will do that to ya), let’s take 13 seconds to revisit:

Variables are simply placeholders… think of them as stand-ins for something else.  Variables can be used an unlimited number of times and we can change their actual value at any point…

The way we use variables in PHP is very simple.  In order for PHP to identify a variable it needs to:

  1. Be within a PHP code block (i.e. <?php … ?>)
  2. Start with a ‘$’ (dollar sign)
  3. Contain no spaces

That’s it.  Too easy you say?  It sure is!  Now, it should be noted that there are a couple more rules when it comes to variables:

Initial Declaration

Before we can actually use a variable anywhere, we need to declare it, or assign it a value.  Once you’ve declared a variable, you can start using it.

Example (String/Text variable):

<?php
$animal="dog";
?>

Notice how we’ve defined the variable, then we’ve assigned the value “dog” to it, by using the ‘=’ (equals) sign.

It’s important to note that since we are assigning text or a ‘string’ to this variable, we need to remember to use the “quotes” around it.  If this was a numeric variable, we would simply use numbers with no quotes… more on that later.

Finally, we’ve finished our declaration, by closing with a ‘;’ (semicolon).  That tells PHP that we are finished with that part.

Subsequent Usage

Now that we’ve declared our variable, let’s use it for something!

Below is an example of how you could use this variable… I will show you two ways you can accomplish the exact same thing:

Example 1:

<?php
$animal="dog";
?>

My favorite animal is a <?php echo $animal; ?>. A <?php echo $animal; ?> is man's best friend.

Example 2:

<?php
$animal="dog";
echo "My favorite animal is a $animal. A $animal is man's best friend.";
?>

Notice the difference in the code use?  Try this out on your own, testing out both methods. Regardless of which way you go with, both of these examples will give you the exact same results:

My favorite animal is a dog. A dog is man’s best friend.

Take a few moments to think about what you just tried.  The concept you should take away from it is this:

PHP code can either be sprinkled into your existing HTML, or you can use PHP to generate content as a whole.

One More Thing…

I really need to point this out before we go any further… please remember this, write it down, tattoo it onto your arm if you need to:

PHP works LINEARLY… meaning the code is read from start to finish or top to bottom.

Keep that very present as you code.  Any variable you declare or initialize, MUST be set before you start using it.

Variables as Numbers

Okay, we’ve now seen an example of variables being used to display text, but what if we wanted to use numbers instead?

Good on Ya!  That’s the spirit!  Assigning numbers to variables allows us to do much more than simply spit out a sentence or two!  Take a look:

Example (Numeric variable):

<?php

$num1=5;
$num2=10;
$result=$num1+$num2;

echo "Adding $num1 to $num2 gives us: $result!";

?>

Try this out on your own.  If you copy/pasted just right, you should get:

Adding 5 to 10 gives us: 15!

Purty neat huh?  Notice that when we assigned the numbers to our variables, we did not use “quotes” to surround those values.  Leaving out the quotes tells PHP that these are values and not just strings of text, which force it to evaluate or calculate the values for us.

Our $result variable is where all the magic happens.  Notice how we are able to add the variables together without having to explicitly say: $result=5+10; (although that would also work… but you get the point).

DAY 3 WRAPUP

We’ll stop right there for today, but as always, the fun doesn’t have to stop on your end; play with the code and try other variations of it.  Practically every type of calculation can be performed, using the above format.

As a reference to you, here are the very basic operations and how to express them in your code:

  • ‘+’ : Adding (i.e. 10+5)
  • ‘-’ : Subtracting (i.e. 10-5)
  • ‘*’ : Multiplying (i.e. 10*5)
  • ‘/’ : Dividing (i.e. 10/5)

See you all back here for Day 4 – Form Integration, where we’ll be dipping our toe into the web2.0 realm and really start looking at how we can not only talk TO our adoring public, but talk WITH them, using basic form integration.

See you all then!

No comments yet.

Leave a comment

Leave a Reply