PHP: Constants - Manual in Deutsh
PHP: Constants - Manual in French
PHP: Constants - Manual in Polish

You Are At PHP: Constants - Manual Area


recent searches:
include functions , variable functions , post functions...




If you are new to PHP or just need to refresh your skills, this is the place to start. This series of tutorials will give you the basic knowledge you will need to create a simple PHP website.

PHP is a reflective programming language originally designed for producing dynamic web pages.[1] PHP is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications. Textual User Interfaces can also be created using ncurses.

PHP Manual
PrevNext

Chapter 13. Constants

Table of Contents
Syntax
Magic constants

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Tip: You may also want to take a look at the Appendix S.

Example 13-1. Valid and invalid constant names

<?php

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR", "something more");

// Invalid constant names
define("2FOO",    "something");

// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__", "something");

?>

Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).

Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.

Syntax

You can define a constant by using the define()-function. Once a constant is defined, it can never be changed or undefined.

Only scalar data (boolean, integer, float and string) can be contained in constants. Do not define resource constants.

You can get the value of a constant by simply specifying its name. Unlike with variables, you should not prepend a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants.

Note: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function.

These are the differences between constants and variables:

Example 13-2. Defining Constants

<?php
define
("CONSTANT", "Hello world.");
echo
CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
?>

See also Class Constants.

wiza do rosji, wizy do rosji, wizy do rosji, wizy do rosji gdańsk, wiza do rosji, wizy do rosji, serwery vps, labrador retriever, projekty domów, prezent, śmieszne filmy, zabudowa stoisk, Przeprowadzki firm Katowice
sklep nike kowalstwo artystyczne Kreta podlasie ma swoje pro czaprak
Pozycjonowanie Stron | darmowe tworzenie stron | wideofilmowanie | Anna Maria Jopek | Wizzair | Nieruchomości Szczecin | olejki do sauny

Another Useful functions:


zend.copy-constructor | zend-api.zval-update-constant | zend-api.zend-update-class-constants | zend-api.zend-u-register-constant | zend-api.zend-u-get-constant | zend-api.zend-register-stringl-constant | zend-api.zend-register-string-constant | zend-api.zend-register-long-constant | zend-api.zend-register-double-constant | zend-api.zend-register-constant | zend-api.zend-get-constant | zend-api.zend-declare-class-constant | zend-api.zend-declare-class-constant-stringl | zend-api.zend-declare-class-constant-string | zend-api.zend-declare-class-constant-long | zend-api.zend-declare-class-constant-double | zend-api.zend-declare-class-constant-bool | zend-api.register-stringl-constant | zend-api.register-string-constant | zend-api.register-main-stringl-constant | zend-api.register-main-string-constant | zend-api.register-main-long-constant | zend-api.register-main-double-constant | zend-api.register-long-constant | zend-api.register-double-constant | streams.constants | reserved.constants | migration52.global-constants | migration52.class-constants | language.oop5.constants | language.oop.constructor | language.constants.predefined | language.constants | internals.pdo.constants | http.constants | function.xsl-xsltprocessor-construct | function.tidy-construct | function.swish-construct | function.swfvideostream.construct | function.swftextfield.construct | function.swftext.construct | function.swfsprite.construct | function.swfsound.construct | function.swfshape.construct | function.swfprebuiltclip.construct | function.swfmovie.construct | function.swfmorph.construct | function.swfgradient.construct | function.swffont.construct | function.swfbutton.construct | function.swfbitmap.construct | function.swfaction.construct | function.soap-soapvar-construct | function.soap-soapserver-construct | function.soap-soapparam-construct | function.soap-soapheader-construct | function.soap-soapfault-construct | function.soap-soapclient-construct | function.simplexml-element-construct | function.sdo-model-reflectiondataobject-construct | function.sdo-das-relational-construct | function.sammessage-constructor | function.samconnection-constructor | function.runkit-constant-remove | function.runkit-constant-redefine | function.runkit-constant-add | function.pharfileinfo-construct | function.phar-construct | function.pdo-construct | function.ming-useconstants | function.httprequestpool-construct | function.httprequest-construct | function.httpquerystring-construct | function.httpmessage-construct | function.httpinflatestream-construct | function.httpdeflatestream-construct | function.harudoc-construct | function.get-defined-constants | function.dom-domxpath-construct | function.dom-domtext-construct | function.dom-domprocessinginstruction-construct | function.dom-domimplementation-construct | function.dom-domentityreference-construct | function.dom-domelement-construct | function.dom-domdocument-construct | function.dom-domcomment-construct | function.dom-domattr-construct | function.directoryiterator-construct | function.constant | function.bcompiler-write-constant | function.arrayobject-construct | function.apc-load-constants | function.apc-define-constants | curl.constants |


PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual, and the example archive sites and some of the other resources available in the links section.