PHP 8.3.4 Released!

Ausdrücke

Ausdrücke (Expressions) sind die wichtigsten Bausteine von PHP. In PHP ist fast alles, was geschrieben wird, ein Ausdruck. Die einfachste, aber auch zutreffendste Definition für einen Ausdruck ist "alles, was einen Wert hat".

Die grundlegendsten Formen von Ausdrücken sind Konstanten und Variablen. Wenn man $a = 5 schreibt, weist man $a den Ausdruck 5 zu. 5 hat offensichtlich den Wert 5. Anders ausgedrückt: 5 ist ein Ausdruck mit dem Wert 5 (in diesem Fall ist 5 eine Integer-Konstante).

Nach dieser Zuweisung würde man erwarten, dass der Wert von $a nun ebenfalls 5 ist; wenn man also $b = $a schreibt, sollte dasselbe dabei herauskommen, als hätte man $b = 5 geschrieben. Anders ausgedrückt: $a ist ebenfalls ein Ausdruck mit dem Wert 5. Wenn alles richtig funktioniert, wird genau das passieren.

Etwas kompliziertere Beispiele für Ausdrücke sind Funktionen. Betrachten wir zum Beispiel die folgende Funktion:

<?php
function foo ()
{
return
5;
}
?>

Angenommen, Sie sind mit dem Konzept von Funktionen vertraut (wenn Sie es nicht sind, lesen Sie das Kapitel über Funktionen), dann würden Sie davon ausgehen, dass die Eingabe von $c = foo() grundsätzlich dasselbe bedeutet, als würde man $c = 5 schreiben, und genau das trifft zu. Funktionen sind Ausdrücke mit dem Wert ihres Rückgabewertes. Da foo() den Wert 5 zurückgibt, ist der Wert des Ausdruckes 'foo()' 5. Normalerweise geben Funktionen nicht einfach einen statischen Wert zurück, sondern berechnen etwas.

Natürlich müssen Werte in PHP keine Integer-Zahlen sein, und oft sind sie es auch nicht. PHP unterstützt vier skalare Datentypen: int (Ganzzahlen), float (Fließkommazahlen), string (Zeichenketten) und bool (Wahrheitswerte). (Skalare sind Datentypen, die man, im Gegensatz zu zum Beispiel Arrays, nicht in kleinere Stücke 'brechen' kann.) PHP unterstützt auch zwei zusammengesetzte (nicht-skalare) Datentypen: Arrays und Objekte. Jeder dieser Datentypen kann Variablen zugewiesen und von Funktionen zurückgegeben werden.

PHP fasst den Begriff Ausdruck aber noch viel weiter, wie es auch andere Programmiersprachen tun. PHP ist eine ausdrucksorientierte Sprache, in dem Sinne, dass fast alles einen Ausdruck darstellt. Zurück zu dem Beispiel, mit dem wir uns bereits beschäftigt haben: $a = 5. Es ist einfach zu erkennen, dass hier zwei Werte enthalten sind: Der Wert der Integer-Konstanten 5 und der Wert von $a, der ebenfalls auf 5 geändert wird. In Wirklichkeit ist aber noch ein weiterer Wert enthalten, nämlich der Wert der Zuweisung selbst. Die Zuweisung selbst enthält den zugewiesenen Wert, in diesem Fall 5. In der Praxis bedeutet dies, dass $a = 5, egal was es tut, immer einen Ausdruck mit dem Wert 5 darstellt. Folglich ist $b = ($a = 5) gleichbedeutend mit $a = 5; $b = 5; (ein Semikolon markiert das Ende einer Anweisung). Da Wertzuweisungen von rechts nach links geparst werden, kann man auch $b = $a = 5 schreiben.

Ein anderes gutes Beispiel für die Ausdrucksorientierung von PHP sind Prä- und Post-Inkrement sowie die entsprechenden Dekremente. Benutzer von PHP und vielen anderen Sprachen sind vermutlich mit den Notationen variable++ und variable-- vertraut. Dies sind Inkrement- und Dekrement-Operatoren. In PHP gibt es, wie in C, zwei Arten von Inkrementen - Prä-Inkrement und Post-Inkrement. Grundsätzlich erhöhen sowohl Prä- als auch Post-Inkrement den Wert der Variable, und der Effekt auf die Variable ist derselbe. Der Unterschied liegt im Wert des Inkrement-Ausdruckes. Das Prä-Inkrement, das ++$variable geschrieben wird, enthält als Wert den Wert der erhöhten Variable (PHP erhöht den Wert der Variablen, bevor es ihren Wert ausliest, daher der Name 'PRÄ-Inkrement'). Das Post-Inkrement, das $variable++ geschrieben wird, enthält dagegen den ursprünglichen Wert von $variable vor der Erhöhung (PHP erhöht den Wert der Variablen, nachdem es ihren Wert ausgelesen hat, daher der Name 'POST-Inkrement').

Ein sehr gebräuchlicher Typ von Ausdrücken sind Vergleichsausdrücke. Diese Ausdrücke geben entweder false oder true zurück. PHP unterstützt > (größer), >= (größer oder gleich), == (gleich), != (ungleich), < (kleiner), und <= (kleiner oder gleich). Die Sprache unterstützt weiterhin ein Set von absoluten Vergleichsoperatoren: === (inhalts- und typgleich) und !== (nicht inhalts- oder typgleich). Diese Ausdrücke werden meist in bedingten Anweisungen, wie z. B. in if-Anweisungen, verwendet.

Im letzten Beispiel für Ausdrücke befassen wir uns mit kombinierten Zuweisungs- und Operator-Ausdrücken. Wir wissen bereits, dass, wenn wir $a um 1 erhöhen wollen, wir einfach $a++ oder ++$a schreiben können. Aber was tut man, wenn man den Wert um mehr als eins erhöhen will, z. B. um 3? Man könnte mehrere Male $a++ schreiben, aber das ist offensichtlich weder ein effizienter noch komfortabler Weg. Viel üblicher ist es, einfach $a = $a + 3 zu schreiben. $a + 3 gibt den Wert von $a plus 3 zurück, dieser wird wieder $a zugewiesen, was dazu führt, dass $a nun um 3 erhöht wurde. In PHP - wie in einigen anderen Programmiersprachen, z. B. in C - kann man dies aber noch kürzer schreiben, was mit der Zeit klarer wird und auch einfacher zu verstehen ist. Um 3 zu dem aktuellen Wert von $a hinzuzufügen, schreibt man $a += 3. Das bedeutet exakt: "Nimm den Wert von $a, addiere 3 hinzu und weise $a den entstandenen Wert zu". Zusätzlich dazu, dass diese Schreibweise kürzer und klarer ist, wird sie auch schneller ausgeführt. Der Wert von $a += 3 ist, wie der Wert einer regulären Zuweisung, der zugewiesene Wert. Beachten Sie, dass dieser Wert NICHT 3, sondern dem kombinierten Wert von $a plus 3 entspricht (das ist der Wert, der $a zugewiesen wird). Jeder Operator, der zwei Elemente verbindet, kann in dieser Schreibweise verwendet werden, z. B. $a -= 5 (vermindert den Wert von $a um 5) oder $b *= 7 (multipliziert den Wert von $b mit 7), usw.

Es gibt einen weiteren Ausdruck, der Ihnen vielleicht seltsam vorkommt, wenn Sie ihn bisher noch in keiner Programmiersprache kennengelernt haben, den dreifach konditionalen Operator:

<?php
$eins
? $zwei : $drei
?>

Wenn der Wert des ersten Sub-Ausdruckes (hier: $eins) true ist (d. h. nicht 0), dann wird der Wert des zweiten Subausdrucks (hier: $zwei) ausgewertet und ist das Ergebnis des konditionalen Ausdrucks. Andernfalls wird der dritte Subausdruck ausgewertet und dessen Wert zurückgegeben.

