Tags
Asked 3 years ago
13 Jul 2021
Views 212
Arnaldo

Arnaldo posted

What is define () in PHP ?

What is define () in PHP ?
sachin

sachin
answered Aug 19 '21 00:00

define() function can define the consant in PHP

define(string $constant_name, mixed $value, bool $case_insensitive = false)
define() function must have two argument , one is constant name and other is value of the constant , third is optional parameter which can be true and false for making it to acess case sensitive or not

define() function return true or false


<?php
define('DATABASE_HOST','localhost');
echo DATABASE_HOST;
?>

it will print localhost


define() function is used to define variable which is constant like configure stuff
angeo

angeo
answered Aug 19 '21 00:00

in PHP 7 , define() function changed so array also can be value of the constant


<?php
define("DATBASECONSTANT",array("host"=>"localhost","databasename"=>"databasename","databasepassword"=>"password"));
print_r(DATBASECONSTANT);
?>


it will print Array ( [host] => localhost [databasename] => databasename [databasepassword] => password )

array defined by define() function , it can be accessed same as array we do in PHP
<?php
define("DATBASECONSTANT",array("host"=>"localhost","databasename"=>"databasename","databasepassword"=>"password"));
echo(DATBASECONSTANT['host']);
?>

it will print : localhost
Post Answer