You are viewing our Forum Archives. To view or take place in current topics click here.
Variables in PHP
Posted:
Variables in PHPPosted:
Status: Offline
Joined: Apr 10, 200915Year Member
Posts: 7,581
Reputation Power: 1576
Status: Offline
Joined: Apr 10, 200915Year Member
Posts: 7,581
Reputation Power: 1576
Hello. Thought I would make a quick tutorial in PHP. This tutorial is about variables.
Variables are basically named and given values by the coder. This term is used widely in the world of coding, mostly in programming. Variables, are used widely in math as well, but varies from in coding. For example in math;
2x = 12
x = 6
In this equation, x is the variable; and that variable equals 6. In coding however, it is particularly different. A variable is given a name, and then a value. I will be showing you how to do this in PHP.
First let's start off with the basic PHP syntax.
Now to declare a variable you need to include a dollar sign. Include this in one of the first lines of code, as it will be used widely in the next code. If you do this after the other code, there will most likely be an error, as the compiler reads code top > bottom.
The dollar signs tells the compiler that we will be declaring a variable. Directly after the dollar sign, you include the name of the variable.
Next include the = sign. This is the conjunction that will give the variable a certain value.
Next you want to give the variable a certain value. For example I will give this variable a value of 10. Remember to end the statement with a semi-colon.
If you compile the code, nothing will appear. This is because we have not echoed the variable onto the screen yet. We need to echo it out, by using a standard "echo" code and then type in the variable name, like this.
Now, we have echoed the variable onto the screen. Running the code you will get the number "10"
Now you can proceed to use variables in different ways. For example, you can declare two variables and add them onto the screen.
Running the code, you will get the number 6, as we told PHP to add the two variables. This is useful for many other types of PHP code that you will learn in the future, if I do continue these tutorials. Thanks, and I hope you enjoyed this tutorial.
Variables are basically named and given values by the coder. This term is used widely in the world of coding, mostly in programming. Variables, are used widely in math as well, but varies from in coding. For example in math;
2x = 12
x = 6
In this equation, x is the variable; and that variable equals 6. In coding however, it is particularly different. A variable is given a name, and then a value. I will be showing you how to do this in PHP.
First let's start off with the basic PHP syntax.
//If you look at the code and delete the spaces it is a mirror image; <?php?> This is a good way to remember.
<?php
?>
Now to declare a variable you need to include a dollar sign. Include this in one of the first lines of code, as it will be used widely in the next code. If you do this after the other code, there will most likely be an error, as the compiler reads code top > bottom.
The dollar signs tells the compiler that we will be declaring a variable. Directly after the dollar sign, you include the name of the variable.
$number
Next include the = sign. This is the conjunction that will give the variable a certain value.
$number =
Next you want to give the variable a certain value. For example I will give this variable a value of 10. Remember to end the statement with a semi-colon.
<?php
$number = 10;
?>
If you compile the code, nothing will appear. This is because we have not echoed the variable onto the screen yet. We need to echo it out, by using a standard "echo" code and then type in the variable name, like this.
<?php
$number = 10;
echo $number;
?>
Now, we have echoed the variable onto the screen. Running the code you will get the number "10"
Now you can proceed to use variables in different ways. For example, you can declare two variables and add them onto the screen.
<?php
$number1 = 2;
$number2 = 4;
echo $number1 + $number2;
?>
Running the code, you will get the number 6, as we told PHP to add the two variables. This is useful for many other types of PHP code that you will learn in the future, if I do continue these tutorials. Thanks, and I hope you enjoyed this tutorial.
#2. Posted:
Status: Offline
Joined: Jan 11, 201212Year Member
Posts: 14
Reputation Power: 0
The = sign is actually called the assignment operator in terms of programming.
Also PHP is not compiled, PHP is interpreted by the web server.
Also PHP is not compiled, PHP is interpreted by the web server.
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Jul 07, 200915Year Member
Posts: 2,596
Reputation Power: 205
Status: Offline
Joined: Jul 07, 200915Year Member
Posts: 2,596
Reputation Power: 205
You should mention that it's not just for numbers and can be used like this.
Which would produce - "Hello Tea welcome to Thetechgame"
Also that you can change a variable such as
Which produces "12". As $number is 5 and $number1 is 7. When added together is 12 however you can make the variable equal itself and more or just its self to change the value of the variable.
<?php
$name = "Tea";
$site = "Thetechgame";
echo "Hello $name welcome to $site";
?>
Which would produce - "Hello Tea welcome to Thetechgame"
Also that you can change a variable such as
<?php
$number = "5";
$number1 = "7";
$number = $number + $number1;
echo "$number";
?>
Which produces "12". As $number is 5 and $number1 is 7. When added together is 12 however you can make the variable equal itself and more or just its self to change the value of the variable.
- 1useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Tea wrote You should mention that it's not just for numbers and can be used like this.
<?php
$name = "Tea";
$site = "Thetechgame";
echo "Hello $name welcome to $site";
?>
Which would produce - "Hello Tea welcome to Thetechgame"
Also that you can change a variable such as
<?php
$number = "5";
$number1 = "7";
$number = $number + $number1;
echo "$number";
?>
Which produces "12". As $number is 5 and $number1 is 7. When added together is 12 however you can make the variable equal itself and more or just its self to change the value of the variable.
Ah but for the last you could also do a <? $number += $number1 ?> type of deal if both variables are of the integer type. Meaning if you remove the quotes during the assignment they will be interpreted as integers and added together!
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.