PHP  Constants

PHP Constants are variables that once they have been defined,they cannot be changed.

PHP Constants

A constant is a name for a certain value.This value will remain unchanged when the script is execute

All constants should either start with a letter or underscore i.e no $ sign is required when declaring a constant.

NB:All constants are global ,i.e can be called inside a function.

See my Previous Lesson:PHP Strings

How  PHP Constants are created

To create a constant, use the define() function.

Constants are created using  function called define().

define(namevaluecase-insensitive)

Parameters:

  • name-this is the name of the constant
  • value-this is the value held by the constant
  • case-insensitive-specfies if the constant is case-insensitive.Default means its case sensitive

Below example creates a constant that is case-sensitive


<?php
define("NAME", "My name is a blogger!");
echo NAME;
?>

Below example creates a constant that is case-insensitive


<?php
define("NAME", "My name is a blogger!",true);
echo NAME;
?>

PHP Constants are Global

PHP  Constants  are always global once they have been declared and they can be called anywhere on the script

Below example shows a constant used inside a function despite the fact that its defined outside the function

<?php
define("NAME", "My Name is blogger!");

function myName () {
echo NAME;
}

myName();//Output is “My Name is Blogger!”
?>

See my Next Lesson PHP Operators

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com