Constants & Operators in PHP: A Complete Beginner’s Guide
php constant with Operators example
In PHP programming, constants and operators play a crucial role in creating efficient, reliable, and easy-to-maintain code. Whether you are learning PHP basics or building scalable web applications, understanding these core concepts is essential.
This guide explains what constants are, how to create them, and how operators are used to perform calculations, comparisons, and logical operations.
A constant in PHP is a name or identifier that holds a value which cannot be changed during script execution.
Value cannot be modified once defined
Constants do not use a dollar sign ($)
Automatically global across the script
Used for fixed values (e.g., API keys, configuration values)
PHP provides two ways to define constants:
define() Function
<?php
define("SITE_NAME", "OrientalGuru");
echo SITE_NAME;
?>
"SITE_NAME" → constant name
"OrientalGuru" → constant value
const Keyword
<?php
const PI = 3.14;
echo PI;
?>
➤ const is used inside classes and global code.
Must start with a letter or underscore
Case-sensitive by default
Use uppercase names for readability (best practice)
PHP has built-in constants that change based on context.
| Magic Constant | Description |
|---|---|
__LINE__ |
Current line number |
__FILE__ |
Full path of the file |
__DIR__ |
Directory of the file |
__FUNCTION__ |
Current function name |
__CLASS__ |
Class name |
__METHOD__ |
Method name |
Example:
<?php
echo __FILE__;
?>
Operators are symbols used to perform operations on variables and values.
PHP provides several types of operators:
Used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition |
$a + $b |
- |
Subtraction |
$a - $b |
* |
Multiplication |
$a * $b |
/ |
Division |
$a / $b |
% |
Modulus |
$a % $b |
** |
Exponent |
$a ** 2 |
Example:
<?php
echo 10 + 5; // 15
?>
Used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
= |
Simple assignment |
$a = 10 |
+= |
Add and assign |
$a += 5 |
-= |
Subtract and assign |
$a -= 5 |
*= |
Multiply and assign |
$a *= 5 |
/= |
Divide and assign |
$a /= 5 |
Used to compare values.
| Operator | Description | Example |
|---|---|---|
== |
Equal |
$a == $b |
=== |
Identical (type + value) |
$a === $b |
!= |
Not equal |
$a != $b |
!== |
Not identical |
$a !== $b |
> |
Greater than |
$a > $b |
< |
Less than |
$a < $b |
>= |
Greater or equal |
$a >= $b |
Example:
<?php
var_dump(5 == "5"); // true
var_dump(5 === "5"); // false
?>
Used for conditional logic.
| Operator | Meaning | Example |
|---|---|---|
&& or and |
True if both true |
$a && $b |
| ` |
oror` |
|
! |
Not |
!$a |
Example:
<?php
if ($age >= 18 && $citizen == true) {
echo "Eligible to vote";
}
?>
| Operator | Description |
|---|---|
++$a |
Pre-increment |
$a++ |
Post-increment |
--$a |
Pre-decrement |
$a-- |
Post-decrement |
| Operator | Description | Example |
|---|---|---|
. |
Concatenation |
$a . $b |
.= |
Append string |
$a .= $b |
Example:
<?php
$name = "Shubham";
$name .= " Mishra";
echo $name;
?>
| Operator | Description |
|---|---|
+ |
Union |
== |
Equal arrays |
=== |
Identical arrays |
!= |
Not equal |
Constants and operators are foundational components in PHP. Constants let you store fixed values that don’t change during execution, while operators allow you to perform calculations, comparisons, and logic operations.
Mastering these concepts will help you write cleaner, more reliable PHP code and prepare you for advanced topics like functions, loops, and object-oriented programming.