Das folgende Beispiel sollte das Verständnis von Prä- und Post-Inkrement und von Ausdrücken im Allgemeinen erleichtern:

<?php
function verdoppeln($i)
{
return
$i*2;
}
$b = $a = 5; /* Weise den Variablen $a und $b den Wert 5 zu */
$c = $a++; /* Post-Inkrement, der urspruengliche Wert von $a (5)
wird $c zugewiesen. */
$e = $d = ++$b; /* Prae-Inkrement, der erhöhte Wert von $b (6) wird $d und
$e zugewiesen. */

/* An diesem Punkt sind $d und $e beide gleich 6 */

$f = verdoppeln($d++); /* Weise $f den doppelten Wert von $d vor
der Erhöhung um eins zu, 2*6 = 12 */
$g = verdoppeln(++$e); /* Weise $g den doppelten Wert von $e nach
der Erhoehung um eins zu, 2*7 = 14 to $g */
$h = $g += 10; /* Zuerst wird $g um 10 erhöht und hat damit den Wert
24. Der Wert dieser Zuweisung (24) wird dann $h
zugewiesen, womit $h ebenfalls den Wert von 24 hat. */
?>

Einige Ausdrücke können wie Anweisungen verwendet werden. In diesem Falle hat eine Anweisung die Form 'expr ;', dies meint einen Ausdruck gefolgt von einem Semikolon. In $b = $a = 5; ist $a = 5 ein gültiger Ausdruck, aber für sich allein keine Anweisung. $b = $a = 5; ist jedoch eine gültige Anweisung.

Ein letzter Punkt, der noch zu erwähnen ist, ist der Wahrheitswert von Ausdrücken. In vielen Fällen, hauptsächlich in bedingten Anweisungen und Schleifen, ist man nicht am spezifischen Wert eines Ausdrucks interessiert, sondern kümmert sich nur darum, ob er true oder false bedeutet. Die Konstanten true und false (case-insensitiv) sind die beiden möglichen Wahrheitswerte. Wenn nötig wird ein Ausdruck automatisch in einen Booleschen Wert konvertiert. Wenn Sie mehr darüber erfahren wollen, lesen Sie den Abschnitt über Typecasting.

PHP stellt eine vollständige und mächtige Implementierung von Ausdrücken bereit, deren vollständige Dokumentation den Rahmen dieses Manuals sprengen würde. Die obigen Beispiele sollten Ihnen einen guten Eindruck davon verschaffen, was Ausdrücke sind und wie man nützliche Ausdrücke konstruieren kann. Im Rest dieses Manuals werden wir expr schreiben, um auszudrücken, dass an dieser Stelle jeder gültige PHP-Ausdruck stehen kann.

add a note

User Contributed Notes 12 notes

up
56
Magnus Deininger, dma05 at web dot de
14 years ago
Note that even though PHP borrows large portions of its syntax from C, the ',' is treated quite differently. It's not possible to create combined expressions in PHP using the comma-operator that C has, except in for() loops.

Example (parse error):

<?php

$a
= 2, $b = 4;

echo
$a."\n";
echo
$b."\n";

?>

Example (works):
<?php

for ($a = 2, $b = 4; $a < 3; $a++)
{
echo
$a."\n";
echo
$b."\n";
}

?>

This is because PHP doesn't actually have a proper comma-operator, it's only supported as syntactic sugar in for() loop headers. In C, it would have been perfectly legitimate to have this:

int f()
{
int a, b;
a = 2, b = 4;

return a;
}

or even this:

int g()
{
int a, b;
a = (2, b = 4);

return a;
}

In f(), a would have been set to 2, and b would have been set to 4.
In g(), (2, b = 4) would be a single expression which evaluates to 4, so both a and b would have been set to 4.
up
50
yasuo_ohgaki at hotmail dot com
23 years ago
Manual defines "expression is anything that has value", Therefore, parser will give error for following code.

<?php
($val) ? echo('true') : echo('false');
Note: "? : " operator has this syntax "expr ? expr : expr;"
?>

since echo does not have(return) value and ?: expects expression(value).

However, if function/language constructs that have/return value, such as include(), parser compiles code.

Note: User defined functions always have/return value without explicit return statement (returns NULL if there is no return statement). Therefore, user defined functions are always valid expressions.
[It may be useful to have VOID as new type to prevent programmer to use function as RVALUE by mistake]

For example,

<?php
($val) ? include('true.inc') : include('false.inc');
?>

is valid, since "include" returns value.

The fact "echo" does not return value(="echo" is not a expression), is less obvious to me.

Print() and Echo() is NOT identical since print() has/returns value and can be a valid expression.
up
21
chriswarbo at gmail dot com
10 years ago
Note that there is a difference between a function and a function call, and both
are expressions. PHP has two kinds of function, "named functions" and "anonymous
functions". Here's an example with both:

<?php
// A named function. Its name is "double".
function double($x) {
return
2 * $x;
}

// An anonymous function. It has no name, in the same way that the string
// "hello" has no name. Since it is an expression, we can give it a temporary
// name by assigning it to the variable $triple.
$triple = function($x) {
return
3 * $x;
};
?>

We can "call" (or "run") both kinds of function. A "function call" is an
expression with the value of whatever the function returns. For example:

<?php
// The easiest way to run a function is to put () after its name, containing its
// arguments (if any)
$my_numbers = array(double(5), $triple(5));
?>

$my_numbers is now an array containing 10 and 15, which are the return values of
double and $triple when applied to the number 5.

Importantly, if we *don't* call a function, ie. we don't put () after its name,
then we still get expressions. For example:

<?php
$my_functions
= array('double', $triple);
?>

$my_functions is now an array containing these two functions. Notice that named
functions are more awkward than anonymous functions. PHP treats them differently
because it didn't use to have anonymous functions, and the way named functions
were implemented didn't work for anonymous functions when they were eventually
added.

This means that instead of using a named function literally, like we can with
anonymous functions, we have to use a string containing its name instead. PHP
makes sure that these strings will be treated as functions when it's
appropriate. For example:

<?php
$temp
= 'double';
$my_number = $temp(5);
?>

$my_number will be 10, since PHP has spotted that we're treating a string as if
it were a function, so it has looked up that named function for us.

Unfortunately PHP's parser is very quirky; rather than looking for generic
patterns like "x(y)" and seeing if "x" is a function, it has lots of
special-cases like "$x(y)". This makes code like "'double'(5)" invalid, so we
have to do tricks like using temporary variables. There is another way around
this restriction though, and that is to pass our functions to the
"call_user_func" or "call_user_func_array" functions when we want to call them.
For example:

<?php
$my_numbers
= array(call_user_func('double', 5), call_user_func($triple, 5));
?>

$my_numbers contains 10 and 15 because "call_user_func" called our functions for
us. This is possible because the string 'double' and the anonymous function
$triple are expressions. Note that we can even use this technique to call an
anonymous function without ever giving it a name:

<?php
$my_number
= call_user_func(function($x) { return 4 * $x; }, 5);
?>

$my_number is now 20, since "call_user_func" called the anonymous function,
which quadruples its argument, with the value 5.

Passing functions around as expressions like this is very useful whenever we
need to use a 'callback'. Great examples of this are array_map and array_reduce.
up
15
egonfreeman at gmail dot com
16 years ago
It is worthy to mention that:

$n = 3;
$n * --$n

WILL RETURN 4 instead of 6.

It can be a hard to spot "error", because in our human thought process this really isn't an error at all! But you have to remember that PHP (as it is with many other high-level languages) evaluates its statements RIGHT-TO-LEFT, and therefore "--$n" comes BEFORE multiplying, so - in the end - it's really "2 * 2", not "3 * 2".

