Getting Started with PHP – Part 4 (PHP Variables)

Guest Posts Paolo Nikko Nuñal

PHP is a loose language and you can clearly see it through their declaration of variables and methods. In this tutorial we are going to examine how variables are declared and how you can use them on your program.

Variables act like containers for storing information. These variables change on whatever you want them to be. They can either be stored as an object, integer, and float just to name a few.

<?php
   $x = 3;
   $y = 23;
?>


From that example we have created two variables, x and y that have a value of 3 and 23 respectively. When creating variables always start with a dollar sign "$" then followed by the variable name. Here is another example.

<?php
   $x = 3;
   $y = 2.3;
   $z = new Person();
?>


From the second example we have three variables, the first one stored an integer, the second one stored a float value and the last one stored an object. Variables in PHP is much like Algebra and you can express them with letters and do mathematical equations.

x=1
y=2
z=x+y

Here are some important things you should know about variables.


  • A variable must start with a dollar sign, followed by the name of the variable.

  • A variable must start with an underscore or a letter.

  • A variable cannot start with a number.

  • A variable can only contain alpha-numeric characters.