#1 PHP Variables Creation and USE

In this first class of the series of the course to learn php I will try to explain the operation and usefulness of the use of variables in programming and more specifically in php.

Content

  • What is a variable in php?
  • How do you create a variable in php?
  • How to use variables in php
  • Advanced concepts of variables in php
  • Summary of PHP variables

What is a variable in php?

Variables, in programming, are the most basic concept. Most of our code will revolve around the use of variables, since these have a very simple objective: to store information to be able to recover it later with a graphical reference. A graphic reference? Yes, I mean that a word or a series of characters would identify this stored data.

An example: imagine that you have to calculate VAT on the price of a product and you have that price stored in a variable called $price. in that variable $price you would have stored the price number, and thanks to this you could create another variable that would store the price with VAT $priceWithIva. Are you taking it? I hope so.

The variables in programming They are stored in the memory of the computer where the code is executed, thus, as we create more and more variables in our code, we occupy more and more. As a general rule a simple variable like the one in the example above will only take up a few bytes of memory.

How do you create a variable in php?

knowing what is a variable and how it works, let’s see how to create variables in php in a basic way. The first thing to remember is that our PHP code should always be written between #php and #/php. So, once inside the php tags, create a variable in php It’s as simple as typing the following:

<?php
$variable = 1;
?>

In the example script create a variable call $variable. This variable stores a single piece of data, which is the integer 1. As you can see, I end the line with a “;”, if you are starting with programming, you should know that this must be done in all our lines of code, but more specifically and avoiding possible confusion this will be done in all code statements.

He variable name in phpthat is, the text that precedes the symbol $ cannot start with numbers. It must also be remembered that the name of the variables cannot contain accents or strange characters.

Now I am going to create two variables to store the first and last names of a person, then I am going to join them and store the value of the variables $name and $lastname in a new variable $full_name:

As you will see, the first two variables follow the same logic as when storing the number in the previous example, but this time, since I am working with a text, I must put it in single or double quotes.

When I put the two variables together, since they’re to the right of equals I’m actually telling the apache server to use the value stored in them. With the symbol “.” I am gathering those values ​​(in programming the word concatenate is used) and saving them in a new variable $full_name that will contain “Pepe Leal”.

<?php
$nombre = 'Pepe';
$apellidos = 'Leal';
$nombre_completo = $nombre . $apellidos;
?>

How to use variables in php

Once a variable will be available for use, that is, we can reference it by its name and use its value. In the following example I am going to print on the screen (in the browser) the value of $variable.

If we enter the browser in the url of our script, for example “localhost/myscript.php”, we can see how a 1. The echo function allows us to display data on the screen and when writing right after $variable we are telling it to display the value that the variable contains.

<?php
$variable = 1;
echo $variable;
?>

Advanced concepts of variables in php

If you are one of those who have arrived here knowing other programming languages, there are several php variable concepts more advanced that will surely be useful to you:

  1. When creating variables in php we do not need to indicate its type, that is, if it is integer, decimal, text, char, etc.
  2. The type of variables in php can change throughout their life in the code. Example: you can assign a text to a variable that stored a number and vice versa.
  3. Variables in php can be declared at any time, that is, you don’t need to have declared them anywhere in the script in order to use them later.
  4. The variables in php have value and referenceto. To use a variable reference we will use the ampersan before the variable name. Example: &$variable.

Summary of PHP variables

  1. Variables are used to store information in memory for later use.
  2. Variables are identified with a graphic reference, their name.
  3. Variables in php must always start with the symbol $
  4. The name of a variable, that is, after the $ cannot start with numbers
  5. The name of a variable cannot contain accents or strange characters
  6. The variables in php do not need to declare their type and they can change types at any time.
  7. php variables they do not need to be declared in a specific place in the code for later use. We can create variables anywhere in our code.
  8. If you try to “concatenate” or display a variable that you have not defined you will get a Notice: undefined index error

Leave a Reply