Constants & Operators in PHP: A Complete Beginner’s Guide

11/19/2025
All Articles

php constant with Operators example

Constants & Operators in PHP: A Complete Beginner’s Guide

Constants & Operators in PHP: A Complete Beginner’s Guide

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.


What Are Constants in PHP?

A constant in PHP is a name or identifier that holds a value which cannot be changed during script execution.

Key Characteristics of Constants

  • 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)


How to Define Constants in PHP

PHP provides two ways to define constants:


1. Using define() Function

<?php
define("SITE_NAME", "OrientalGuru");
echo SITE_NAME;
?>
  • "SITE_NAME" → constant name

  • "OrientalGuru" → constant value


2. Using const Keyword

<?php
const PI = 3.14;
echo PI;
?>

const is used inside classes and global code.


Constant Naming Rules

  • Must start with a letter or underscore

  • Case-sensitive by default

  • Use uppercase names for readability (best practice)


Magic Constants in PHP

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 in PHP

Operators are symbols used to perform operations on variables and values.

PHP provides several types of operators:


1. Arithmetic 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
?>

2. Assignment Operators

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

3. Comparison Operators

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
?>

4. Logical Operators

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";
}
?>

5. Increment & Decrement Operators

Operator Description
++$a Pre-increment
$a++ Post-increment
--$a Pre-decrement
$a-- Post-decrement

6. String Operators

Operator Description Example
. Concatenation $a . $b
.= Append string $a .= $b

Example:

<?php
$name = "Shubham";
$name .= " Mishra";
echo $name;
?>

7. Array Operators

Operator Description
+ Union
== Equal arrays
=== Identical arrays
!= Not equal

Conclusion

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.

Article