Home Logo
Show Full Menu
Hide Full Menu
 

Variables

Created: 30/09/10 Views: 861 myFave Image


In this tutorial we will learn PHP variables
PHP variables are very basic and easy just like PHP print and echo commands. PHP variables are exactly what you probably learnt in maths at school, they simply hold data under a specific name
So why do we have these in PHP? Well, in PHP you do tend to write a lot of long scripts that you will use multiple times, and if you can stick the script into a single variable, then all you have to run is that variables over and over again instead of re-writing the actual script.

To write a variable is pretty simple
  • For a numeric value
  • $theName = 5

  • For a numeric value
  • $theName = "this is long script!"
So all it takes is a dollar sign, $, then a name given for the variable, and equals sign and then the value of the variable and finally a semi-collon.

Here are some simple examples of variables
    Script
  • <?php
  • $someTxt = "Hey, how are you?";
  • print $someText;
  • ?>

    Result
  • Hey, how are you?

    Script
  • <?php
  • $aaa = "How many do you want?";
  • $bbb = 8;
  • print $aaa;
  • print "<br />";
  • print $bbb;
  • ?>

    Result
  • How many do you want?
  • 8

    Script
  • <?php
  • $aaa = "Hey ";
  • $bbb = "Dude";
  • $ccc = "Babe";
  • print $aaa.$bbb;
  • print "<br />";
  • print $aaa.$ccc;
  • ?>

    Result
  • Hey Dude
  • Hey Babe



next tutorial: If Statements

There are no comments