note 11883 modified in language.expressions by felipe

From: Date: Tue, 22 Jul 2008 18:59:59 +0000
Subject: note 11883 modified in language.expressions by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142538@lists.php.net to get a copy of this message
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. --was-- Manual defines "expression is anything that has value", Therefore, parser will give error for following code. ($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, ($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. http://php.net/manual/en/language.expressions.php

« previous php.notes (#142538) next »