PHP: Oper�torok - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousKifejez�sekAritmetikai oper�toroknext
Last updated: Fri, 30 Aug 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

Fejezet 11. Oper�torok

Oper�torok precedenci�ja

Az oper�torok precedenci�ja azt hat�rozza meg, hogy milyen "szorosan" k�t �ssze k�t kifejez�st. P�ld�ul az 1 + 5 * 3 kifejez�sben, a kifejez�s �rt�ke 16, �s nem 18, mert a szorz�s oper�tor�nak, a ("*")-nak nagyobb precedenci�ja van, mint az �sszead�s�nak ("+"). Z�r�jelek seg�ts�g�vel tetsz�leges precedenci�t lehet fel�ll�tani egy kifejez�sen bel�l, ha sz�ks�ges. P�ld�ul a (1 + 5) * 3 eredm�nye 18 lesz.

[Az asszociativit�s tal�n meg�r egy kis magyar�zatot. Ez azt hat�rozza meg, hogy az adott szinten lev� oper�torok egym�s ut�ni, z�r�jel n�lk�li haszn�lat�t hogyan �rtelmezi a ford�t�. Egy p�ld�n kereszt�l tal�n �rthet�bb� v�lik: 1-2+3 �rtelmezhet� (1-2)+3-nak (= 2), vagy 1-(2+3)-nak (= -4). Az el�bbi ki�rt�kel�s, amely balr�l jobbra asszociat�v, a helyes ebben az esetben. A m�sodik ki�rt�kel�s pedig jobbr�l balra asszociat�v - ilyen lenne p�ld�ul a hatv�nyoz�s, amely nincs a nyelvben implement�lva (erre szolg�l pow()). Ha a ** lenne a hatv�nyoz�s, akkor a 2**3**2 helyesen 2 a 9-en = 512 lenne. Vannak nem k�thet� (nem asszociat�v) oper�torok is, ilyenek az �sszehasonl�t� oper�torok. A PHP-ban teh�t nem �rtelmes a 2<$x<4 kifejez�s.]

Az al�bbi t�bl�zat az oper�torokat precedenci�juk szerint n�vekv� sorrendben tartalmazza.

T�bl�zat 11-1. Oper�torok precedenci�ja

asszociativit�soper�torok
balr�l jobbra,
balr�l jobbraor
balr�l jobbraxor
balr�l jobbraand
jobbr�l balraprint
balr�l jobbra= += -= *= /= .= %= &= |= ^= ~= <<= >>=
balr�l jobbra? :
balr�l jobbra||
balr�l jobbra&&
balr�l jobbra|
balr�l jobbra^
balr�l jobbra&
nem k�thet�== != === !==
nem k�thet�< <= > >=
balr�l jobbra<< >>
balr�l jobbra+ - .
balr�l jobbra* / %
jobbr�l balra! ~ ++ -- (int) (float) (string) (array) (object) @
jobbr�l balra[]
nem k�thet�new

User Contributed Notes
Oper�torok
add a note about notes
[email protected]
28-Mar-2000 06:47

In case you are just beginning to code in PHP and this is your first experience in programming, the Modulus (%) is a great way to code photo albums based on a table (it returns the remainder of integer divided by integer). The modulus helps to wrap data to next row based on the result of '$currentrow(from db)' % 'number of columns in database'. The only thing to look out for is that 1 % 4 is 1, 2 % 4 is 2, etc. and not zero or some other number, like you might think at first. Hope this will help some newbie programmers like myself.
[email protected]
15-Aug-2000 12:41

You can also use the floor() and ceil() functions to give the nearest integer values of a double. So, for instance, to get the integer part of the quotient, you can do this:

floor( $a / $b );

[email protected]
04-Jan-2001 01:54

Normally, a negative value modulo a positive number generates a negaive number, if you don't want that (for example, the number has to be between 0 and 9 and not negative) then you can use a function like this:
function mod($a,$b)
{
if ($a <= 0) return (int) mod($b-abs($a), $b); else return (int) $a % $b;
}
for example:
-2 % 10 = -2
but mod(-2, 10) will return 8.

[email protected]
25-Jan-2001 01:57

Yet another IWBNI posting...
I used the caret ( ^ ) "operator" in a truncate() function of mine with a result similar to a substraction. So, I had to implement an even weirder function which would work better. Should anyone know of a better way to do this, I thank in advance.

//The function expects a double $num and an int $pos indicating the decimal
//positions to allow

function truncate($num,$pos){
$pos=(int)$pos;
$m="1e$pos"; //this is our divider, ten at $pos power
$t=(double)$num*(double)$m; //just to be sure
$t=(int)$t; //yes, we need to cut superfluous decimals
$t=(double)$t/(double)$m;
return $t;
}

