PHP

PHP Constants

Constants are PHP containers for values that remain constant and never change. They’re mostly used for data that is known well in advance and that is used, unchanged, in multiple places within your application. Good candidates for constants are debug and levels, version numbers, configuration flags, and formula.

Constants are defined using PHP’s define() function, which accept two arguments:
The name of the constant and its value. Constant names must follow the same rule as variable names with one exception: the $ prefix is not required for constant names.
Here’s an example of defining and using a constant in a script:

<?php
//define constants
define(‘PROGRAM’, ‘The Matrix’);
define(‘VERSION’, 11.7);
// use constants
//output: ‘Welcome to The Matrix(version 11.7)’
echo ‘Welcome to ‘. PROGRAM. ‘(version’. VERSION. ‘)’;
?>

By convention, constant names are usually entirely uppercased; this is to enable their easy identification and differentiation from “regular” variables in a script.

Recommended Tutorials for you


Related posts

Sorting Arrays in PHP

Anuj Kumar

Understanding of php.ini

Anuj Kumar

PHP Constants

Anuj Kumar

Password Hashing in PHP

Anuj Kumar

Captcha Image Verification

Anuj Kumar

How to insert data into MySql using PHP

Anuj Kumar

PHP Echo / Print

Anuj Kumar

PHP – PECL

Anuj Kumar

How to fetch data from mysql using PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.