표현식

표현식은 PHP에서 가장 중요한 구성요소다. PHP에서 쓰는 거의 대부분이 표현식이다. 표현식을 정의한 가장 단순하고 가장 정확한 말은 "모든것이 값을 갖는다"는 것이다.

표현식의 가장 기본적인 형태가 상수와 변수이다. "$a = 5"라고 쓰면, $a에 '5'를 지정하는것이다.'5'는 확실히 5란 값을 갖는다. 다르게 표현하면 '5'는 5란 값을 갖는 표현식이 된다 (이경우에, '5'는 정수형 상수이다)

이렇게 지정하고 나면 $a의 값은 5가 될것이란 것도 기대할수 있을것이다. 그래서 $b = $a라고 쓴다면, $b = 5를 쓴것과 같이 동작할것이라고 기대할수 있을것이다. 다른말로, $a는 또한 5란 값을 갖는 표현식이다. 모든것이 제대로 작동한다면, 기대하던대로 될것이다.

표현식의 약간 더 복잡한 예가 함수이다. 다음 함수를 예로 들어보자.

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

함수의 개념에 익숙하다면 (그렇지 않으면, 함수에 관한 장을 참고) $c = foo()로 쓰는것이 $c = 5와 같을것이라고 생각할것이다. 옳은 생각이다. 함수는 반환된 값을 갖는 표현식이다. foo()가 5를 반환하기때문에 'foo()'표현식의 값은 5가 된다. 함수는 대체로 정적인 값보다는 계산값을 반환한다.

물론, PHP에서 값은 정수가 되어야 하는것은 아니다.. PHP에서는 4가지 스칼라 값 자료형을 지원한다: integer 값, 부동소수점 값(float), string 값, boolean 값. (스칼라 값은 배열과 다르게, 더 작게 쪼갤수 없는 값을 말한다) PHP는 두가지 조합형(스칼라값이 아닌)을 지원한다: 배열과 객체. 이 두가지 값형은 변수나 함수에서 넘기는 값으로 지정될수 있다.

PHP는 여기에 더하여, 많은 다른 언어들이 하는 방식의 표현식을 받아들입니다. PHP는 거의 모든것이 표현식이라는 관점에 입각한 표현식-지향 언어이다. 전술했던 예제 '$a = 5'를 보자. 이 식과 연관된 값은 '5'라는 정수상수 두개라는것과 $a는 5로 수정이 된다는 것을 알수 있다. 그러나 진실은 여기에 연관되어있는 것이 하나의 추가값이라는것이고, 이 추가된 값이 지정되어지는 값 자체라는 것이다. 지정 자체는 지정값 5만 적용된다. 실제로, '$a = 5'라는 식은 그것이 무엇을 하든 5라는 값을 갖는 표현식이라는 것이다. 따라서, '$b = ($a = 5)'라는 식은 '$a = 5; $b = 5'를 쓰는것과 같다 (세미콜른은 구문의 끝을 표시한다). 지정연산은 오른쪽에서 왼쪽방향으로 해석되므로 '$b = $a = 5'라고 쓸수 있다.

표현식-지향의 또다른 좋은 예는 전처리(pre)/후처리(post)되는 증가와 감소 연산이다. PHP 사용자와 다른 언어 사용자는 변수++과 변수--의 부호표시에 익숙할것이다. 이런 부호표시는 증가와 감소 연산자이다. PHP/FI 2에서 '$a++'문은 값을 갖지않는다 (즉, 표현식이 아니다), 그래서 어떤수단으로도 특정값을 지정하거나 사용할수 없다. PHP는 C처럼 이런 표현식을 사용할수 있어서 증가/감소 연산 능력을 확장시켰다. PHP는 C처럼 두가지 연산형이 존재한다 - 전처리-증가 와 후처리-증가. 전처리 증가와 후처리 증가 모두 변수를 증가시키고 변수에 적용하므로, 변수에 미치는 영향은 동일합니다. 차이점은 증가 표현식의 값에 달려있다. 전처리-증가, 즉 '++$변수' 는 값을 증가시켜 평가합니다. (PHP는 처음 값을 읽기 전에 값을 증가시킨다, 따라서 이름이 '전처리-증가'가 되는것이다) 후처리-연산, '$변수++'는 값을 증가시키기 전의 $변수의 처음값을 평가합니다. (PHP는 그 값을 읽은 후에 그 값을 증가시킨다, 그래서 '후처리-증가'가 되는것이다)

