Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP Defining Constants
Next article icon

PHP Defining Constants

Last Updated : 23 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In a production-level code, it is very important to keep the information as either variables or constants rather than using them explicitly. A PHP constant is nothing but an identifier for a simple value that tends not to change over time(such as the domain name of a website eg. www.geeksforgeeks.org). It is ideal to keep all the constants in a single PHP script so that maintenance is made easier. A valid constant name must start with an alphabet or underscore and requires no '$'. It is to be noted, constants are irrespective of their scope i.e constants are automatic of global scope. In order to create a constant in PHP, we must use the define() method.

Syntax:  

bool define(identifier, value, case-insensitivity)

Parameters: The function has two required parameters and one optional parameter.  

  • identifier: Specifies the name to be assigned to the constant.
  • value: Specifies the value to be assigned to the constant.
  • case-insensitivity(Optional): Specifies whether the constant identifier should be case-insensitive. By default, it is set to false i.e. case-sensitive.  

Return Type: This method returns TRUE on success and FALSE on Failure.

Note: From PHP 8.0, the constants defined with the help of the define() function may be case-insensitive.

Below are some examples to illustrate the working of the define() function in PHP.

Example 1: Below program illustrates defining case-insensitive constants.

PHP
<?php      // Case-insensitive constants   define("Constant","Hello Geeks!",TRUE);   echo constant;   echo Constant; ?>  

Output: 

Hello Geeks!  // Case Insensitive thus value is echoed Hello Geeks!

Example 2: Below program illustrates defining case-sensitive constants.

PHP
<?php    // Case-sensitive constant   define("Constant","Hello Geeks!");   echo constant;   echo Constant; ?>  

Output: 

constant   // Case Sensitive thus value not echoed Hello Geeks! 

The PHP compiler will also throw a warning for the above program along with the output as "PHP Notice: Use of undefined constant constant- assumed 'constant' in line 5". 

Summary: 

  • Constants are identifiers that can be assigned values(string, boolean, array, integer, float, or NULL) that generally don't change over time.
  • Constants are irrespective of the scope and always populate the global scope.
  • define() method is used to define constants.
  • The defined() method is used to check if a constant is defined.
  • The constant() method is used to return the value of a constant and NULL if not the constant is not defined. 

Next Article
PHP Defining Constants

P

PronabM
Improve
Article Tags :
  • Misc
  • Web Technologies
  • PHP
  • PHP-basics
Practice Tags :
  • Misc

Similar Reads

    PHP constant() Function
    The constant() function returns the value of a constant. It also works with class constants. Syntax: constant(constant) Parameter Values: constant: It is a required value that specifies the value of the constant. Return Value: It returns the value of a constant if the constant is defined else return
    1 min read
    PHP get_defined_constants() Function
    The get_defined_constants() function is an inbuilt function in PHP that returns the name of all the constants that are currently defined with their values in the form of an associative array. Syntax: get_defined_constants(bool $categorize = false): arrayParameter: This function accepts single parame
    2 min read
    PHP Constants
    A constant is a name or identifier used to store a fixed value that does not change during the execution of a PHP script. Unlike variables, constants do not start with a $ symbol and stay the same once they are defined.Constants are immutable (cannot be changed after definition).They are global by d
    3 min read
    Constants in C
    In C programming, constants are read-only values that cannot be modified during the execution of a program. These constants can be of various types, such as integer, floating-point, string, or character constants. They are initialized with the declaration and remain same till the end of the program.
    3 min read
    PHP cos( ) Function
    Trigonometry is an important part of mathematics and PHP’s math functions provides us with some functions which are very useful for calculations involving trigonometry. The cos()function in PHP is used to find the cosine of a number. The cos() function returns a float value between -1 and 1, which r
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences