Variables
In previous tutorials (found here
https://ininjas.com/forum/index.php?topic=4146.0, and here
https://ininjas.com/forum/index.php?topic=4147.0) I explained how to set up PHP and test it. However, before we move on, you must understand some key points and key terms before fully immersing in PHP. So that is the purpose of this section. (Note: I too wanted to skip this section when I first started but unfortunately the later sections will make no sense without this knowledge.
Variables:
"A variable is a keyword or phrase that acts as an identifier for a value stored in a system’s memory. This is useful because it allows us to write programs that will perform a set of actions on a variable value, which means you can change the output of the program simply by changing the variable, rather than changing the program itself."
Or, a variable is essentially a variable could be A = foo, or B = 5. In PHP, you can store variables in one of the following ways:
String: Words or letters
Integer: A number that does not use decimals (14)
Float: A number that uses decimals (14.0)
Boolean: Answers either TRUE or FALSE
Array: An indexed collection of data
Object: A collection of data and methods
(An important thing to remember about PHP is that it determines what you meant by using "guessing" so to speak. It is not definite like C)
The following sections will define different variables:
Strings
--------
A string is any type of data input with single (') or double (") quotes on both sides. An example of this is:
$string = 'It\'s cold outside today!';
Note: if you notice the \ is right before the apostrophe. This is so when you run the script PHP does not get confused with the because of the quotes before and after it.
Integers
----------
Integers are just like regular numbers that do not end in decimals. To use an integer in a variable would look something like this:
$foo = 27; (no quotes around the number always means integer.
Floats
-------
Floating point numbers (also known as floats) are numbers with decimals at the end. This means numbers like 15.5555, 1999.4, and 3.14.
note: floats can be tricky as they never produce a finite amount of numbers. For instance, 2/3 would end up being 0.6666666...
Boolean values
----------------
These are the simplest type of data as they only return as TRUE or FALSE, which make them very easy for determining if a condition exists by using the
i statement.
if($condition===true) {
echo 'The condition is true!'; }
else {
echo 'The condition is false!'; }