가장 흔한 표현식 형태는 비교 표현식입니다. 이 표현식은 FALSETRUE로 평가합니다. PHP는 >(초과), >=(이상), ==(같음), !=(같지 않음), <(미만), <=(이하)를 지원합니다. 또한 엄격한 등가 연산자를 지원합니다: ===(같고 같은 자료형)과 !==(같지 않거나 다른 자료형). 이 표현식은 보통 if 구문과 같은 조건부 실행문에 사용됩니다.

이 장에서 취급할 제일 마지막 표현식의 예는 조합된 연산자-지정 표현식이다. $a를 1만큼 증가시키고자 한다면 단순히 '$a++'이나 '++$a'로 쓸수 있다는것을 이미 알고 있을것이다. 그러나 하나 이상의 값을 증가시키고자 한다면, 예를 들어 3을 증가시키고자 한다면 어떻게 해야 할까? '$a++'을 여러번 쓸수도 있다. 그러나 이런 식으로 쓰는것은 효과적이거나 편한 방법이 아니라는것을 쉽게 알수 있다. 좀더 평범하게 표현하면 '$a = $a + 3'이다. '$a + 3'은 $a에 3을 더한 값이 계산 되고 $a로 그 값이 지정된다. 그래서 $a에 3을 증가시킨 결과가 된다. PHP에서는 C와 같은 다른 언어처럼 더 짧은 형태로 쓸수있다. 그래서, 좀더 이해하기 쉽고 좀더 빨리 이해할수있다. $a의 현재 값에 3을 더하는 것은 '$a += 3'으로 쓸수 있다. 이 표현식의 의미는 정확히 '$a값을 취해 그 값에 3을 더하고 다시 $a에 계산된값을 지정하라'이다. 좀더 짧고 명확할 뿐만 아니라, 좀더 빠른 수행이 이루어진다. 일반적인 표현식의 값처럼 '$a += 3'의 값은 지정값이다. 3이 아니라, $a에 3을 더한 값이라는 것에 주의하라 (이값은 $a에 지정되는 값이다) 어떠한 두개연속의 연산자도 이런 연산자-지정 방식으로 사용될수 있다. 예를 들면, '$a -= 5' ($a의 값에서 5를 뺀것), '$b *= 7' ($b의 값에 7을 곱한것) 등등이 쓰일수 있다.

다른 언어에서 본적이 없다면 이상하게 보이는 표현식이 있다. 3중 조건적 연산자이다:

<?php
$first 
$second $third
?>

first 에 속하는 표현식의 값이 TRUE (non-zero)이면 second 에 속하는 표현식이 적용되고, 이것이 조건적 표현식의 결과가 된다. 이 경우가 아니면, third 에 속하는 표현식이 적용되고, 그 값이 된다.

다음 예제 코드는 좀더 일반적으로 사용된 전처리/후처리-증가와 표현식을 이해하는데 도움이 될것이다.

<?php
function double($i)
{
    return 
$i*2;
}
$b $a 5;        /* $a와 $b에 5라는 값을 지정함 */
$c $a++;          /* 후처리-증가, $a 의 원래값을 적용함
                       $c는 (5)가 됨 */
$e $d = ++$b;     /* 전처리-증가, $b가 증가된 값이 적용됨
                       $d와 $e는 (6)이 됨 */

/* 여기서, $d와 $e는 6과 같다 */

$f double($d++);  /* 증가되기 전의 $d값의 두배가 적용됨
                       $f는  2*6 = 12가 됨 */
$g double(++$e);  /* 증가된 후의 $e값의 두배가 적용됨
                       $g는  2*7 = 14가 됨 */
$h $g += 10;      /* 우선, $g는 10만큼 증가되어 24가 됨.
                       지정된 값 (24)가 $h로 지정된다.
                       $h도 24값으로 지정된다 */
?>

몇몇 표현식은 구문으로 간주할 수 있습니다. 이런 경우는 구문이 'expr' ';'의 형태를 갖는다. 즉, 세미콜른이 표현식 뒤에 온다. '$b=$a=5;'에서 $a=5는 유효한 표현식이다. 그러나 그자체가 구문이 되지는 않는다. 하지만, '$b=$a=5;'는 유효한 구문이다

마지막으로 언급할 만큼 가치있는것이 표현식의 진리값이다. 많은 사건중, 주로 조건적 수행과 루프에서, 표현식의 특정 값에 흥미있지는 않을것이다. 그러나 그 값이 TRUE인지 FALSE인지는 중요하다. 상수 TRUEFALSE (대소문자를 구별하지 않음)는 두가지 논리값이다. 필요하면, 표현식은 자동적으로 논리값으로 변환된다. 더 자세한 정보는 자료형 변환을 참고할것.

PHP는 표현식의 완벽하고도 강력한 구현방법을 제공한다. 그런 방법을 완전히 문서화하는 것은 이 매뉴얼의 범위를 넘는다. 위 예제 코드들은 표현식이 무엇인지에 대한것과 유용한 표현식을 구축할수 있는 방법에 대한 아이디어를 줄것이다. 이후 매뉴얼에서는 유효한 PHP 표현식을 expr라고 쓰겠다.

add a note add a note

User Contributed Notes 21 notes

up
44
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
42
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
11
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
12
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
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 wont remove it in future PHP versions.

It is rather nice to write:

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

Greets,
Oliver
up
8
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
6
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
4
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
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
1
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
up
2
tom at darlingpet dot com
19 years ago
Something I've noticed with ternary expressions is if you do something like :

<?= $var=="something" ? "is something" : "not something"; ?>

It will give wacky results sometimes...

So be sure to enclose the ternary expression in parenthesis when ever necessary (such as having multiple expressions or nested ternary expressions)

The above could look like:

<?= ($var=="something") ? "is something" : "not something"; ?>

It's also a good idea to use parenthesis when using something SIMILAR to:

<?php
echo (trim($var)=="") ? "empty" : "not empty";
?>

In some cases other than the <?= ?> example, not placing the entire expression in appropriate parenthesis might yield undesirable results as well.. but I'm not quite sure.
up
0
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
1
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
1
anthony at n dot o dot s dot p dot a dot m dot trams dot com
23 years ago
The ternary conditional operator is a useful way of avoiding inconvenient if statements.  They can even be used in the middle of a string concatenation, if you use parentheses. 

Example:

if ( $wakka ) {
  $string = 'foo' ;
} else {
  $string = 'bar' ;
}

The above can be expressed like the following:

$string = $wakka ? 'foo' : 'bar' ;

If $wakka is true, $string is assigned 'foo', and if it's false, $string is assigned 'bar'.

To do the same in a concatenation, try:

$string = $otherString . ( $wakka ? 'foo' : 'bar' ) ;
up
0
richard at phase4 dot ie
18 years ago
Follow up on Martin K. There are no hard and fast rules regarding operator precedence. Newbies should definitely learn them, but if their use results in code that is not easy to read you should use parentheses. The two important things are that it works properly AND is maintainable by you and others.
up
0
Anonymous
18 years ago
I don't see why it is necessary here to explain pre- and post- incrementing.

This is something that will confuse new users of PHP, even longer time programmers will sometimes miss a the fine details of a construct like that.