It is also worthy to mention that the same behavior will change:

$n = 3;
$n * $n++

from 3 * 3 into 3 * 4. Post- operations operate on a variable after it has been 'checked', but it doesn't necessarily state that it should happen AFTER an evaluation is over (on the contrary, as a matter of fact).

So, if you ever find yourself on a 'wild goose chase' for a bug in that "impossible-to-break, so-very-simple" piece of code that uses pre-/post-'s, remember this post. :)

(just thought I'd check it out - turns out I was right :P)
up
20
Mattias at mail dot ee
21 years ago
A note about the short-circuit behaviour of the boolean operators.

1. if (func1() || func2())
Now, if func1() returns true, func2() isn't run, since the expression
will be true anyway.

2. if (func1() && func2())
Now, if func1() returns false, func2() isn't run, since the expression
will be false anyway.

The reason for this behaviour comes probably from the programming
language C, on which PHP seems to be based on. There the
short-circuiting can be a very useful tool. For example:

int * myarray = a_func_to_set_myarray(); // init the array
if (myarray != NULL && myarray[0] != 4321) // check
myarray[0] = 1234;

Now, the pointer myarray is checked for being not null, then the
contents of the array is validated. This is important, because if
you try to access an array whose address is invalid, the program
will crash and die a horrible death. But thanks to the short
circuiting, if myarray == NULL then myarray[0] won't be accessed,
and the program will work fine.
up
16
winks716
16 years ago
reply to egonfreeman at gmail dot com
04-Apr-2007 07:45

the second example u mentioned as follow:
=====================================

$n = 3;
$n * $n++

from 3 * 3 into 3 * 4. Post- operations operate on a variable after it has been 'checked', but it doesn't necessarily state that it should happen AFTER an evaluation is over (on the contrary, as a matter of fact).

===========================================

everything works correctly but one sentence should be modified:

"from 3 * 3 into 3 * 4" should be "from 3 * 3 into 4 * 3"

best regards~ :)
up
10
petruzanauticoyahoo?com!ar
16 years ago
Regarding the ternary operator, I would rather say that the best option is to enclose all the expression in parantheses, to avoid errors and improve clarity:

<?php
print ( $a > 1 ? "many" : "just one" );
?>

PS: for php, C++, and any other language that has it.
up
7
denzoo at gmail dot com
16 years ago
To jvm at jvmyers dot com:
Your first two if statements just check if there's anything in the string, if you wish to actually execute the code in your string you need eval().
up
8
oliver at hankeln-online dot de
21 years ago
The short-circuiting IS a feature. It is also available in C, so I suppose the developers won?t remove it in future PHP versions.

It is rather nice to write:

$file=fopen("foo","r") or die("Error!");

Greets,
Oliver
up
7
shawnster
17 years ago
An easy fix (although intuitively tough to do...) is to reverse the comparison.

if (5 == $a) {}

If you forget the second '=', you'll get a parse error for trying to assign a value to a non-variable.
up
2
Bichis Paul
7 years ago
Regarding 12345alex at gmx dot net's example:

I think you miss the identical equal documentation line from: http://php.net/manual/en/language.operators.comparison.php

$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

Try:
print array() === NULL ? "True" : "False";

Check this:
var_dump(is_null(array()));
up
2
antickon at gmail dot com
12 years ago
evaluation order of subexpressions is not strictly defined for all operators

<?php
function a() {echo 'a';}
function
b() {echo 'b';}
a() == b(); // outputs "ab", ie evaluates left-to-right

$a = 3;
var_dump( $a == $a = 4 ); // outputs bool(true), ie evaluates right-to-left
?>

this is not a bug: "we [php developers] make no guarantee about the order of evaluation".
See https://bugs.php.net/bug.php?id=61188
To Top