PDA

View Full Version : Basic PHP Tutorial - 2



ctt1010
10-31-2014, 09:25 PM
In this tutorial, we will be covering variables.

What is a variable?

A variable is used to store data so it can be used at any time. It can also be overwritten at any time. Variables in PHP are easier than most other languages as you do not need to define the type. All you need to do is put a '$' before the word and an equals with the data afterwards.

Example :

If you wanted to store the word "Hello" in a variable, it would look like this :



<?php
$var="Hello";
?>

With this new variable we can now print it.


<?php
echo $var;
?>


Maths

We can even do maths with variables. For example :



<?php
$x = 5;
$y = 10;
$z = $x+$y;
echo $z;
?>


This would echo 15 as 5 + 10 is 15.