The Php constants are similar to the variables. Constants are used to identify the name of the function. The constant name starts with a letter or underscore. You are not allowed to use $ sign before the constant name. The constants by default have the global value.

Examples:

Constant with a Case-sensitive name:

[php] <?php
// case-sensitive constant name
define(“GREETING”, “Welcome to W3Schools.com!”);
echo GREETING;
?>[/php]

In the above code the word “define” is used to begin the constant. The “GREETING” is the name of the constants and “Welcome to W3Schools.com!” is the value of the name “GREETING“. The final output is produced by using echo statement.

You will get following outcome if you run the above constants script.

Welcome to W3Schools.com!

Constant with a case-insensitive name:

[php] <?php
// case-insensitive constant name
define(“GREETING”, “Welcome to W3Schools.com!”, true);
echo greeting;
?> [/php]

In the above code, we have mentioned the echo statement “greeting” in the lowercase text. Our original name statement has the uppercase text. The constants are case sensitive, it means if you do not use the Name statement as given in the constants then script will not produce the value that defined in the constants. In such case, you have to define the “true” statement at the end of the constants to avoid error.

In the echo statement, we have added lowercase Name statement. If we run this statement, it will produce following outcome.

Welcome to W3Schools.com!

If we set the false instead of the true value then the outcome will be presented as below.

greeting

The “true” and “false” statements are the instruction to ignore the case insensitively text while running the text. The lower case echo statement will also produce the positive outcome.

Related Posts

Leave a Reply