Friday, September 18, 2020

PHP Constants



Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Constants.

A constant can be described as an identifier or a name for a simple value. A constant value cannot be changed during execution of the script. By default, a PHP constant is case-sensitive. By convention, constant identifiers are uppercase. The name of a constant begins with a letter or an underscore, then followed by any number of letters, numbers, or underscores. Once a constant is defined, it can never be changed or undefined.


How to Define a Constant?

In order to define a constant, you have to use the define() function and in order to retrieve the value of a constant, you just have to simply specify its name. Unlike with the variables, you do not need to have a constant with a $ sign. You can also use the constant() function to read a constant's value if you wish to obtain the constant's name in a dynamic manner.


PHP Constant() Function

As indicated by the name, this function will return the value of the constant.

This is useful when you want to retrieve the value of a constant, but you do not know its name, i.e. it is stored in a variable.

Example

The following below is a simple example -

<?php
   define("MINSIZE", 50);
   
   echo MINSIZE;
   echo constant("MINSIZE"); // same thing as the previous line
?>

Only scalar data (integer, string, boolean and float) can be held in constant.

RECOMMENDED: PHP Static Variables

Difference between Variables & Constants

Difference between constants and variables are -

  • There is no need to write a dollar sign ($) before writing a constant, whereas one has to write a dollar sign ($) in variable.
  • Constants can't be defined by simple assignment, they can only be defined by using the define() function.
  • Constants may be defined and accessed anywhere in the program without regard to variable scoping rules.
  • Soon as the constants have been set out, they may not be redefined or undefined.

RECOMMENDED: PHP Local Variables

Valid and Invalid Constant Names

Following is an example of valid and invalid constant names -

// Valid constant names
define("ONE",     "first thing");
define("TWO2",    "second thing");
define("THREE_3", "third thing");
define("__THREE__", "third value");

// Invalid constant names
define("2TWO",    "second thing");


PHP Magic Constants
PHP language provides a large number of predefined constants to any script which it runs.

PHP language provides five magic constants that changes depending on where they are been used. For example, the value of __LINE__ depends on the line that it is used on in your script.

This special constants are case-insensitive and are as follows -

Sr.NoName & Description
1

__LINE__

The current line number of the file.

2

__FILE__

The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.

3

__FUNCTION__

The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

4

__CLASS__

The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

5

__METHOD__

The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).


RECOMMENDED: PHP Basic Syntax

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be discussing about the PHP Operators.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.