Constants are similar to variables only in the sense that are used to store values, but otherwise, constants and variables differ in many ways.
To create a constant, it is used the define() function instead of the assignment operator (=):

define("CONSTANT_NAME", 'value', [case-insensitive]);
-The case-insensitive parameter at the end of the definition is optional (a boolean value: true or false), and by default is false, meaning that the defined constant is in fact case-sensitive.
- Constants do not use the initial dollar, their name must start with either a letter or an underscore character, followed by letters, numbers, or underscores.
- By default and convention, a defined constant is uppercase, although this is not required.
- A constant can only be assigned a scalar value (a string or a number). And unlike variables, a constant's value cannot be changed.
- To access the value of a constant, make reference to its name.

The following example creates two constants and attempts to recreate one of them.
define("BROTHERS", 'Victor, Alex');
define("COUSIN", "Vasile", true);                // defined case-insensitive

echo 'My brothers: '. BROTHERS. '<br />';        // My brothers: Victor, Alex

// Try to redefine the BROTHERS constant
define("BROTHERS", 'Relu');                      // Notice: Constant BROTHERS already defined in ...

echo 'My cousin: '. COUSIN. '<br />';            // My cousin: Vasile

// assign the constant's value (the one case-insensitive) to a variable
$cname = Cousin;
echo 'My cousin: '. $cname;                     // My cousin: Vasile

You cannot put the constant within quotation marks:
                echo "My brothers: BROTHERS";     results:   My brothers: BROTHERS

The scope of a defined constant is global, meaning it can be used in any defined function or class that is also part of that code file, including any other included files or functions.

• If you want to check if a constant is defined, you can use the defined("CONSTANT") function. This function returns 1 if "CONSTANT" is defined, otherwise returns false.
define("BROTHERS", 'Victor, Alex');
echo defined("BROTHERS");           // 1
- The parameter with the name of the constant must be added within double quotes.

Daily Test with Code Example

Which attribute can be used to add CSS styles directly in the HTML tag?
type style class
<div style="width: 80%; border: 3px solid #888888;">Content</div>
Which CSS transform method distorts the HTML element in a given angle (including its content)?
translate() scale() skew()
#some_id {
  transform: skew(20deg, 25deg);
  -ms-transform: skew(20deg, 25deg);   /* IE 9 */
  -webkit-transform: skew(20deg, 25deg);   /* Safari and Chrome */
Click on the function which converts a Date object to a string.
indexOf() toString() getDate()
var rightnow = new Date();
alert( rightnow.toString() );
Which function applies a callback function to the elements of the given array?
array_merge() array_search() array_map()
$arr = arra("abc", "<p>xyz</p>", "<em>PHP</em>");
// apply the strip_tags() function to delete HTML tags from each array item
$arr = array_map("strip_tags", $arr);

Last accessed pages

  1. jQuery ajax() method (13416)
  2. SHA512 Encrypt hash in JavaScript (7775)
  3. Mahjong Games (17768)
  4. Create simple Website with PHP (16216)
  5. Add data from form in text file in JSON format (8983)

Popular pages this month

  1. Qwop (3030)
  2. Drag Racer V3 (1516)
  3. Bubbles3 (727)
  4. Flash Games - Free online Games (677)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (665)