Ryan R Posted November 17, 2011 Share Posted November 17, 2011 Hi, I find that "(!flag)" has been confusing throughout the time that I am reading the book. If you look at the example below: $header = false; // Loop through the results: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // If the header hasn't been shown, create it: if (!$header) { ......?> I think that "!$header" means TRUE since it's started with "false" default value. But you described that "!$header" means FALSE.. I think I need a little bit of more explanation to understand clearly. thanks. Link to comment Share on other sites More sharing options...
Jonathon Posted November 17, 2011 Share Posted November 17, 2011 Not exactly $header = FALSE; Basically sets the header to false, so it doesn't exist/have a value and is good practice if you want to make sure that something hasn't been accidentally or maliciously set. So if(!$header) means "if $header has not been set" i.e $header has no value. Which in your example is true because $header has no value* as its false, so $header has no value or doesn't exist, therefore Larry wants you to create the $header part. Follow? * - has no value probably isn't exactly correct as it does have a value of FALSE but it's not a true value of some kind. 1 Link to comment Share on other sites More sharing options...
Craig-UK Posted November 18, 2011 Share Posted November 18, 2011 As Jonathan has said. if ($header) { Checks whether "$header" has a valid value (i.e. not FALSE or empty), so "!$header" does the opposite. if (!$header) { Just like above but this time checks whether "$header" is empty or FALSE, if so do between { }. If doing this way theres usually a else clause. 1 Link to comment Share on other sites More sharing options...
Ryan R Posted November 21, 2011 Author Share Posted November 21, 2011 Thanks guys! It helps me to understand it better. I have one more thing I want to sort out. If I want to apply to another example like this: $live = false; <?php if(!$live){ define('....', '.....'); }else{ define('..--','...--'); } !$live means "not live". Therefore $live is "live". Interestingly even though its value is false, it represents that "it's live". Is it because the value doesn't exist so that it can be on line without being affected to this flag ? Thanks. Link to comment Share on other sites More sharing options...
Jonathon Posted November 21, 2011 Share Posted November 21, 2011 You can do that style of if statement, I do it myself for deciding whether we're locally or live. Interestingly even though its value is false, it represents that "it's live". in your example $live is "not live" and !$live also equates to "not live" $live will only actually be "live" is you set $live = true; Link to comment Share on other sites More sharing options...
Recommended Posts