Programming in PHP - Variables
Posted 01/30/2015 at 16:01 by manulaiko3.0
Copy and paste from my website: 
In last tutorial we saw a general view of PHP, now we’re going to see one of the most important things in any programming language: Variables.
What is a variable?
Correct definition:
So, now, how we use variables in PHP? Easy.
A PHP variable starts with “$” and is followed by the name of the variable.
Let’s see some examples…
<?php $thisIsAFuckingVariable = "Hello world!"; //<- THAT FUCKING ;!!! //Print the value of the variable echo $thisIsAFuckingVariable; ?> If you go now to your localhost you’ll see “Hello world!”, what has changed?
We’ve used a variable, why to use a variable? Because we can store a lot of things there, let’s see more variables…
<?php $thisIsAFuckingVariable = "Hello world!"; //<- THAT FUCKING ;!!! //Print the value of the variable echo $thisIsAFuckingVariable; //Change variable's value $thisIsAFuckingVariable = "Bye bye world!"; //Print a new line echo "<br />"; //Print new value echo $thisIsAFuckingVariable; ?> Reload the page and you’ll see that the text has changed, but…
echo "<br />"; Why??
Because PHP will print the value of the variable + the new value, that line adds a new line to the document.
Variable types
So, a variable can just contain strings? No.
There are 4 types of values that PHP can handle:
Booleans can contain just 2 values: true and false, why do you want that? Well, think that you want to check if user is logged in your website, a boolean variable would be the solution
Numbers
Numbers are that, numbers, 1,2,3,4,5,6,7,8 <- Those are numbers.
Strings
We’ve already seen strings. Strings are a sucession of characters, the whole post is a string, the whole life is a string (well, actually not, but you got the idea).
Objects
THIS are the most importan type of variables, we will talk about them in the futere.
Default values
Look at this pice of shitcode:
<?php $variable; $variable = 1; echo $variable; //Prints 1 ?> In the first line we declare a variable, but we don’t assign a value to it, we assign it at the next line. So, what would happen if I use the variable before I assign a value? Nothing, to avoid that kind of errors PHP (and any other programming language) assigns default values to the variables:
Okay, now that we know the types of variables we will see how they’re declared.
Booleans
Boolean variables are declared as any other type of variables: variable_name = true|false;
<?php $trueBoolean = true; $falseBoolean = false; ?> Numbers
Numbers are declared like all variables: variable_name = [-2,147,438,648 – 2,147,438,647];
In 32 bits computer the max value of a number variable is 2,147,438,647 because 2³¹ = 2,147,438,647. In 64 bits computers the max value of a number variable is 9,223,372,036,854,775,807 because 2³¹ = 9,223,372,036,854,775,807.
Why 2³¹ and not 2³²? Because the first bit of the number is used to identify if the number is positive (0) or negative (1).
<?php $positiveNumber = 5431; $negativeNumber = -343; //I'm not going to put all possible combinations of number because they're a lot -.- > Strings
Strings variables are declared like this: variable_name = “value”;
The max leght of a strings is the max value of a number (from now till the end of world we will call them integers) variable, why? Because EVERYTHING are bits, 1 and 0, a lot, many, a lot of a lot of bits, so how does PHP knows how long is a string variable? The first 4 bytes contains the length of the string. I know this might be hard/boring but it’s curiosity to know how things work:
00000000 00000000 00000000 00000001 0000001 That amount of bits is the same as a string with 1 character length, it contains the character 1 represented in the ASCII table.
Now let’s see some code.
<?php $string = "Fuck you"; ?> Objects
To declare objects you need to put the new keyword before the object type.
What can contain objects? Well, basically EVERYTHING, other variables, functions, dubstep…
We will see objects in future so don’t worry if you don’t understand this
<?php $object = new Object(); ?> And that’s all, no more shit about variables untill next chaper
See you!

