English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Grundlagenanleitung

PHP Fortgeschrittene Anleitung

PHP & MySQL

PHP Referenzhandbuch

PHP-Konstante

In diesem Tutorial werden Sie lernen, wie man in PHP Konstanten verwendet, um festgelegte Werte zu speichern.

Was sind Konstanten in PHP

Konstanten sind Namen oder Identifikatoren für festgelegte Werte. Konstanten funktionieren wie Variablen, mit dem Unterschied, dass Konstanten nach ihrer Definition nicht mehr aufgelöst oder geändert werden können (Magische KonstantenAusgenommen).

Konstanten sind sehr nützlich für das Speichern von Daten, die während der Ausführung des Skripts nicht geändert werden. Beispiele für solche Daten sind Konfigurationseinstellungen, wie Benutzername und Passwort der Datenbank, die grundlegende URL der Website, der Name des Unternehmens usw.

Konstanten werden mit dem PHP-Funktion define() definiert, die zwei Parameter annimmt: den Namen der Konstante und ihren Wert. Sobald der Wert der Konstante definiert ist, kann dieser jederzeit durch Referenzierung ihres Namens aufgerufen werden. Dies ist ein einfaches Beispiel:

<?php
// Konstanten definieren
define("SITE_URL", "https://de.oldtoolbag.com/");
 
// Verwenden Sie Konstanten
echo 'Vielen Dank, dass Sie besucht haben - ' . SITE_URL;
?>
Testen Sie es heraus‹/›

Die Ausgabe des obigen Codes wird sein:

Vielen Dank, dass Sie besucht haben-https://de.oldtoolbag.com/

PHP Echo-Anweisungen werden in der Regel verwendet, um Daten anzuzeigen oder Daten an den Webbrowser auszugeben. Wir werden im nächsten Kapitel detailliert über diese Anweisung sprechen.

Hinweis:Durch das Speichern von Werten in Konstanten anstatt in Variablen kann sichergestellt werden, dass der Wert während der Ausführung der Anwendung nicht unerwartet geändert wird.

The difference between constants and variables is

  • A dollar sign ($) does not need to be written before constants, but it must be written before the name of a variable.

  • Constants cannot be defined by simple assignment; they can only be defined using the define() function.

  • Constants can be defined and accessed at any location without considering variable scope rules.

  • Once a constant is set, it cannot be redefined or undefined.

Valid and invalid constant names

// Valid constant names
define("ONE",          "first  thing");
define("TWO2",        "second  thing");
define("THREE_3",        "third  thing");
define("__THREE__",  "third  value");
// Invalid constant name
define("2TWO",        "second  thing");

PHP Magic Constants

PHP provides a large number of predefined constants for any script it runs.
There are five magic constants that change according to the position used. For example, the value of __LINE__ depends on the line used in the script. These special constants are not case-sensitive, as shown below-
Below are some 'magic' PHP constants-

Sr.NoName & Description
1

__LINE__

The current line number of the file.

2

__FILE__

The complete path and file name of the file. If used in Include, it returns the name of the included file. From PHP 4.0.2starting, __file__ always contains the absolute path, while in older versions, it may contain a relative path in some cases.

3

__FUNCTION__

Function name. (In PHP 4.3.0 added) from PHP 5starting, this constant returns the name of the declared function (case-sensitive). In PHP4inside, its value is always lowercase.

4

__CLASS__

Class name. (In PHP 4.3.0 added) from PHP 5Starting from, this constant returns the name of the declared class (case-sensitive). In PHP4inside, its value is always lowercase.

5

__METHOD__

Class method names. (In PHP5.0.0 added) method names are returned as declared (case-sensitive).

PHP naming conventions for constants

Constant names must follow the sameVariable namesThe same rule applies, which means that a valid constant name must start with a letter or underscore, followed by any number of letters, numbers, or underscores, but there is an exception:Constant names do not require a prefix with a dollar sign ($)..

Note:By convention, constant names are usually written in uppercase. This is because they are easy to recognize and distinguish from variables in the source code.