pTutorial

New way to learn PHP

PHP 5 Constant

Constant

In PHP, constant is nothing it just variable that holds fixed value. You can define constant by using define keyword. As the name implies, you cannot change these constants once they represent a certain value. The names for constants have the same rules as PHP variables except that they don’t have the leading dollar sign.

  • Constants are define using the define() function.
  • You cannot give the constant name that are keyword in PHP like echo, exit.
  • The value of constant never changes.

Example of general constant

Like Pi (3.14)

Like g(9.8)

Syntax of PHP constant


  define ( " variable name " , " value " ) ;

Example of PHP constant

define ( " Pi ",3.14);

Example of PHP constant

<?php
  
    define("str", "This is a String Constant");
    
	define("data", 38);
	
    echo str;
	
    echo '
'; echo "this is integer constant:-"; echo data; echo '
'; ?>

In above example str is a string constant that hold the "This is a String Constant" and data is a integer constant that hold the 38.

One more example of PHP constant

<?php
  
define("pi", 3.14);
 
echo "find the area of circle"."
"; $radius = 12; echo " Radius of circle:".$radius."
"; $area = pi*$radius*$radius; echo " area of circle : ". $area; ?>

In above example pi is an integer constant that have value 3.14. You can use any number of time in your program. It did not changed in our whole script.


Php constant example

<?php
  
    define("srt1", "U");
    
	define("salary", 50000.50);
	
    echo " Float constant = ".salary;
	
    echo '
'; echo " this is integer constant:- "; echo srt1; echo '
'; define("srt2", "True"); echo " Boolean constant = ".srt2; ?>

.

Once a constant has been declare, it cannot be changed at any point of the program.

Pre-define constant

PHP has a lot of built-in constants like _ _LINE_ _ that you can use in your scripts for line number. I am using some PHP pre-define constant to explain the concept of PHP constant.

Some of important pre define constant

Constant Description
__file__ The file name of the current PHP script
__line__ The current line number, where this function used
PHP_VERSION PHP version used
PHP_OS The operating system running PHP

Example constant

<?php
  
echo " line number of this file: " . __LINE__. "
"; echo " Name of this file: " . __FILE__. "
"; echo " Directory of current file: " . __DIR__. "
"; echo " Running system (o S): " .PHP_OS. "
"; echo " Php version" . PHP_VERSION. "
"; ?>

In third line pre-define constant _ _LINE_ _ is used to display the current line where echo the _ _LINE_ _ constant.

_ _FILE_ _ is another constant that is used to display the name of script.

_ _DIR_ _ is used for print the working directory of the file.

PHP_OS is used for print the operating system, where script execute.

PHP_VERSION is a also PHP pre-define constant that is used for version of PHP.


QUOTES OF THE DAY

A good beginning makes a good ending.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER