constant variables in PHP | Learn PHP

By | August 16, 2019
Spread the love

Other variables value can be changed but constant variables maintain its constant value.
Constant variables in PHP value cannot be changed.

constant variables in PHP

For declaring constants ‘const’ Keyword is used for it.

Note: During declaring a constant variable you should not use dollar sign with it.

Accessing constant variables in PHP:

For accessing constant in php

you should write class name then :: and then const variable.

Syntax:

hours::name;

Constants can be two types

See Also:

Basics of PHP

Class Constants:

Constants which are created within the class

Syntax of declaring class constants in php:

Constants can be created in php through the following methods

const VariableNameWithoutDollarSign = ValueOFVariable;

e.g const name = “khan”;

Accessing class constant variables in php:

For accessing constant in php

you should write class name then :: and then const variable.

Syntax:

hours::name;

Code:

<?php


class hours {



    //This constant is called class constant because this is defined within the class.
	
    const name = "Asad Khan";
	
	}
	
	echo hours::name;
	
	
?>

Non-class Constants:

Constants which are created outside the class

for declaring constant outside the class you have to write define() method for that.

inside define the method you have to write two parameters:

  1. One is Name of constant or variable for constant
  2. Second is Value of constant.

e.g define(“NameOfConstant”,”ValueOfConstant”);

Note: Constant Keyword is not used for not class constants.

See Also:

How to Create a Calculator in PHP

How to access Non-class constants:

To access non-class constants only The name of constant or variable of constant is used;

syntax

constantName;

e.g If you want to access the non-class constant then you to write only the variable or name of that constant.

example: echo NameOfConstant;

e.g If you want to print the non-class constant then you have to write echo then Name of constant;

echo NameOfConstant;

code:

<?php


define("NameForConstant","Value of constant");

class hours {
	
	}
	
	
	
	echo nameForConstant;
	
?>

Note: You should write the same name as the constant which you have given in constant variable

I mean to say by this is that it is case sensitive you have to write the same name which you have written in
define() function.

Solution for this:

If you are doing mistakes by writing a constant name in small letters but you have given in capital
letters in define() function then can solve this problem through the following method

define(“NameOfConstant”,”ValueOfconstant”,true);

By writing true in the define() function this will avoid your case sensitivity error during accessing your non-class constant.

example:

<?php


define("NameForConstant","Value of constant",true);

class hours {
	
	}
	
	
	echo nameForConstant;
	
?>

If you found some difficulty in code or you are facing other problems then feel free to contact me on my facebookpage

  •  
    78
    Shares
  • 76
  •  
  •  
  •  
  • 2
  •  
  •  
  •   
  •   
  •   
  •   
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *