Variables are the bread and butter of any programming language.
A variable is a small portion of memory that stores a particular piece of information. Imagine for a second that you have a cardboard box and a pen in your hand. You now decide to store your favorite CD in the box. To better remember what is in it later, you write ‘favealbum’ on the front of the box. You now know that if you look at the box and see ‘favealbum,’ you will remember that you have your slightly embarrassing Leprechaun
Hits Volume 1 CD in there.
This is exactly how variables work, except that a variable stores information. In your program, you can create a variable to store some information and then reference what is in that variable by the name of the variable itself.
This is best explained with an example.
Create a new file called simplevariable.php and add the following code:
when you see it.
One important note at this point, particularly for those of you with prior experience in other programming languages, is that you do not need to indicate what type a variable will be in PHP. In other languages (such as C/C++), you would need to indicate that a variable containing a number is an integer variable. PHP is different and figures out what the type is when you use the variable.
With the variable set, it is then used in the next line in which the echo command is used to first display Number of monkeys: and then the value of $monkeys is displayed next to it. When you run the script, you should see the following output:
Number of monkeys: 10
This echo line uses a technique called concatenation. This bizarre-sounding act provides a simple method of stringing together bits of text (called strings) with the contents of variables. To do this, you use the . symbol (called the concatenation operator) to glue together whatever is on the left and right sides of the dot, or period.
In the preceding code, the part on the left side is the text Number of monkeys: and the part on the right is the contents of the $monkeys variable. Concatenation is an important but often slightly confusing topic. It does take a little while to get used to, particularly when using more complex examples. As an example of this, change the echo line above to the following line:
In this line, you used the concatenation operator twice to glue an additional part onto the line. If you read the line from left to right, you can see how You have is then glued to the value of $monkeys (10), which is then glued to monkeys.
A variable is a small portion of memory that stores a particular piece of information. Imagine for a second that you have a cardboard box and a pen in your hand. You now decide to store your favorite CD in the box. To better remember what is in it later, you write ‘favealbum’ on the front of the box. You now know that if you look at the box and see ‘favealbum,’ you will remember that you have your slightly embarrassing Leprechaun
Hits Volume 1 CD in there.
This is exactly how variables work, except that a variable stores information. In your program, you can create a variable to store some information and then reference what is in that variable by the name of the variable itself.
This is best explained with an example.
Create a new file called simplevariable.php and add the following code:
<?php
$monkeys = 10;
echo "Number of monkeys: " . $monkeys;
?>
All variables in PHP begin with a dollar sign ($). In the preceding example, you first create a variable called $monkeys and then used the = sign to set this to the value 10. This is the common way to create a variable in PHP; you simply pick a variable name out of thin air (in this example, my love of monkeys has been indulged) and then set the variable to store a particular value. With $monkeys created, you can now read the code and mentally replace $monkeys for the value 10$monkeys = 10;
echo "Number of monkeys: " . $monkeys;
?>
when you see it.
One important note at this point, particularly for those of you with prior experience in other programming languages, is that you do not need to indicate what type a variable will be in PHP. In other languages (such as C/C++), you would need to indicate that a variable containing a number is an integer variable. PHP is different and figures out what the type is when you use the variable.
With the variable set, it is then used in the next line in which the echo command is used to first display Number of monkeys: and then the value of $monkeys is displayed next to it. When you run the script, you should see the following output:
Number of monkeys: 10
This echo line uses a technique called concatenation. This bizarre-sounding act provides a simple method of stringing together bits of text (called strings) with the contents of variables. To do this, you use the . symbol (called the concatenation operator) to glue together whatever is on the left and right sides of the dot, or period.
In the preceding code, the part on the left side is the text Number of monkeys: and the part on the right is the contents of the $monkeys variable. Concatenation is an important but often slightly confusing topic. It does take a little while to get used to, particularly when using more complex examples. As an example of this, change the echo line above to the following line:
echo "You have " . $monkeys . " monkeys!";
In this line, you used the concatenation operator twice to glue an additional part onto the line. If you read the line from left to right, you can see how You have is then glued to the value of $monkeys (10), which is then glued to monkeys.
0 comments:
Post a Comment
We really appreciate if you comment after reading our posts. Thank You