Re: "!" in front of a variable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jay Blanchard wrote:
[snip]
I've got some code from someone else I've inherited and need to sort out
some problems with.  The programmer that wrote it originally was much
better than I and programmed a little over my head to say the least.

One function that I've come across that has 5 variables as input:

function($var1,$var2,!$var2,$var3->cc,$var3->bcc);

The question I have is on the 3rd input variable, what does the "!" in
front of $var2 do to that variable?
[/snip]

Hmmm. Looks like a weird mistake, to be sure. If $var2 is boolean it should

I'd say it looks odd - but I wouldn't charcterise it as a mistake without
seeing the function and the code that calls it.

the ! is the logical NOT operator  - what Jay says is correct but I'd like to
add that $var2 would be negated regardless of whether the value of $var2 is a
boolean or not. how this is done internally has everything to do with the
php's auto-casting rules (i.e. the rules it uses to determine how/when to
automatically cast a variable of a given type to another type in order to
be able to use the value in a given context.

as an example, the following line fo code says " if $x is NOT set then do stuff":

	if (!isset($x)) { /*do stuff*/ }

check here for more info on logical operators:
	http://nl2.php.net/manual/en/language.operators.logical.php

also try running this code to get a feeling for what auto-casting does
in relation to the NOT operator:

// echo '<pre>' // uncomment this if running in a browser
class Test  {}
class Test2 { var $v; function Test2($v) { $this->v = $v; } }
$a = 1;
$b = 0;
$c = true;
$d = false;
$e = "";
$f = "non-empty-string";
$g = array();
$h = array(1,2,3);
$i = new Test();
$j = new Test2("test");
var_dump(!$a, !$b, !$c, !$d, !$e, !$f, !$g, !$h, !$i, !$j);

pass the opposite of its current state. Is $var2 a boolean? It is being
passed twice here, once AS and once AS NOT.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux