15

I've been working with PHP for quite a while now, but this was always a mystery to me, the correct use of the exclamation mark (negative sign) in front of variables.

What does !$var indicate? Is var false, empty, not set etc.?

Here are some examples that I need to learn...

Example 1:

$string = 'hello';
$hello = (!empty($string)) ? $string : '';

if (!$hello)
{
    die('Variable hello is empty');
}

Is this example valid? Would the if statement really work if $string was empty?

Example 2:

$int = 5;
$count = (!empty($int)) ? $int : 0;

// Note the positive check here
if ($count)
{
   die('Variable count was not empty');
}

Would this example be valid?

I never use any of the above examples, I limit these if ($var) to variables that have boolean values only. I just need to know if these examples are valid so I can broaden the use of the if ($var) statements. They look really clean.

Thanks.

Dennis
  • 6,954
  • 8
  • 53
  • 97
aborted
  • 4,504
  • 11
  • 59
  • 118
  • You should look up [Logical Operators](http://php.net/manual/en/language.operators.logical.php) in PHP – My Head Hurts Oct 23 '12 at 12:01
  • You are not answering my question with that link. No one below does. I need to know if the above examples are valid or not. – aborted Oct 23 '12 at 12:06
  • As mentioned by @Evert in his answer, this is the basics, you need to understand what is happening. There is no point us just telling you what the answer is. Try this link [here](http://www.homeandlearn.co.uk/php/php3p10.html) it may explain better. If you still have questions then we can explain further if you still struggle to understand these concepts. – My Head Hurts Oct 23 '12 at 12:10
  • 1
    @MyHeadHurts Tell me how it can't be the point to tell me what the answer is? Isn't this site supposed to work that way? Someone asks, the ones who know answer. I didn't post here to make myself look stupid, but I asked for clarification on a thing I already know, but looking to extend it. It would have been simpler to say "Example 1 is valid, example 2 is valid" instead of telling me to read logical operators. Afaik, no one really read the question to the end, except bhovhannes, which saved me lots of headache with that link. – aborted Oct 23 '12 at 12:28
  • 1
    I'm sure everyone read your question to the end, it was just a poorly asked question. It reads like something that has been received as part of a junior programmers job application process which checks that you at least have an understanding of basic programming logic. If you had written something like "*I understand that this does this, but I'm not sure why it doesn't do that*" then you would have received better answers and maybe even some upvotes – My Head Hurts Oct 23 '12 at 12:42
  • 1
    I think that I explained it very well. It doesn't matter if it's a poorly asked question or if I looked like a junior programmer, the fact is that if you came here to answer, answer, don't give lectures on how to ask a question if the question is clear enough. I asked if those two examples are valid. Period. – aborted Oct 23 '12 at 13:48

4 Answers4

21

if(! $a) is the same as if($a == false). Also, one should take into account that type conversion takes place when using == operator.
For more details, have a look into "Loose comparisons with ==" section here. From there it follows, that for strings "0" and "" are equal to FALSE ( "0"==false is TRUE and ""==false is TRUE, too).

Regarding posted examples:
Example 1
It will work, but you should note, that both "0" and "" are 'empty' strings.

Example 2
It will work

bhovhannes
  • 4,771
  • 1
  • 20
  • 36
  • 1
    Finally someone that actually understood what I meant. The table `Comparisons of $x with PHP functions` answered my question. From there we can understand that `$x = "php";` then `if($x)` would be `true`. Those who said that I need to learn basic stuff, please try to read the question carefully next time and first understand what I'm asking. Don't rush to put an answer that isn't actually answering my (or someone else's) question. Thanks a lot bhovhannes. – aborted Oct 23 '12 at 12:20
2

The ! negates. true becomes false, and anything that evaluated to false becomes true.

If you're writing PHP and you don't know all the operators by heart.. you should not write a single line of code until you know them by heart:

http://php.net/manual/en/language.operators.php

These are absolute basics.

Evert
  • 75,014
  • 17
  • 95
  • 156
  • 1
    Well, read the last paragraph. I just needed an explanation if I could use it more than just for booleans. – aborted Oct 23 '12 at 12:04
0

It's a boolean tester. Empty or false.

William The Dev
  • 495
  • 1
  • 6
  • 15
0

It's the not boolean operator, see the PHP manual for further detail.

mensi
  • 8,850
  • 1
  • 29
  • 42