In this tutorial I am going to explain PHP Constant, Variable, Data Type Operator with simple examples.
Constant, Variable, Data Type Operator:
Constant: Constant refers to fixed value that does not change the value during the execution of program. Constant are defined using PHP’s define () function which accept two arguments. The $ prefixed is not required for constant.
Here is example of defining and using constant-
<?php
//define constant
define(‘PI’,3.14);
$r=5;
//use constant
$area=PI*$r*$r;
echo “Area of circle=”.$area;
?>
Output: 78.5
Variable: Variable is a container that is use to store numeric and non numeric value for further use. Every variable name must prefix with dollar ($) symbol and must begin with a letter or underscore character. A variable name can be chosen by the programmer in the meaningful way like – $name, $student_address.
Example:
<?php
$name=”Rojer”;
echo “Name=”.$name;
?>
Data Type: For storing value in a variable it is not recommended to data type must be explicitly defined by programmer like other programming language as C,C++,Java.C# etc. during script variable change the value automatically which illustrate in following program. according to this nature php is called loosely type language.
Example:
<?php
$num=85;
echo “Value of num=”.$num;
echo “<br>”.”Data type of num=”.gettype($num).”<br>”;
$num=”Rojer”;
echo”<br>”.”Now value of num=”.$num;
echo “<br>”.”Now data type of num=”.gettype($num);
?>
Operator: An operator operates on variables and performs an action in a program. They usually form a part of the arithmetical or logical expression.
PHP operators can be classified into a number of categories . They are
- Arithmetical operator : + , – , * , / , %
- Relational operator : < , <= , > , >= , = = ,= = = , !=
- Logical operator : && (AND) , || (OR), ! (NOT)
- Assignment operator : =
- Increment and Decrement operator: ++, —
- Conditional operator : ? (ternary)
- Special operator : -> , .(dot), $this , :: (scope resolution)
Now you will use each operator one by using example in next section.