<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.info.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.assert.php',
    1 => 'assert',
    2 => '&Uuml;berpr&uuml;ft eine Assertion (Zusicherung)',
  ),
  'up' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP-Optionen-/-Informationen-Funktionen',
  ),
  'prev' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP-Optionen-/-Informationen-Funktionen',
  ),
  'next' => 
  array (
    0 => 'function.assert-options.php',
    1 => 'assert_options',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/info/functions/assert.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.assert" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">assert</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">assert</span> &mdash; <span class="dc-title">Überprüft eine Assertion (Zusicherung)</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.assert-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>assert</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$assertion</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.throwable.php" class="type Throwable">Throwable</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$description</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>assert()</strong></span> ermöglicht es, Expectations (Annahmen) zu
   definieren: Zusicherungen, die in Entwicklungs- und Testumgebungen wirken,
   aber so optimiert sind, dass sie in Produktionsumgebungen keinen
   Mehraufwand verursachen.
  </p>
  <p class="para">
   Zusicherungen können zur Unterstützung der Fehlersuche verwendet werden.
   So können sie z. B. für Plausibilitätsprüfungen für Vorbedingungen verwendet
   werden, die immer <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> sein sollten, und die andernfalls auf
   Programmierfehler hinweisen.
   Ein weiterer Anwendungsfall ist, sicherzustellen, dass bestimmte Merkmale
   wie Funktionen von Erweiterungen oder bestimmte Grenzwerte und Eigenschaften
   des Systems vorhanden sind.
  </p>
  <p class="para">
   Da Zusicherungen so konfiguriert werden können, dass sie deaktiviert
   werden, sollten sie <em>nicht</em> für normale
   Laufzeitoperationen wie die Überprüfung von Eingabeparametern verwendet
   werden. Als Faustregel gilt, dass sich der Code auch bei deaktivierter
   Zusicherungsüberprüfung so verhalten sollte wie erwartet.
  </p>
  <p class="para">
   <span class="function"><strong>assert()</strong></span> prüft, ob die in
   <code class="parameter">assertion</code> angegebene Annahme zutrifft. Wenn dies
   nicht der Fall ist und das Ergebnis somit <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> ist, werden die
   entsprechenden Maßnahmen ergriffen, je nachdem wie
   <span class="function"><strong>assert()</strong></span> konfiguriert wurde.
  </p>

  <p class="para">
   Das Verhalten von <span class="function"><strong>assert()</strong></span> wird durch die folgenden
   INI-Einstellungen bestimmt:
   <table class="doctable table">
    <caption><strong>Konfigurationsoptionen für assert</strong></caption>
    
     <thead>
      <tr>
       <th>Name</th>
       <th>Standard</th>
       <th>Beschreibung</th>
       <th>Changelog</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><a href="ini.core.php#ini.zend.assertions" class="link">zend.assertions</a></td>
       <td><code class="literal">1</code></td>
       <td>
        <ul class="simplelist">
         <li>
          <code class="literal">1</code>: erzeugt Code und führt ihn aus
          (Entwicklungsmodus).
         </li>
         <li>
          
          <code class="literal">0</code>: erzeugt Code, aber führt ihn zur Laufzeit
          nicht aus.
         </li>
         <li>
          <code class="literal">-1</code>: erzeugt keinen Code (Produktionsmodus).
         </li>
        </ul>
       </td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.active" class="link">assert.active</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        Wenn <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, prüft <span class="function"><strong>assert()</strong></span> die Annahme nicht und
        gibt automatisch <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> zurück.
       </td>
       <td>
        Seit PHP 8.3.0 veraltet.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.callback" class="link">assert.callback</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td>
       <td>
        <p class="para">
         Eine benutzerdefinierte Funktion, die aufgerufen wird, wenn eine
         Zusicherung fehlschlägt. Sie sollte die folgende Signatur haben:
         <div class="methodsynopsis dc-description">
          <span class="methodname"><strong>assert_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$file</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$line</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.null.php" class="type null">null</a></span> <code class="parameter">$assertion</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$description</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

        </p>
       </td>
       <td>
        <p class="para">
         Vor PHP 8.0.0 musste die Signatur des Callbacks wie folgt lauten:
         <div class="methodsynopsis dc-description"><span class="methodname"><strong>assert_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$file</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$line</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$assertion</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$description</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

        </p>
        <span class="simpara">
        Seit PHP 8.3.0 veraltet.
        </span>
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        Wenn <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, wird ein <span class="classname"><a href="class.assertionerror.php" class="classname">AssertionError</a></span> ausgelöst,
        wenn die Erwartung nicht erfüllt wird.
       </td>
       <td>
        Seit PHP 8.3.0 veraltet.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.bail" class="link">assert.bail</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></td>
       <td>
        Wenn <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, wird die Ausführung des PHP-Skripts gestoppt, wenn die
        Erwartung nicht erfüllt wird.
       </td>
       <td>
        Seit PHP 8.3.0 veraltet.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.warning" class="link">assert.warning</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        Wenn <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, wird eine <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> ausgegeben, wenn
        die Erwartung nicht erfüllt wird. Diese INI-Einstellung hat keine
        Auswirkung, wenn
        <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a> aktiviert
        ist.
       </td>
       <td>
        Seit PHP 8.3.0 veraltet.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.assert-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">assertion</code></dt>
     <dd>
      <p class="para">
       Dies ist ein beliebiger Ausdruck, der einen Wert zurückgibt, der
       ausgeführt wird und dessen Ergebnis verwendet wird, um anzuzeigen, ob
       die Zusicherung erfolgreich war oder fehlschlug.
      </p>
      <div class="warning"><strong class="warning">Warnung</strong>
       <p class="para">
        Vor PHP 8.0.0 wurde <code class="parameter">assertion</code>, wenn es vom Typ
        <span class="type"><a href="language.types.string.php" class="type string">string</a></span> war, als PHP-Code interpretiert und über
        <span class="function"><a href="function.eval.php" class="function">eval()</a></span> ausgeführt. Diese Zeichenkette wurde dann
        als drittes Argument an den Callback übergeben. Dieses Verhalten war
        in PHP 7.2.0 <em>MISSBILLIGT</em>, und wurde in PHP 8.0.0
        <em>ENTFERNT</em>.
       </p>
      </div>
     </dd>
    
    
     <dt><code class="parameter">description</code></dt>
     <dd>
      <p class="para">
       Wenn <code class="parameter">description</code> eine Instanz von
       <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span> ist, wird sie nur ausgelöst, wenn die
       <code class="parameter">assertion</code> ausgeführt wird und fehlschlägt.
       <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
        <p class="para">
         Seit PHP 8.0.0 wird dies durchgeführt <em>bevor</em> ein
         möglicherweise definierter Zusicherungs-Callback aufgerufen wird.
        </p>
       </p></blockquote>
       <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
        <p class="para">
         Seit PHP 8.0.0 wird das <a href="language.types.object.php" class="link">Objekt</a> unabhängig von der Einstellung von
         <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a>
         ausgelöst.
        </p>
       </p></blockquote>
       <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
        <p class="para">
         Seit PHP 8.0.0 hat die Einstellung von
         <a href="info.configuration.php#ini.assert.bail" class="link">assert.bail</a> in diesem Fall
         keine Wirkung mehr.
        </p>
       </p></blockquote>
      </p>
      <p class="para">
       Wenn <code class="parameter">description</code> vom Typ <a href="language.types.string.php" class="link">String</a> ist, wird
       diese Nachricht verwendet, wenn eine Exception oder eine Warnung
       ausgegeben wird. Eine optionale Beschreibung, die in die Fehlermeldung
       aufgenommen wird, wenn die <code class="parameter">assertion</code>
       fehlschlägt.
      </p>
      <p class="para">
       <code class="parameter">description</code> kann weggelassen werden.
       
       Die Standardbeschreibung entspricht dem Quellcode für den Aufruf von
       <span class="function"><strong>assert()</strong></span> und wird zur Kompilierzeit erstellt.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.assert-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Wenn mindestens eine der folgenden Bedingungen erfüllt ist, gibt
   <span class="function"><strong>assert()</strong></span> immer <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> zurück:
  </p>
  <ul class="simplelist">
   <li><code class="literal">zend.assertions=0</code></li>
   <li><code class="literal">zend.assertions=-1</code></li>
   <li><code class="literal">assert.active=0</code></li>
   <li><code class="literal">assert.exception=1</code></li>
   <li><code class="literal">assert.bail=1</code></li>
   <li>
    An <code class="parameter">description</code> wird ein benutzerdefiniertes
    Exception-Objekt übergeben.
   </li>
  </ul>
  <p class="para">
   Wenn keine der Bedingungen wahr ist, gibt <span class="function"><strong>assert()</strong></span>
   <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> zurück, sofern <code class="parameter">assertion</code> wahr ist, ansonsten
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.assert-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Beschreibung</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        Alle INI-Einstellungen für <code class="literal">assert.</code> sind veraltet.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        <span class="function"><strong>assert()</strong></span> wertet Zeichenketten nicht mehr aus,
        sondern behandelt sie stattdessen wie jeden anderen Parameter.
        Anstelle von <code class="code">assert(&#039;$a == $b&#039;)</code> sollte
        <code class="code">assert($a == $b)</code> verwendet werden. Die
        <var class="filename">php.ini</var>-Direktive <code class="literal">assert.quiet_eval</code> und die
        Konstante <strong><code><a href="info.constants.php#constant.assert-quiet-eval">ASSERT_QUIET_EVAL</a></code></strong> wurden ebenfalls
        entfernt, da sie keine Auswirkungen mehr haben.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Wenn <code class="parameter">description</code> eine Instanz von
        <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span> ist, wird nun ein Objekt ausgelöst,
        wenn die Zusicherung fehlschlägt, unabhängig vom Wert von
        <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a>.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Wenn <code class="parameter">description</code> eine Instanz von
        <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span> ist, wird kein Benutzer-Callback
        aufgerufen, auch wenn er gesetzt ist.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Die Deklaration einer Funktion namens <code class="literal">assert()</code>
        innerhalb eines Namensraums ist nicht mehr erlaubt und führt zu einem
        Fehler der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-compile-error">E_COMPILE_ERROR</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        Die Deklaration einer Funktion namens <code class="literal">assert()</code>
        innerhalb eines Namensraums ist veraltet und gibt nun einen
        <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>-Hinweis aus.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        Die Verwendung eines <a href="language.types.string.php" class="link">String</a>s als <code class="parameter">assertion</code>
        wird missbilligt. Dies erzeugt nun einen
        <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>-Hinweis, wenn sowohl
        <a href="info.configuration.php#ini.assert.active" class="link">assert.active</a> als auch
        <a href="ini.core.php#ini.zend.assertions" class="link">zend.assertions</a> auf
        <code class="literal">1</code> gesetzt sind.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.assert-examples">
  <h3 class="title">Beispiele</h3>
  <div class="example" id="example-1">
   <p><strong>Beispiel #1 <span class="function"><strong>assert()</strong></span>-Beispiel</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>
    Falls Zusicherungen aktiviert sind
    (<a href="ini.core.php#ini.zend.assertions" class="link"><code class="literal">zend.assertions=1</code></a>),
    gibt das obige Beispiel folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught AssertionError: assert(1 &gt; 2) in example.php:2
Stack trace:
#0 example.php(2): assert(false, &#039;assert(1 &gt; 2)&#039;)
#1 {main}
  thrown in example.php on line 2</pre>
</div>
   </div>
   <div class="example-contents"><p>
    Falls Zusicherungen deaktiviert sind (<code class="literal">zend.assertions=0</code>
    oder <code class="literal">zend.assertions=-1</code>), gibt das obige Beispiel
    folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>Beispiel #2 Verwenden einer benutzerdefinierten Meldung</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"Es wurde erwartet, dass eins größer ist als zwei"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>
    Falls Zusicherungen aktiviert sind, gibt das obige Beispiel folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught AssertionError: Es wurde erwartet, dass eins größer ist als zwei in example.php:2
Stack trace:
#0 example.php(2): assert(false, &#039;Es wurde erwartet,...&#039;)
#1 {main}
  thrown in example.php on line 2</pre>
</div>
   </div>
   <div class="example-contents"><p>
    Falls Zusicherungen deaktiviert sind, gibt das obige Beispiel folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-3">
   <p><strong>Beispiel #3 Verwenden einer benutzerdefinierten Exception-Klasse</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ArithmeticAssertionError </span><span style="color: #007700">extends </span><span style="color: #0000BB">AssertionError </span><span style="color: #007700">{}<br /><br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, new </span><span style="color: #0000BB">ArithmeticAssertionError</span><span style="color: #007700">(</span><span style="color: #DD0000">"Es wurde erwartet, dass eins größer ist als zwei"</span><span style="color: #007700">));<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>
    Falls Zusicherungen aktiviert sind, gibt das obige Beispiel folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught ArithmeticAssertionError: Es wurde erwartet, dass eins größer ist als zwei in example.php:4
Stack trace:
#0 {main}
  thrown in example.php on line 4</pre>
</div>
   </div>
   <div class="example-contents"><p>
    Falls Zusicherungen deaktiviert sind, gibt das obige Beispiel folgendes aus:
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.assert-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.assert-options.php" class="function" rel="rdfs-seeAlso">assert_options()</a> - Setzt oder liefert Assert-Optionen</span></li>
   </ul>
  </p>
 </div>


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