PHP Constants: Introduction



PHP Constants Step By Step Tutorial: Constant is an expression which have a value remain means that it is not change during the program/script execution. Constant is also called with the term of literal. Constant is usually used to assign value remain at calculation.

Note: Constant have the global characteristic, it means that constant can be accessed from any part in script without the abusively scope access.

To define a constant, you can use the function of define(). Only data type of scalar (boolean, integer, float, and string) which can be used as the data type of constant. The identifier name for constant is mould and declare without early with the dollar sign($).

Pay attention to the example of using constant at script as following:

File name: konstanta.php

<html>
<head>
<title>Using Konstanta</title>
</head>

<body>
<?
  define ("interest", 0.12); // constant
  $save = 50000000;
  $saving = $save + ($save * interest); 
  printf ("interest = %s"."<br>", interest);
  printf ("save = %s "."<br>", $save);
  printf ("saving = %s"."<br>", $saving);
  echo ("<h3>try to alter the value 
  of interest konstanta</h3>");
  $interest = 0.15;
  // mould interest as constant
  printf ("interest = %s"."<br>", interest);
  // mould interest as variable
  printf ("interest = %s"."<br>", $interest);
?>
</body>
</html>
php constant

In the example of the script above, the changes of the $interest constant value and 0.15 is not have an effect when mould as constant.



Series this article:
PHP Operators: Introduction
PHP Operators: Arithmetic Operators
PHP Operators: Assignment Operators
PHP Operators: Assignment Operators Table
PHP Operators: Bitwise Operators
PHP Operators: Relation Operators
PHP Operators: Ternary Operator
PHP Operators: Error Control Operator
PHP Operators: Execution Operator
PHP Operators: Increment or Decrement Operator
php Operators: Logic Operator
PHP Operators: String Operator
PHP Operators: Array Operator
PHP Operators: Object Operator
PHP Operators: Operator Priority
PHP Constants: Introduction


Tag: Constants Category: PHP Basic Post : March 08th 2008 Read: 1,538 Bookmark and Share

blog comments powered by Disqus