| |
|
|
| You
are currently reading |
 |
|
|
|
|
| The
article requested: |
 |
|
|
|
The concept of a variable Before entering in this topic I would like to give you the primary concepts of variables. Variables are names that we create from our minds in order to label them with data. for instance, we can say this:
Person_name = "Andres" Person_Lastname = "Robalino";
This is very similiar with math, we assign X values, Y values, etc.. However, in computer programming we create variables in the same way. if you see the example above, we can conclude that we have 2 variables and each one of them has one value, if I say this:
Person = Person_name + Person_lastname Print(Person)
You also conclude that Person = "AndresRobalino" because I sum both variables (Person_name & Person_lastname) into one and after that I print the Person variable that holds the data of the sum of both variables defined above (Person_name and Person_lastname) meaning that the ouput will be AndresRobalino
PHP variables Every computer programming language needs to have variables, they exist since the first computer programming language appeared I guess. When you program, you need to store values somewhere so you can work properly, for example if you are sending data from a form to another page, in these cases you need to specify variable names so you can extract them with PHP. I'll start now defining what a variable is.
Variables exists on computer programs so you can use them for storing any data type. In PHP you use variables (vars) for storing and extracting information and there also exists various types of data. As you can see, variables are data holders, therefore, they hold data of any type. In PHP you create your variables and you can assign (store) values in them, you can also alter them and delete them as well.
We've seen now what variables can do for us, now 'll show you how to create, use, and alter them in PHP.
The syntax of variables In this language "PHP" every variable starts with a dollar sign '$' followed by the name of the variable you are using or would like to create. The name of the variable must begin with an underscore "_" or any letter or any ASCII character from 127 through 255 and never with numbers, you can use letters or numbers or ASCII characters right after the first character of the variable's name, you can't use spaces, if you want, you can use underscores "_" instead of spaces so you can pretty much understand the names of your variables. The variables in php are case-sensitive meaning that if you create two variables with the same name but the difference in them is that one is uppercase and the other lowercase for example "$variable1 and $Variable1", actually they are not the same and they are 2 different variables. Here's a tip: Always create variables that are understandable and all of them in lowercase, if you want, you can comment your variables so you and other people can understand why the variable is created and what is the purpose of the variable. Here you can see some examples of creating variables in PHP:
1 <?php 2 $firstname = "Andres"; 3 $lastname = "Robalino"; 4 5 echo "My name is $firstname and my last name is $lastname"; 6 ?>
Read this simple code, in line 2 and 3 I'm creating two variables and assigning at the same time some values, line 2: I create the variable $firstname and right after that I assign the value "Andres" to it, therefore, the variable $firstname now holds the word "Andres", We'll see about assigning values and data types in the next page. Line 3 did the same thing as line 2. In line 5 I make use of the echo function that was explained in "Introduction to PHP" article. Well, for a quick summary for echo, this function sends the data to the screen, therefore, it will print what is inside the double quotes, that is, "My name is $firstname and my last name is $lastname" and if you notice inside those double quotes, there are also the variable's name in there, therefore, PHP will recognize them and PHP will replace them by their respective values stored in those variables. The result of that PHP script is:
My firstname is Andres and my lastname is Robalino
|
[1][2][3][Data types and Assigning values]
|
|