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

11/19/2025

php constant with Operators example

Go Back

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.


php constant with Operators example

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 ConstantDescription
__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.

OperatorDescriptionExample
+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.

OperatorMeaningExample
=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.

OperatorDescriptionExample
==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.

OperatorMeaningExample
&& or andTrue if both true$a && $b
` oror`
!Not!$a

Example:

<?php
if ($age >= 18 && $citizen == true) {
    echo "Eligible to vote";
}
?>

5. Increment & Decrement Operators

OperatorDescription
++$aPre-increment
$a++Post-increment
--$aPre-decrement
$a--Post-decrement

6. String Operators

OperatorDescriptionExample
.Concatenation$a . $b
.=Append string$a .= $b

Example:

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

7. Array Operators

OperatorDescription
+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.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials