0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP Constants

Last updated at Posted at 2019-07-26

PHP Constants

In this post, you will learn how to use constants to store static values ​​in PHP.

how to define a constant in PHP

What is Constant in PHP

A constant is a name or an identifier for a fixed worth. Constants are like variables, actually, once they are defined, they can not be undefined or changed (with the except magic constants).

Constants are very valuable for storing data that doesn't change while the script is running. Common examples of such data include configuration settings for example database username and password, site's base URL, company name, etc.

Constants are defined utilizing PHP's define() function, which accepts two arguments: the name of the constant, and its value. When characterized the constant value can be accessed at any time just by referring to its name. Here is a simple example:

Read also: ASP.NET VS PHP

Example

The output of the above code will be:

For more detail about PHP - http://cbitss.in/php-training-in-chandigarh/

PHP Echo Statement is often used to display or output data in web browsers.

Tip: By storing the value in a constant rather than a variable, you can ensure that the value won't get changed accidentally when your application runs.

Naming Conventions for PHP Constants

The name of the constant must follow the same guidelines. The variable name, which means a valid continuous name should start with a letter or underscore, followed by any number of letters, numbers or underscores with an exception:  the $ prefix isn't required for constant names.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?