Working with Data types and Variables in PHP
Welcome to another exciting tutorial of PHP. Previously we discussed the Basics of HTML5 on the way to PHP Learning. Now it’s time to jump to something advance. As the title suggests, we will be discussing today the Data types and Variables in PHP. Both of the topics are of real importance while learning any programing language.
Variables are really fun and easy to learn. The whole concept behind this is to save some data temporarily and use it in time of need. In PHP, the syntax of a variable starts with a “$”, followed by the variable name. After that, we assign a value to it using this syntax. A variable name can be anything you want just it cannot start with a digit or a special character. Also, inserting a space in a variable name is not allowed but you can add a ‘_’ instead. Example
$Var = 25; (Don’t forget to write it inside a PHP tag)
So in this particular example, we created a variable name “Var” and it is holding 25 value for us. So, In the future if we need to use this value, we can simply write the variable name and it will do the work.
Try “echo $Var” and see what is being printed. Remember the syntax
Output must be this web page
While working with variables, we must know what kind of values we can store in a variable. In the previous example, we stored a number but there are many more options we have. Let’s discuss each one in details.
In PHP we have options of Integers, Strings, Floats, Booleans and Arrays as data types. There are some others as well but we will stick to all these in beginning. The fun part is that unlike many other languages, you don’t have to mention the data type when describing a variable.
Integers are used to store the numeric data. All the negative and positive numbers are included in this list. We use this kind of data in all kind of calculations and whenever we need to store or show some numeric values. We have already seen an example of a variable holding a numeric value.
A string is a list of characters combined together. We always make a string in PHP by putting some characters (Text) inside the double quotes “”. The thing to remember is that there can also be integers inside a String as well, but since it is surrounded by a double quote we will handle it as a string and not as an integer. For example $Var = “25”. In this case, the variable is holding a string and not an integer.
We know integers cover a lot of our data that is to be used on a website. But they cannot handle the values in points. For that we use the Floats. They are used to store, modify and calculate the values in points. An example is given here. $myFloat = 2.5;
Arrays are really important in any programing language. We will be doing a whole tutorial on theses. Right now, all you have to know is that Arrays are the list of different data types. And you can access any entry using an index.
These are the special kind of variables that can only store true values of “true” or “false”. Booleans are really handy while working with the “if-else statements” (Hold on for the Next tutorial to know about these statements).
Since we are now familiar with different data types, let’s use them in a real code example. In a PHP tag write all this code.
$intVar = 100;
$floatVar = 8.5;
$str = “Everything is great”;
echo “Integer saved value = “.$intVar;
echo “<br>Float value = “.$floatVar;
echo “<br>String Value = “.$str;
When we echo different data types with strings we combine them with a Dot “.” As you can see from the example above. Merging this with HTML will look like
As we discussed the earlier position of PHP code doesn’t matter. You see, I have defined all of the variables in a separate PHP tag which is on the top of all the code. That is completely fine, but what matters is that I put the second part of the code inside the body tag. Because this is where I want to echo all of my outputs. Hence the output will look like:
Updating Value in a Variable
Different techniques are used to update a value in a variable.
- To remove the first value in a variable and replace it with a completely new value. Just assign it as $Variable=newValue; In this way, the previous value will be lost.
- But if you do not want to completely replace the value but to update it, so that’s also possible. Suppose, there is a $test variable with a value of 5. And now we want to add 5 more to it so we will user. $test = $test +5; In this way, 5 will be added to the previous value and new value (10) will be stored in that variable.
- Increment of 1 is really important while working with the So we can use a syntax of $test= $test +1; or $test++; to increment the value with 1.
Same goes with the decrement, jus use $test–; or $test=$test-1;
So this was another tutorial in the path of your PHP learning. We really appreciate your opinion so please comment. And get ready for the upcoming tutorial which is going to be all about If Else Statements and Arithmetic operations in PHP. It is going to be an exciting experience for you. Good Luck in your journey.
Also see more PHP Tutorials:
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Main Image Source : Pixabay