Search This Blog

  • ()
  • ()
Show more
PHP Tutorial - PHP Constants
HomeTutorialsPROGRAMMIM

PHP Tutorial - PHP Constants

Constants are used to make sure a value does not change throughout the running of the script. Syntax To define a constant, use the define() ...

Constants are used to make sure a value does not change throughout the running of the script.

Syntax


To define a constant, use the define() function, and include name of the constant, followed by the value for the constant, as shown here:

define( "MY_CONSTANT", "1" ); // MY_CONSTANT always has the string value "1"

Note



  • Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects).

  • Constants can be used from anywhere in your PHP program without regard to variable scope.

  • Constants are case-sensitive.


Example 1




<?PHP
define("aValue", 8);
print aValue;
?>


The code above generates the following result.







Example 2


Pass in true as a third parameter to define() makes the constant case-insensitive:


<?PHP/* w ww .ja va2s . co m*/
define("SecondsPerDay", 86400, true);
print SecondsPerDay;
print SECONDSperDAY;
?>


The code above generates the following result.







Example 3


The defined() function is basically the constant equivalent of isset(), as it returns true if the constant string you pass to it has been defined.

For example:


<?PHP//from ww w . j a v a2 s.c om
define("SecondsPerDay", 86400, true);
if (defined("Secondsperday")) {
// etc
}
?>







Example 4


constant() returns the value of a constant.


<?PHP/*from w w w .j a v a 2 s.c om*/
define("SecondsPerDay", 86400, true);
$somevar = "Secondsperday";
print constant($somevar);
?>


The code above generates the following result.


Example 5


Use Math constant to calculate circle area


<?php //from ww w . j a v a 2 s .co m
$radius = 4;

$diameter = $radius * 2;
$circumference = M_PI * $diameter;
$area = M_PI * pow( $radius, 2 );

echo "A radius of " . $radius . " n ";
echo "A diameter of " . $diameter . " n ";
echo "A circumference of " . $circumference . " n ";
echo "An area of " . $area . " n ";

?>


The code above generates the following result.
Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: PHP Tutorial - PHP Constants
PHP Tutorial - PHP Constants
http://www.java2s.com/Tutorials/PHPImage/myResult/E/EXAMPLE_CREATE_A_CONSTANT__26ABD1000003A2B0678B.PNG
Cr337
https://cr337.blogspot.com/2016/03/php-tutorial-php-constants.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2016/03/php-tutorial-php-constants.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy