PHP Variables

Register here to save your progress

? ?

Intro

The variables are used as containers, like boxes, where we can keep values.

Their names should always have the $ symbol at the beginning, followed by a letter or "_" (underscore). After that you can either use letters, numbers or underscore, like "$first_name" or "$_address1". Beside underscore no other special characters (like "+, -, *, % ...") are allowed for the name.

The names are case sensitive (PHP makes the difference between uppercase and lowercase letters of the name), so PHP will see "$movieName" as one variable and "$moviename" as another variable. 

<?php

//examples of variables
$first_name = 'Edward';

$description = 'This is a PHP course';

$month_number = 11;


?>

Which variable names are correct?

As their names suggests ("variables") we can change the content of a variable as many times as we want, the content is variable. We can also assign the value from one variable to another one.

<?

$fruit = 'apple';

$fruit = 'mango';

$tropical_fruit = $fruit;

//it will display the value 'mango'
echo $tropical_fruit;

The semicolor symbol ; is required to signal the end of an line instruction.

We use echo when we want to output a value. 

How many times we can change the value of a variable?