VARIABLES AND CONSTANTS
VARIABLES AND CONSTANTS
As in many other programming language, also in PHP we deal with variables
and constants. What are they and what will we use them for when writing scripts?
As you’ve already done the first exercise with writing first line of code, you are ready to learn further.
VARIABLES
Variable in PHP is a container that stores a certain value. It doesn’t matter if it’s
boolean, integer, rational or text. An interesting fact in PHP is that there is no need to declare a variable or its type, which may be required in other popular languages (like C# or Java). This means that you can use a certain variable even if you haven’t yet created it. In addition, PHP itself selects the appropriate data type for the variable.
As I said, boolean values, floating point numbers, integers, strings are just examples of the data types. The interpreter underneath chooses the right type for us, based on the assigned value. Fortunately, you don’t have to worry about that. Even if the variable originally had one type, it can change to a different one later. Nothing of this is possible in C # or JAVA languages, where the variable must be declared before using it, and once the type is declared, it will not change (you need to create a new variable). Later, you will see in practice what I mean.
CONSTANTS
Constant is also a data container, just like a variable. There is one important difference between them. As the name implies, its value cannot be changed.
It’s permanent. We assign a value when defining it, after which this value cannot be changed.
OK, so how can I define a constant or variable?
DEFINING CONSTANTS AND VARIABLES
What does the initialization of constants and variables look like?
See an example:
<?php
define("TELEPHONE","5557708");
$telephone = "5557708";
?>
<html>
<head>
<title>Variables and constants</title>
</head>
<body>
<?php
echo TELEPHONE;
echo $telephone;
?>
</body>
</html>
Looking at the above code, we see two assignments at the very beginning. The first one is the defining a constant. It’s define(“NAME”, “VALUE”); where NAME is any combination of letters, numbers and underscores, and VALUE is a value that can be primarily numbers, text or logical values (true, false). In addition, the NAME must start with letter or underscore. Because constants in PHP are case sensitive by default, the NAME and Name constants are are two different containers.
The next line is the defining a variable. Variable names follow the same rules
as constants with the only difference that we must precede the variable name with the $ sign.
To assign values to variables, the assignment operator “=” is used. It’s task is to assign the value on the right of the operator to a variable on the left side. You can find more about the “=” operator and other operators in the next lesson.
You’ve created a variable, constant, and assigned them values.
What’s next?
Of course you can display them on the screen with the echo command as shown in the example.
However, this is not the only option.
A constant or variable is treated by PHP as a number, string or logical value – depending on the value assigned to it. If so, assigning the variable $number = 5; allows us to use it like a traditional number. It will be possible to add, subtract, multiply, divide, etc. You will learn about operators in the next lesson.
CONSTANT METHOD
It’s funny how programming is so complex and so simple at once.
You know that you can display variable’s value – it’s obvious. You can also display constant’s value.
But did you know that you can keep your constant’s name inside of variable and display value of such constant with use of it’s name kept inside this variable?
Check out following example:
<?php
define("TELEPHONE","5557708");
$name = "TELEPHONE";
?>
<html>
<head>
<title>Variables and constants</title>
</head>
<body>
<?php
echo constant($name);
?>
</body>
</html>
With use of constant me
Comments
Post a Comment