This is a discussion on reading "<?" from a php file within the PHP Language forums, part of the PHP Programming Forums category; aa wrote: > Thanks, Roy, I felt I was missing something fundamental in PHP syntax. > Will you please explain ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
aa wrote:
> Thanks, Roy, I felt I was missing something fundamental in PHP syntax. > Will you please explain how > if(!$line=="<?") > is interpreted by PHP as > "if $line is zero equals <?" ? I.e. if(!$line= (0=="<?")) > > Where zero is implied in (!$line=="<?") ? Putting an exclamation-mark in front of a variable like that will test the value of the variable. I said zero, but I guess "false" would be more appropriate. I didn't mean the number 0, but zero as in nothing, none, null, nada - call it what you like, just be aware that PHP will probably want to call it false ;) if (!$test) { do_stuff(); } In this case, do_stuff() will be called if $test is false, meaning if $test is not set, if it contains the value 0, if it's set to null/false, or if it contains an empty string (e.g. '', or "" if you prefer). if (!$test == "some string") { do_stuff(); } In this case, do_stuff() will be called if $test is false and equal to "some string", which by it's very definition isn't false - "some string" can only be false if it's "", in which case it's no longer "some string", and there it goes around and around until we get dizzy and give up - PHP is smarter than us, so it just says "this won't work" and moves on. The statement will never evaluate to true, and do_stuff() will never happen. Basically, just keep it simple. Ask the question in plain english, and then translate it into PHP code. If you want to ask if $a equals $b, then ask if $a == $b. If you want to ask if $a doesn't equal $b, then ask if $a != $b. If you want to ask if $a is false, then you can either ask if !$a, or if $a == false. Roy W. Andersen -- ra at broadpark dot no / http://roy.netgoth.org/ "Hey! What kind of party is this? There's no booze and only one hooker!" - Bender, Futurama |
|
|||
|
"aa" <aa@virgin.net> wrote in message
news:41e52d6b$0$55486$ed2619ec@ptn-nntp-reader02.plus.net... > Thanks, Roy, I felt I was missing something fundamental in PHP syntax. > Will you please explain how > if(!$line=="<?") > is interpreted by PHP as > "if $line is zero equals <?" ? I.e. if(!$line= (0=="<?")) > > Where zero is implied in (!$line=="<?") ? > > "Roy W. Andersen" <roy-news@netgoth.org> wrote in message > news:34kl59F4btrgfU1@individual.net... > > aa wrote: > > > Thanks for offering other options. > > > However I would like to understand what is wrong with the one I am > using? > > > The litaruture does not say that fgets() is not suatable for reading > > > characters like "<" which migh happen in any text file. > > > > The problem is this line: > > > > > if (!$line=="<?") > > > > You're basically saying "if $line is zero equals <?", which always fails > > since $line isn't zero (nor would if equal <? if it ever was zero). > > > > Try this instead: if ($line != "<?") > > > > That one says "if $line isn't <?" which is exactly what you're trying to > > say. > > He didn't really state that clearly. He meant "is equal to" rather than "equals". This: if(!$line == "<?") Equates to: if( !($line) == "<?") As the '!' has higher precedence than the '=='. So, either use: if($line != "<?") Or use: if(! ($line == "<?")) (the former is better). -Noah |