<-- Back to PHP: Lesson 1 "Hello World"
--> Forward to PHP: Lesson 3 Forms
Other than just printing out "Hello World", PHP can be used for other things!
In computer lingo we call "constants" things that never change, while "variables" can take on any value. The number "5" is a constant, so is a character string like "Bob". We might want to manipulate these constants in some way. We can assign them to variables. All variables in PHP start with a "$". Using our "Hello World" script we can make one small change to show the use of a variable.
<?php
$some_txt = "Hello World";
echo $some_txt;
?>
This program will also simply output
Hello World
but what we've done here is use a variable to store the value and then output that variable, instead of the simpler character string. You can easily see how this variable could instead be based on reading some file data, or making some calculation. Here in this small program we can see exactly what it's going to be but that's not generally the case.
Variables and assignment are not just for constant character strings. They can be used for all sorts of things, such as a calculation.
<?php
$my_result = 9*875/12 - 21;
echo $my_result;
?>
It's not as easy in this last example to quickly see what the output would be.
As you can see, PHP is what is called "loosely typed", this means that we do not need to specify whether something is a character string or a number before we use it in a variable expression. PHP figures this all out in the background for us.
<-- Back to PHP: Lesson 1 "Hello World"
--> Forward to PHP: Lesson 3 Forms
--> Forward to PHP: Lesson 3 Forms
Other than just printing out "Hello World", PHP can be used for other things!
In computer lingo we call "constants" things that never change, while "variables" can take on any value. The number "5" is a constant, so is a character string like "Bob". We might want to manipulate these constants in some way. We can assign them to variables. All variables in PHP start with a "$". Using our "Hello World" script we can make one small change to show the use of a variable.
<?php
$some_txt = "Hello World";
echo $some_txt;
?>
This program will also simply output
Hello World
but what we've done here is use a variable to store the value and then output that variable, instead of the simpler character string. You can easily see how this variable could instead be based on reading some file data, or making some calculation. Here in this small program we can see exactly what it's going to be but that's not generally the case.
Variables and assignment are not just for constant character strings. They can be used for all sorts of things, such as a calculation.
<?php
$my_result = 9*875/12 - 21;
echo $my_result;
?>
It's not as easy in this last example to quickly see what the output would be.
As you can see, PHP is what is called "loosely typed", this means that we do not need to specify whether something is a character string or a number before we use it in a variable expression. PHP figures this all out in the background for us.
<-- Back to PHP: Lesson 1 "Hello World"
--> Forward to PHP: Lesson 3 Forms