Feb 24 2010

PHP Variables and Constants

Variables and Constants are different types of identifiers (names) for saving a simple value in PHP. Both are case-sensitive. For the easy identification of Constants and Variables, you should use lowercases for Variables and uppercases for Constants.
1. Variables
They are used more times than constants. As the word already says a Variable is variable and can change its value 100 times and more while the execution of a PHP script. Variables always starts with a dollar sign. Directly after the dollar sign, there are no numbers allowed. But you can use a number after the first letter. Spaces, minus sign and other special chars are also not allowed. To separate a word within a Variable use the underscore sign.

  1. <?php
  2. $2myvariable="sweet cat"; // Wrong
  3. $my2variable="sweet cat"; // Correct
  4. $myvariable2="sweet cat"; // Correct
  5. $my-variable="sweet cat"; // Wrong
  6. $my_variable="sweet cat"; // Correct
  7. ?>

Example on how to use a Variable in PHP:

  1. <?php
  2. $my_variable="sweet cat";
  3. echo $my_variable; // will output: sweet cat
  4. $my_variable=ucwords($my_variable);
  5. echo $my_variable; // will output: Sweet Cat
  6. $my_variable="Sweet Dog";
  7. echo $my_variable; // will output: Sweet Dog
  8. $my_variable=strtolower($my_variable);
  9. echo $my_variable; // will output: sweet dog
  10. ?>

There are 4 times the same Variable name in the same PHP script, but it has 4 different outputs.

2. Constants
A constant should be used for a value which will never be changed in a PHP script. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

  1. <?php
  2. define("MYCONSTANT", "My Constant Value 1"); // Correct
  3. define("MYCONSTANT2", "My Constant Value 2"); // Correct
  4. define("2MYCONSTANT", "My Constant Value 3"); // Wrong
  5. define("MY_CONSTANT", "My Constant Value 4"); // Correct
  6. define("MY-CONSTANT", "My Constant Value 5"); // Wrong
  7. ?>

As the name already says, that value cannot change during the execution of a php script.

Example on how to use a Constant in PHP:

  1. <?php
  2. define("MYBASEURL", "http://www.developerstutorial.com/");
  3. echo MYBASEURL; // will output: http://www.developerstutorial.com/
  4. ?>

Using a Constant will always output the first defined value.

  1. <?php
  2. define("MYBASEURL", "http://www.developerstutorial.com/");
  3. echo MYBASEURL; // will output: http://www.developerstutorial.com/
  4. define("MYBASEURL", "http://www.mydomain.com/");
  5. echo MYBASEURL; // will also output: http://www.developerstutorial.com/
  6. define("MYBASEURL", str_replace("/","",MYBASEURL));
  7. echo MYBASEURL; // will also output: http://www.developerstutorial.com/
  8. ?>

So, here are 3 different definitions, but you will always get the output of the first definition.

16 Responses to “PHP Variables and Constants”

  1. Trula Packer says:

    This is a outstanding article, im lucky I discovered this. Ill be back again down the track to check out other posts that you have on your blog.

  2. This is a good piece of writing, I was wondering if I could use this blog on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.

  3. devadmin says:

    Hello Summer Falkner!
    You can link to this blog at your website and can use summaries of our posts, but please do NOT copy our articles.

  4. Zack Harwell says:

    Good site, where did you come up with the information in this write-up? I’m glad I found it though, ill be checking back soon to see what other articles you have.

  5. Exceptional site, where did you come up with the info in this blog? I’m glad I found it though, ill be checking back soon to see what other articles you have.

  6. Watch Movies says:

    Keep up the good work!

  7. Bruce says:

    This is a outstanding article, im lucky I discovered this. Ill be back again down the track to check out other posts that you have on your blog.

  8. This post was very nicely written, and it also contains a lot of useful facts. I appreciated your professional manner of writing this post. Thanks, you have made it easy for me to understand.

  9. Mia Finello says:

    Awesome article post.Much thanks again. Really Cool.

  10. Rene says:

    Well, I think that clears up a few issues for me personally. How about anybody else?

  11. Margert Beougher says:

    Web Development has always been a passion of mine. I’m always trying to learn more and get better. Thanks.

  12. best php hosting says:

    Awesome detailed post. Bookmarked for future reference.

  13. Windy Heisner says:

    I very much like your website. Very good content. Please continue posting such great cotent.

  14. Lazaro Pen says:

    Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my information gathering. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject?

  15. easter decor says:

    Extremely interesting post thanks for writing it I have added your site to my favorites and will check back :) By the way this is a little off topic but I really like your web page layout.

  16. Mark Ritterbush says:

    I just required to say that I discovered your site via Goolge and I am glad I did. Keep up the very good work and I will be sure to bookmark you for when I have much more free of charge time away from the books. Thanks!

Leave a Reply

Alibi3col theme by Themocracy