Php tutorial: Constant and echo-print statement



Constant  and echo-print statement

Constants:

1) A constant is a name or an identifier for a simple value. It is defined using the define function and it does not start with $ sign

define(“DBNAME”, TECH);

2) A constant value cannot change during the execution of the script. By default, a constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.

3) A constant differ from variable as variable value can be changed across the execution while constant value cannot

4) Constant value are global across the execution.

5) constant value are retrived using constant() function or simlying we can use echo
<?php
echo “DBNAME”;
echo “constant(DBNAME);
?>

6) PHP defined some predefined constants like _LINE_,_FILE_, _CLASS_
Print/echo statement

1) Print /echo does the same job i.e printing the statement on the screen

example
<?php
print “This is php script”;
echo ” This is php script”;
?>

2) It print the variable name in the same manner as given below

<?php
$x=10
print “This is $x”;
echo ” This is $x”;
?>

This is 10
This is 10

3) The behavior with single quotes is different
<?php
$x=10
print ‘This is $x’;
echo “This is $x”;
?>

This is $x
This is 10


Leave a Reply