PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousExpressionsOperatori aritmeticinext
Last updated: Wed, 10 Jul 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

Capitolo 10. Operatori

Precedenza degli operatori

La precedenza di un operatore specifica come esso tenga legate assieme "strettamente" due espressioni. Per esempio, nell'espressione 1 + 5 * 3, la risposta � 16 e non 18 perch� l'operatore di moltiplicazione ("*") ha una precedenza pi� alta rispetto all'operatore di addizione ("+"). Le parentesi possono essere usate per forzare la precedenza, se necessario. Per esempio: (1 + 5) * 3 viene valutata 18.

La seguente tabella fornisce una lista della precedenza degli operatori con gli operatori a pi� bassa precedenza listati prima.

Tabella 10-1. Precedenza degli operatori

Associativit�Operatori
sinistra,
sinistraor
sinistraxor
sinistraand
destraprint
sinistra = += -= *= /= .= %= &= |= ^= ~= <<= >>=
sinistra? :
sinistra||
sinistra&&
sinistra|
sinistra^
sinistra&
non associativi== != === !==
non associativi< <= > >=
sinistra<< >>
sinistra+ - .
sinistra* / %
destra! ~ ++ -- (int) (float) (string) (array) (object) @
destra[
non associativinew

User Contributed Notes
Operatori
add a note about notes

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.


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 );


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.


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;
}


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.


09-Mar-2001 09:25

For type casting is described at



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


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.


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);


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.

add a note about notes
previousExpressionsOperatori aritmeticinext
Last updated: Wed, 10 Jul 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Jul 12 08:19:06 2002 CEST