PHP Constant tutorial with practicle example

PHP Constant tutorial with practicle example

Constant: Constant refers to fixed value that does not change the value
during the execution of program. Constant are defined using PHP’s
define () function which accept two arguments. The $ prefixed is not
required for constant.A simple Example :

<?php
//define constant
define(‘PI’,3.14);
$r=5;
//use constant
$area=PI*$r*$r;
echo “Area of circle=”.$area;
?>

 

Result – 78.5

Practical example:
This script is used for data base connection.

<?php
define(‘MYSQL_HOST’,’localhost’);
define(‘MYSQL_USER’,’root’);
define(‘MYSQL_PASS’,”);
?>

Leave a Reply

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

Do you have any Query / Suggestion