If something has a side-effect it should be on a line of it's own, or at least be an expression of it's own and not part of an assignment, condition or whatever.
up
-2
jvm at jvmyers dot com
16 years ago
<?php
// Compound booleans expressed as string args in an 'if' statement don't work as expected:
//
//    Context:
//        1.  I generate an array of counters
//        2.  I dynamically generate a compound boolean based on selected counters in the array
//                Note: since the real array is sparse, I must use the 'empty' operator
//        3.  When I submit the compound boolean as the expression of an 'if' statement,
//            the 'if' appears to resolve ONLY the first element of the compound boolean.
//    Conclusion: appears to be a short-circuiting issue

$aArray = array(1,0);

// Case 1: 'if' expression passed as string:

$sCondition = "!empty($aArray[0]) && !empty($aArray[1])";
if (
$sCondition)
{
    echo
"1. Conditions met<br />";
}
else
{
    echo
"1. Conditions not met<br />";
}

// Case 1 output:  "1. Conditions met"

// Case 2: same as Case 1, but using catenation operator

if ("".$sCondition."")
{
    echo
"2. Conditions met<br />";
}
else
{
    echo
"2. Conditions not met<br />";
}

// Case 2 output:  "2. Conditions met"

// Case 3: same 'if' expression but passed in context:

if (!empty($aArray[0]) && !empty($aArray[1]))
{
    echo
"3. Conditions met<br />";
}
else
{
    echo
"3. Conditions not met<br />";
}

// Case 3 output:  "3. Conditions not met"

// jvm@jvmyers.com
?>

PS: the bug folks say this "does not imply a bug in PHP itself."  Sure bugs me!
up
-2
nabil_kadimi at hotmail dot com
17 years ago
Attention! php will not warn you if you write (1) When you mean (2)

(1)
<?
if($a=0)
    echo "condition is true";
else
    echo "condition is false";
//output: condition is false
?>

(2)
<?
if($a==0)
    echo "condition is true";
else
    echo "condition is false";
//output: condition is true
?>
up
-9
george dot langley at shaw dot ca
16 years ago
Here's a quick example of Pre and Post-incrementation, in case anyone does feel confused (ref anonymous poster 31 May 2005)

<?PHP
echo "Using Pre-increment ++\$a:<br>";
$a = 1;
echo
"\$a = $a<br>";
$b = ++$a;
echo
"\$b = ++\$a, so \$b = $b and \$a = $a<br>";
echo
"<br>";
echo
"Using Post-increment \$a++:<br>";
$a = 1;
echo
"\$a = $a<br>";
$b = $a++;
echo
"\$b = \$a++, so \$b = $b and \$a = $a<br>";
?>

HTH
up
-10
12345alex at gmx dot net
18 years ago
this code:
    print array() == NULL ? "True" : "False";
    print " (" . (array() == NULL) . ")\n";

    $arr = array();
    print array() == $arr ? "True" : "False";
    print " (" . (array() == $arr) . ")\n";

    print count(array()) . "\n";
    print count(NULL) . "\n";

will output (on php4 and php5):
    True (1)
    True (1)
    0
    0

so to decide wether i have NULL or an empty array i will also have to use gettype(). this seems some kind of weird for me, although if is this is a bug, somebody should have noticed it before.

alex
up
-17
Martin K
18 years ago
At 04-Feb-2005 05:13, tom at darlingpet dot com said:
> It's also a good idea to use parenthesis when using something SIMILAR to:
>
> <?php
> echo (trim($var)=="") ? "empty" : "not empty";
>
?>

No, it's a BAD idea.

All the short-circuiting operators, including the ternary conditional operator, have LOWER precedence than the comparison operators, so they almost NEVER need parentheses around their subexpressions.

Inserting the parentheses suggested above does not change the meaning of the code, but their use misleads inexperienced programmers to expect that things like this will work in a similar manner:

<?php
function my_print($a) { print($a); }
my_print (trim($var)=="") ? "empty" : "not empty";
?>

when of course it doesn't.

Rather than worrying that code doesn't work as expected, simply learn the precedence rules (http://www.php.net/manual/en/language.operators.php) so that one expects the right things.
To Top