In last tutorial we saw a general view of PHP, now we’re going to see one of the most important things in any programming language: Variables.
What is a variable?
Correct definition:
A variable is a part of the principal memory (aka RAM) that contains a value, that value can be changed anytime when the program (or script in PHP) is running.My definition:
A variable is something where you can store more things.Choose your favourite.
So, now, how we use variables in PHP? Easy.
A PHP variable starts with “$” and is followed by the name of the variable.
Let’s see some examples…
<?php $thisIsAFuckingVariable = "Hello world!"; //<- THAT FUCKING ;!!! //Print the value of the variable echo $thisIsAFuckingVariable; ?> If you go now to your localhost you’ll see “Hello world!”, what has changed?
We’ve used a variable, why to use a variable? Because we can store a lot of things there, let’s see more variables…
<?php $thisIsAFuckingVariable = "Hello world!"; //<- THAT FUCKING ;!!! //Print the value of the variable echo $thisIsAFuckingVariable; //Change variable's value $thisIsAFuckingVariable = "Bye bye world!"; //Print a new line echo "<br />"; //Print new value echo $thisIsAFuckingVariable; ?> Reload the page and you’ll see that the text has changed, but…
echo "<br />"; Why??
Because PHP will print the value of the variable + the new value, that line adds a new line to the document.
Variable types
So, a variable can just contain strings? No.
There are 4 types of values that PHP can handle:
- Booleans
- Numbers
- Strings
- Objects
Booleans can contain just 2 values: true and false, why do you want that? Well, think that you want to check if user is logged in your website, a boolean variable would be the solution
Numbers
Numbers are that, numbers, 1,2,3,4,5,6,7,8 <- Those are numbers.
Strings
We’ve already seen strings. Strings are a sucession of characters, the whole post is a string, the whole life is a string (well, actually not, but you got the idea).
Objects
THIS are the most importan type of variables, we will talk about them in the futere.
Default values
Look at this pice of shitcode:
<?php $variable; $variable = 1; echo $variable; //Prints 1 ?> In the first line we declare a variable, but we don’t assign a value to it, we assign it at the next line. So, what would happen if I use the variable before I assign a value? Nothing, to avoid that kind of errors PHP (and any other programming language) assigns default values to the variables:
- Booleans: false
- Numbers: 0
- Rest of variables: null
Okay, now that we know the types of variables we will see how they’re declared.
Booleans
Boolean variables are declared as any other type of variables: variable_name = true|false;
<?php $trueBoolean = true; $falseBoolean = false; ?> Numbers
Numbers are declared like all variables: variable_name = [-2,147,438,648 – 2,147,438,647];
In 32 bits computer the max value of a number variable is 2,147,438,647 because 2³¹ = 2,147,438,647. In 64 bits computers the max value of a number variable is 9,223,372,036,854,775,807 because 2³¹ = 9,223,372,036,854,775,807.
Why 2³¹ and not 2³²? Because the first bit of the number is used to identify if the number is positive (0) or negative (1).
<?php $positiveNumber = 5431; $negativeNumber = -343; //I'm not going to put all possible combinations of number because they're a lot -.- > Strings
Strings variables are declared like this: variable_name = “value”;
The max leght of a strings is the max value of a number (from now till the end of world we will call them integers) variable, why? Because EVERYTHING are bits, 1 and 0, a lot, many, a lot of a lot of bits, so how does PHP knows how long is a string variable? The first 4 bytes contains the length of the string. I know this might be hard/boring but it’s curiosity to know how things work:
00000000 00000000 00000000 00000001 0000001 That amount of bits is the same as a string with 1 character length, it contains the character 1 represented in the ASCII table.
Now let’s see some code.
<?php $string = "Fuck you"; ?> Objects
To declare objects you need to put the new keyword before the object type.
What can contain objects? Well, basically EVERYTHING, other variables, functions, dubstep…
We will see objects in future so don’t worry if you don’t understand this
<?php $object = new Object(); ?> And that’s all, no more shit about variables untill next chaper
See you!
Total Comments 0






