Saturday, December 8, 2012

PHP Variables - PHP Lesson 3

Using Variables

 

       A Variable in PHP, simply put, is one thing that means another thing or things - a “container” if you will. It can represent text, numbers, calculations, and more.

 

Variables are quite powerful, and if you mess ‘em up, they’ll come get you in the middle of the night.

 

Declaring a variable is easy. No spaces in the variable name, please - PHP doesn’t like that...

 

<?php

$This_thing = “The Other Thing”;

?>

Now before you go off saying “What the heck would I need THAT for?”, remember that variables are very useful...especially if you are PHP Include-ing other files (Like the foreshadowing there? Do ya?)

 

Real World Usage For Variables

 

Here’s an example of how you can use a variable in the real world: show the current date on your website.

 

<?php

$today = date("F j, Y");

echo "$today";

?>

 

This example sets the date command as a variable called “$today”, and uses echo to display it on the screen.

 

And now, for a quick tangent...    

 

More about the “DATE” command - it is very versatile and flexible - see the guide below to use it to it’s potential!
 

The DATE command

 

Expanding on the above example, here are the options for DATE and TIME display:

 

Time:

                a:  am or pm

                A:  AM or PM

                g:  Hour without leading zeroes (1-12)

                G:  Hour in military time without leading zeroes (0-23)

                h:  Hour with leading zeroes (01-12)

                H:  Hour in military time with leading zeroes (00-23)

                i:  Minute with leading zeroes (00-59)

                s:  Seconds with leading zeroes (00-59)

 

Days:

                d:  Day of the month with leading zeroes (01-31)

                j:  Day of the month without leading zeroes (1-31)

                D:  Day of the week abbreviations (Sun – Sat)

                I:  Day of the week (Sunday – Saturday)

                w:  Day of the week without leading zeroes (0-6)

                z:  Day of the year without leading zeroes (1-365)

 

Months:

                m:  Month of the year with leading zeroes (01-12)

                n:  Month of the year without leading zeroes (1-12)

                M:  Month abbreviations (Jan – Dec)

                F:  Month names (January – December)

                t:  Number of days in the month (28-31)

 

Years:

                L:  Displays 1 if it is a leap year, 0 if not

                Y:  Year in 4-digit format (2006)

                y:  Year in 2-digit format (06)

                Other Date Formats:

                r:  Full date, including timestamp and timezone offset (O)

                U:  Number of seconds since the Unix Epoch (Jan. 1, 1970)

                O:  Offset difference from Greenwich Meridian Time (GMT).  100 = 1 hour, -100 = -1 hour
 

And now...back to Variables!
 
 
ECHOing more that one Variable at a time
 
You can use the ECHO command we learned earlier to display more than one variable at a time. Combining variables can be extremely useful. Take a look at this example...
 
<?php
$phrase1 = "That's No Moon,";
$phrase2 = "It's a Space Station!";
echo "$phrase1 $phrase2";
?>
This code example will show up in your web browser like this:
 
Neat, huh?
 
So, you can see where this might be useful, I hope?
 
You can also echo text and variables in the same statement by putting periods around the variable....like so...
 
<?php
$items = 3;
echo "You have purchased ".$items." items.";

?>
 
 



 

No comments:

Post a Comment