answersLogoWhite

0

You can define a constant using the define() directive.

you can use this a number of ways;

to define a variable to a constant do:

$string = "hello";

define("string",$string);

to define a string to a constant use:

define("hello","there");

to define a integer or other numerical value use:

define("number",1.0);

Summery:

to define a string use quotes as you would do a string.

Unlike variables in PHP a constant cannot be changed or undefined once it is defined. Constant remains automatically globally throughout the script. It means that it can be accessed from inside a function. e.g.

define("Program" , "This is my first line of code!");

function simTest() {

echo Program;

}

simTest();

?>

User Avatar

Wiki User

9y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
RossRoss
Every question is just a happy little opportunity.
Chat with Ross

Add your answer:

Earn +20 pts
Q: How do you define a constant in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp