CONSTANTS A CONCEPT IN ‘C’ LANGUAGE constant. c void main() { const int a=10; a=a+1; printf(“%d”, a); } OUTPUT: Error at Line 4: Cannot modify a constant object in function main
CONSTANTS A CONCEPT IN ‘JAVA’ LANGUAGE constant. java class constant { public static void main(String args[]) { final int a=10; a=a+1; System. out. println(a); } } OUTPUT: Constant. java: Line 6: Cannot assign value to final variable ‘a’
CONSTANTS A CONCEPT IN ‘PHP’ LANGUAGE üConstants are identifiers that store values, which cannot be changed during the execution of the script. üConstants in PHP are case-sensitive. üBy convention, the constant identifier name is always in upper case. üConstant names do not start with $ sign. üA valid constant name starts with a letter or under score, followed by any number of letters, digits, or underscores.
CONSTANTS A CONCEPT IN ‘PHP’ LANGUAGE üThe syntax to create a constant is as follows: define(“CONSTANT_NAME”, constant value);
EXAMPLE 1: Constant. php <? php define(“a”, 10); a=a+1; echo a; ? > OUTPUT: Parse error: syntax error, unexpected '=' in constant. php on line 3
CONSTANTS A CONCEPT IN ‘PHP’ LANGUAGE üIf a constant is redefined in the script, it might show an error message or prints the value that is assigned to it at the very first time. üHowever, it depends on the error reporting configuration whether an error is printed or not.
EXAMPLE 2: Constant. php <? php define(“a”, 10); echo a; define(“a”, 20); echo a; ? > OUTPUT: 10 Notice: Constant a already defined in constant. php on line 4 10