Oct
02
2008

PHP Basics: Variables and Constants

PHP is all about Variables! If you are intending to take the Zend PHP 5 Certification then you should be already very aware of Variables therefore I’m going to keep this one small and simple. Constants are even simpler than Variables both PHP Variables and Constants basics are bellow and feature (a lot) in the Certification.

Variables

Widly Used in any language! Variables are temparary storage containsers and contain any data in any data type! Variables are identified by money, also known as the doller sign $ followed by their name such as $variable . Variables can only contain numbers, letters and underscores in their name.
They are easy to use, but are confusing at first as they are case sensitive and must only start with letters or underscores!

$variable = "I am valid";
$variable1 = "I am Valid";
$_variable = "I am Valid";
$1variable = "NOT VALID!!";

Variable Variables

Rarely Used and sat here explaining, I can not think of any reason apart from writeing confuseing code to use Variable Variables. Its a way of dynamicly naming your variables, here is an example:

$bar = 10;
$foo = "bar"

There are two ways we can output the value of $bar here. We can either use print $bar, which is quite straightforward, or we can take advantage of the concept of variable variables, and use print $$foo;. That’s right - two dollar signs.

By using $$foo, PHP will look up the contents of $foo, convert it to a string, then look up the variable of the same name, and return its value. In the example above, $foo contains the string “bar”, so PHP will look up the variable named $bar and output its value - in this case, 10.

Do Variables Exist?

You can ask PHP that question using the function isset. This function returns true or false so is commonly used in conditional statements, to ensure that the variable is set and ready to use in the rest of the script. Usage:

echo isset($x);

This will output true if it contains any value other then Null (aka nothing). As mentioned we can use it in conditional statements:

if(isset($y)){
echo "We all know the letter Y has value of $y ";
} else {
exit("Y Variable Not Set!");
}

Constants

Very much like variables but constants don’t change, constants like variables are case sensitive and follow the same naming rules. They do not need prefix such as $ but are set using define , Constants can be accessed within any scope of the script.
It is common practice to name constants in uppercase its contents can be (like variables) anything

define('EMAIL', 'adam@is-hacked.com')
echo EMAIL;

This will print the email as shown!

PHP Basics: Variables and Constants End?

Yes, its mostly simple what you should be taken away from this is how to use constants, how to use isset, how to understand variable variables and high knowledge of variable rules.

The rules apply! If I have missed or wrote something incorrect comment bellow.

Much Thanks
Adam

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis

1 Comment »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS