<?php define('ConstName', 'value'); ?>
<?php //valid constant names define('ONE', "first value"); define('TWO', "second value"); define('SUM 2',ONE+TWO); //invalid constant names define('1ONE', "first value"); define(' TWO', "second value"); define('@SUM',ONE+TWO); ?>
<?php define('NAME', "Rexx"); echo "Hello ".NAME; ?>
In the above example We define a constant using define( ) function. first argument for name of constant and second for its value="phptpoint". Now we print the value. Pass name of constant inside print statement Output will become
<?php define('ONE', 100); define('TWO', 100); define('SUM',ONE+TWO); print "Sum of two constant=".SUM; ?>
In the above example We declare three constant of name=(ONE,TWO,SUM). First Add the value of two constant. Now sum of these two value work as a value for third define constant(SUM). Now pass $sum inside print it will show the sum of two number.
<?php define('X', 1000); define('Y', 500); define('Z',X - Y); print "Subtraction of given number =".Z; ?>
In the above example We define three constant with name(X,Y,Z). First subtract the value of two defined constant . Now result of these two value work as a value for third define constant(Z). Pass Z inside print so it will show the subtraction of two value.
<?php define('ONE', 100); define('TWO', 100); $res= ONE+TWO; print "Sum of two constant=".$res; ?>
In the above example We define two constant with name(one,two) and value(100,100) respectively. $res variable is also define. Now we perform addition of two defined constant value and result store in a variable($res = one+two;). To print the result pass $res inside print statement.
<h2>USD/EUR Currency Conversion</h2> <?php //define exchange rate //1.00 USD= 0.80 EUR define('EXCHANGE_RATE',0.80); //define number of dollars $dollars=150; //perform conversion and print result $euros=$dollars*EXCHANGE_RATE; echo "$dollars USD is equivalent to :$euros EUR"; ?>
if you have been following along, the script should be fairly easy to understand. It begins by defining a constant named "EXCHANGE_RATE" which surprise, surprise stores the dollar-to-euro exchange rate(assumed here at 1.00 USD to 0.80 EUR). Next, it defines a variable named "$dollars" to hold the number of dollars to be converted, and then it performs an arithmetic operation using the * operator, the "$dollars" variable, and the "EXCHANGE_RATE" constant to return the equivalent number of euros. This result is then stored in a new variable named "$euros" and printed to the Web page.