CakeFest 2024: The Official CakePHP Conference

if

(PHP 4, PHP 5, PHP 7, PHP 8)

Das if-Konstrukt ist eines der wichtigsten Features vieler Programmiersprachen, so auch in PHP, denn es ermöglicht die bedingte Ausführung von Kodefragmenten. PHP bietet eine if-Anweisung, die der in C ähnelt:

if (ausdruck)
  anweisung

ausdruck wird wie im Abschnitt über Ausdrücke beschrieben zu einem booleschen Wahrheitswert ausgewertet. Evaluiert ausdruck zu true so wird anweisung von PHP ausgeführt, anderenfalls wird es ignoriert. Weitere Informationen dazu welche Werte als true oder false ausgewertet werden, dem Abschnitt 'Umwandlung zu boolean' zu entnehmen.

Das folgende Beispiel würde a ist größer als b ausgeben, wenn $a größer als $b ist:

<?php
if ($a > $b)
echo
"a ist größer als b";
?>

Oft werden Sie mehr als eine Anweisung bedingt ausführen wollen. Dazu ist es natürlich nicht notwendig jede Anweisung mit einer eigenen if-Klausel zu versehen. Statt dessen können mehrere Anweisung zu einer Anweisungsgruppe zusammengefasst werden. So würde z.B. der folgende Programmcode a ist größer als b ausgeben, falls $a größer als $b ist, und den Wert von $a an $b zuweisen:

<?php
if ($a > $b) {
echo
"a ist größer als b";
$b = $a;
}
?>

If-Anweisungen können beliebig oft ineinander verschachtelt werden, was vollständige Flexibilität für die bedingte Ausführung der verschiedenen Teile des Programs bietet.

add a note

User Contributed Notes 4 notes

up
168
robk
10 years ago
easy way to execute conditional html / javascript / css / other language code with php if else:

<?php if (condition): ?>

html code to run if condition is true

<?php else: ?>

html code to run if condition is false

<?php endif ?>
up
35
techguy14 at gmail dot com
13 years ago
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:

<?php
if( $a == 1 || $a == 2 ) {
if(
$b == 3 || $b == 4 ) {
if(
$c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>

You could just simply do this:

<?php
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) {
//do that something here.
}
?>

Hope this helps!
up
24
Christian L.
13 years ago
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:

<?php
$v
= 1;

$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes'
$r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'

echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed

// and since PHP 5.3
$v = 'My Value';
$r = ($v) ?: 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE

$v = '';
echo (
$v) ?: 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE
?>

Parentheses can be left out in all examples above.
up
24
grawity at gmail dot com
16 years ago
re: #80305

Again useful for newbies:

if you need to compare a variable with a value, instead of doing

<?php
if ($foo == 3) bar();
?>

do

<?php
if (3 == $foo) bar();
?>

this way, if you forget a =, it will become

<?php
if (3 = $foo) bar();
?>

and PHP will report an error.
To Top