[email protected]
09-Mar-2001 07:38

There are some operator descriptions missing. (I'm not sure what is considered as operator in PHP, but anyway)

::
Scope resolution operator for class

->
Member selection operator for class

new
Object creation operator

?:
It is described in Comparison Operator Section. I think it worth to make a new sub section for ternary operator.

[]
I think Array "[]" is considered as operator. It mentioned in Operator Precedence Section as [, but no description. (Or is there any other use for single "["?)

()
Grouping expressions. It might not considered as operator in PHP, but it is better to be included at least in Operator Precedence section.

{}
Resolve ambiguous reference to variables. For example, {} is needed to resolve $array1['array_stored_as_array_element']['value']. {} is also needed to resolve ambiguity such as echo " $array_3d[1][2][3] "
It might not considered as operator in PHP, but it is better to be included at least in Operator Precedence section.

&
Reference operator. There is a section for reference, but it is better to be explained briefly in this section and direct reader to Reference section.

(type)
Cast operator like (int)$v to get integer value. I suppose these are operators also. Mentioned in Precedence section, but no description.

+= -= *= /= .= %= &= |= ^= ~= <<= >>=
Some assignment operators that are supported are not described. Mentioned in Precedence section, though.

print
Looks like "print" is considered as operator in PHP. (I thought "echo" is) It mentioned in Precedence section, but no description. Description for these may be helpful for programmers to distinguish what is "expression" and what is not.

For some reason, PHP4.0.4pl1 dose not allow. (I posted as bug, I got reply "echo" is not a valid expression. Therefore, PHP complained for following code as a parse error)

($val) ? echo('true') : echo('false');

but PHP does ALLOW

($val) ? print('true') : print('false');
($val) ? include('true.inc') : include('false.inc');

I'm confused with these. i.e. What is a expression, what is not an expression.

If there are other operators like "print" (maybe echo?), all of them should be described or mentioned, at least, in this section. (I think it would be nice to have a section describing what is statements/language construct)

It is not about operator, but it would be nice to have BNF of PHP (Zend Script Engine) as a reference.

[email protected]
09-Mar-2001 09:25

For type casting is described at



(I suppose cast is operator, isn't it?)

[email protected]
31-Jul-2001 08:38

There is no power-operator. Use pow() instead.

See:


There might come a power-operator in a future release, but that is still being discussed

mahmoud.at.infoscience.it
12-Dec-2001 03:24

You can use the shorthand:

$a = 5;
$a += 10; // $a is now 15
$a -= 3; // $a is now 12

php.at.michrev.com
08-Mar-2002 08:42

If this doesn't work:
($val) ? echo('true') : echo('false');
...you should use this:
if($val) { echo('true'); } else { echo('false'); }
...or, better yet, this:
echo($val ? 'true' : 'false');
...because $val, 'true', and 'false' are all expressions, but echo() is not.

The "expr?expr:expr" operator is used to select between expressions in the middle of a command. If you need to select between different commands instead, it's probably better to use if() instead. That's what it's there for.

[email protected]
15-Apr-2002 03:59

The PHP modulus function will (interestingly) not return the decimal portion of the result. I wrote a little function that will do so, and mimics the results obtained by using the Windows calculator mod function:

function modphp($x, $y, $z) {

for ($y = 0; $y <= abs($z); $y++) {
$w = abs($z) - (abs($x) * $y);
if ($w < abs($x)) {
break;
}
}

if ($z < 0) {
return(-$w);
} else {
return($w);
}

}

Here's how to pull the answer out of the mod function above:

$answer = modphp($var_x, $var_y, $var_z);

[email protected]
09-Jun-2002 11:56

Remember that when you're reading from a socket, all output is of type string.

Use ord() on each 'character', to get the byte values and use bitwise arithmetics.

18-Jun-2002 10:21
The =& operator can be found in section 14 -- returning references.
[email protected]
04-Aug-2002 10:53

It appears that the shortcut logical operators &&= and ||= are not supported. Drag. :(
28-Aug-2002 08:43
It should be noted that PHP's ?: operator associates left to right. In C and in C++, the ?: operator associates right to left. Thus:

1 ? 2 : 3 ? 4 : 5;

has the value 4 in PHP and the value 2 in C and in C++. I find it curious that they would reverse the associativity of an operator from its "accepted" sense, but yet they didn't fix the precedence of the bitwise operators. Thus:

if(A & 7 == 5)

groups like:

if(A & (7 == 5))

It is a trap for the unlucky.

add a note about notes
previousKifejez�sekAritmetikai oper�toroknext
Last updated: Fri, 30 Aug 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Aug 31 06:19:44 2002 CEST