PHP working with Data types and Variables

PHP working with Data types and Variables

Welcome to another exciting tutorial of PHP. Previously we discussed 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- PHP working with Data types and Variables

Variables are really fun and easy to learn. Whole concept behind this is to save some data temporary and use it in time of need. In PHP, 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

PHP working with Data types and VariablesOutput must be this web page

PHP working with Data types and VariablesWhile working with variables, we must know that 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.

Data Types- PHP working with Data types and Variables

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. Fun part is that unlike many other languages, you don’t have to mention the data type when describing a variable.

Integers

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.

Strings

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 “”. Thing to remember is that there can also be integers inside a String as well, but since it is surrounded by a double quotes we will handle it as a string and not as an integer. For example $Var = “25”. In this case, variable is holding a string and not an integer.

Floats

We know integers cover a lot of our data that is to be used in 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. Example is given here. $myFloat = 2.5;

Arrays

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.

Booleans

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

PHP working with Data types and VariablesAs we discussed 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:

PHP working with Data types and VariablesUpdating 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 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 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 One: HOW TO RUN YOUR FIRST PHP SCRIPT ON WINDOWS
Part Two: BASICS OF HTML5 ON THE WAY TO PHP LEARNING
Part Four: IF ELSE STATEMENTS AND ARITHMETIC OPERATIONS IN PHP
Part Five: TUTORIAL ON TYPES OF LOOPS IN PHP – PHP LOOP TYPES
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Part Ten: FORMS IN PHP – PHP FORM EXAMPLE WITH DATABASE
Like this post:

Leave a Reply

Your email address will not be published. Required fields are marked *