All we’ve done so far is outputted some text using “echo.” To stop you dying from boredom, we’ll have a play with some variables. A variable is simply a box that holds a value or values. It is how PHP stores data and passes them between functions.
Without variables PHP and dynamic websites would be rendered useless! Dynamic web content that changes as a result of an input relies on data being stored and transferred around or between pages. Variables are the responsible for storing and moving data in this manner.
For you to properly understand variables, I think you should play with them some. But first, let’s look at the main things that we can do to variables:
- Set a variable - assign them a value.
- Access a variable - read the value and use it.
- Re-set a set variable - re-assign them a value.
In PHP variables are defined by a $. Here, a variable is set, accessed, re-set and used again. A variable’s value can be changed at any time.
$phrase = “Hello!";
echo “$phrase How are you?";
echo “$phrase It was nice seeing you again.";
I began by making a variable called “phrase.” As variables begin with a $, the variable should look like this: $phrase.
This is what happens when the above code is executed:
- Indicates the start of a PHP code block.
- The variable $phrase is made. It is then set with the string “Hello!”
- Outputs a static string and accesses the $phrase variable and outputs that also.
- Inserts a HTML paragraph tag to make some white space.
- The variable $phrase is re-set and given the new value “Goodbye!".
- Outputs a static string and accesses the new $phrase variable and outputs that also.
- Indicated the end of the PHP code block.
Wow! You just witnessed your first PHP variable in action! All we did was set and access the variable a few times, however, we can do so much more! The magic of PHP lets you set a variable in one place. For example, a user could complete a form and the user inputs could be held in a variable for use later.
Set the variable with =
Use quotations for strings (not needed for numbers)
end instructions with ;
Just Like Naming a Baby
You can call your new-born variable anything you wish, as long as it follows these rules:
- A variable cannot be a system reserved variable, such as echo
- A variable must start with a letter (you wouldn’t name your child with numbers…would you?)
- A variable must be comprised of alphanumeric characters - letters, numbers and underscores (okay, perhaps this is not quite that same as naming a child);
Listen up! Names of variables are case-sensitive, therefore, $phrase is not the same as $Phrase. Also, give your variables meaningful names! As with comments this will help you read your code at a later date.
Up until now we have set variables as simple “strings.” However, among other things, they can store numbers, arrays and boolean operations.
All languages have different variable types. PHP has loads of data types, some of which include numeric, boolean, string and arrays. For example:
Boolean - The most simple type of all! A Boolean variable can store one of two values either true or false.
Integer - A normal, whole number. Such as, 1, or 99.
Floating-point - A decimal number, such as 1.5 or 99.241.
String: A string is made up of characters, “Hello World!” for example. Strings can be between double quotes " or single ones '. Strings wrapped with double quotes are parsed for variable names. If a variable is discovered the value of the variable is shown instead.
Today we have looked at data types and set variables and then and accessed them in the same PHP script. Although this lets you get a taste of variables it isn’t useful because, instead, we could have just written the values of the variables manually, with the rest of the string.
Next time we’ll make some juicy HTML forms to acquire user input, convert the user input to variable, then perform PHP magic with the data collected. See you next time!
This post is part of the series: A Guide to Linux Programming with PHP
In this series of articles I’ll be teaching you the basics of using PHP, and showing you why I think it’s the best language for web development. There are no pre-requisites for your level of knowledge, other than that you can understand basic HTML and are willing to learn.
- A Guide to Linux Programming with PHP
- A Guide to Linux Programming with PHP - Commenting and Executing Code
- A Guide to Linux Programming with PHP - Variables & Data Types
- A Guide to Linux Programming with PHP - HTML Forms and PHP
- A Guide to Linux Programming with PHP - Arrays
- A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays