<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.constant.php',
    1 => 'constant',
    2 => 'Retourne la valeur d\'une constante',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'Fonctions diverses',
  ),
  'prev' => 
  array (
    0 => 'function.connection-status.php',
    1 => 'connection_status',
  ),
  'next' => 
  array (
    0 => 'function.define.php',
    1 => 'define',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/misc/functions/constant.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.constant" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">constant</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">constant</span> &mdash; <span class="dc-title">Retourne la valeur d&#039;une constante</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.constant-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>constant</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   Retourne la valeur de la constante <code class="parameter">name</code>.
  </p>
  <p class="simpara">
   <span class="function"><strong>constant()</strong></span> est pratique lorsqu&#039;il faut lire la
   valeur d&#039;une constante, mais qu&#039;on ne connaît son nom que durant
   l&#039;exécution du script. Par exemple, ce nom peut être le résultat
   d&#039;une fonction.
  </p>
  <p class="simpara">
   Cette fonction fonctionne également avec les
   <a href="language.oop5.constants.php" class="link">constantes de classe</a> et <a href="language.types.enumerations.php" class="link">types enum</a>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.constant-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">name</code></dt>
     <dd>
      <p class="para">
       Le nom de la constante.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.constant-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne la valeur de la constante.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.constant-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
   Si la constante n&#039;est pas définie, une exception <span class="classname"><a href="class.error.php" class="classname">Error</a></span> est levée.
   Antérieur à PHP 8.0.0, une erreur de niveau <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était émise dans ce cas.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.constant-changelog">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       Si la constante n&#039;est pas définie, <span class="function"><strong>constant()</strong></span> lance
       désormais une exception <span class="classname"><a href="class.error.php" class="classname">Error</a></span> ; auparavant une
       <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était émise, et <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> était retourné.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>



 <div class="refsect1 examples" id="refsect1-function.constant-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Utilisation de la fonction <span class="function"><strong>constant()</strong></span> avec des constantes</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">MAXSIZE</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">); </span><span style="color: #FF8000">// identique à la ligne précédente<br /><br /><br /></span><span style="color: #007700">interface </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br />class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$const </span><span style="color: #007700">= </span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'bar::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$const</span><span style="color: #007700">)); </span><span style="color: #FF8000">// string(7) "foobar!"<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$const</span><span style="color: #007700">)); </span><span style="color: #FF8000">// string(7) "foobar!"<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Utilisation de la fonction <span class="function"><strong>constant()</strong></span> avec des types enum (à partir de PHP 8.1.0)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">enum </span><span style="color: #0000BB">Suit<br /></span><span style="color: #007700">{<br />    case </span><span style="color: #0000BB">Hearts</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Diamonds</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Clubs</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Spades</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$case </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hearts'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'Suit::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$case</span><span style="color: #007700">)); </span><span style="color: #FF8000">// enum(Suit::Hearts)<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.constant-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.define.php" class="function" rel="rdfs-seeAlso">define()</a> - D&eacute;finit une constante</span></li>
    <li><span class="function"><a href="function.defined.php" class="function" rel="rdfs-seeAlso">defined()</a> - V&eacute;rifie si une constante avec le nom donn&eacute; existe</span></li>
    <li><span class="function"><a href="function.get-defined-constants.php" class="function" rel="rdfs-seeAlso">get_defined_constants()</a> - Retourne la liste des constantes et leurs valeurs</span></li>
    <li>La section sur les <a href="language.constants.php" class="link">constantes</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>