PHP Constant

·   A constant is an identifier or a name.These constant value remains the same during the execution of script.

·  Constant are like variable except  once the value is assigned to the constant it remains the same throughout the script.

·   Constant are case sensitive.

·    Note:Unlike Variable constant are global across the entire script.

·     Unlike variable you do not need to define $ sign before the name of a constant.

·     A constant name starts with underscore or letter,followed by number letter or underscore.when you have defined the constant ,it can never be changed.


Following is an example:

To create a php constant use define() function:

Syntax: define(name,value,case-sensitive);

 name:name of the constant.

 Value:value of the constant.

 Case-sensitive:specifies whether it should be case-sensitive or not.By default it is false.


<?php
define("text", "Hello World!!",true);
echo text;
?>
  Output: 

Hello World!!

Constant are Global: 
Constant are global and can be used anywhere in the script.
<?php
define("text", "Hello World!!",true);
function myPhp() {
    echo text;
}
myPhp();
?>

Output: 

Hello World!!

PHP Variables
What are Variables?

Variable means to store a value, such as text string “Hello Everyone!” or the integer value 4.A variable can then be reused throughout your code,instead of using the actual value over and over again.Here are the most important things to know about about variables in php:

  • A variable starts with the $ sign, followed by the name of variable.
  • PHP variables should be alpha-numeric and underscores.(a-z,A-z,0-9,or _).
  • Variable are case sensitive.($text and $Text both are two different thing).
  • Variables with more than one word must be separated by underscore.($my_text).

Example:  
<?php
$text=”Hello”;
echo(“$text World!”);
?>
Result: Hello World!

 

  Modified On Dec-18-2017 06:03:16 AM

Leave Comment