Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

8. Constants & Operators

Lesson 9 of 36 in the free Web Based Programming notes on Siksha Sarovar, written by Rohit Jangra.

PHP: Constants, Comments, Operators

1. PHP Constants

A constant is an identifier whose value cannot be changed once defined.

Features of Constants: • Do not use $ symbol • Case-sensitive by default • Global scope

Defining Constants:

        define("SITE_NAME", "MyWebsite");
        echo SITE_NAME;

Using const keyword:

        const PI = 3.14;

2. PHP Comments

Comments are used to explain code and are ignored by the PHP interpreter.

Types of Comments:

  1. Single-line comment
        // This is a single-line comment
        # This is also a single-line comment
  1. Multi-line comment
        /*
         This is
         a multi-line comment
        */

3. PHP Operators

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

3.1 Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
        echo 10 + 5;

3.2 Assignment Operators

OperatorExample
=$x = 10
+=$x += 5
-=$x -= 2

3.3 Comparison Operators

OperatorMeaning
==Equal
===Identical
!=Not equal
>Greater than
        if ($a == $b) { }

3.4 Logical Operators

OperatorName
&&AND
``OR
!NOT

3.5 Increment / Decrement Operators

        $x++;
        $y--;

4. PHP Expressions

An expression is a combination of variables, operators, and values that produces a result.

Example:

        $result = ($a + $b) * 2;

5. PHP Regular Expressions

Regular Expressions (Regex) are used for pattern matching in strings.

Common PHP Regex Functions:

FunctionPurpose
preg_match()Match pattern
preg_replace()Replace text
preg_split()Split string

Example: Email Validation

        $email = "test@gmail.com";
        if (preg_match("/^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,}$/i", $email)) {
          echo "Valid Email";
        } else {
          echo "Invalid Email";
        }

Common Regex Patterns:

PatternMeaning
^Start of string
$End of string
.Any character
*Zero or more
+One or more
[a-z]Lowercase letters

6. Advantages of PHP

Why Use PHP? • Open source and free • Easy to learn and use • Platform independent • Fast execution • Supports many databases • Large community support • Widely used in web development