RE: [PHP] Re: ramble was [PHP] Not sure of my error

From: Date: Sat, 18 Nov 2000 01:45:46 +0000
Subject: RE: [PHP] Re: ramble was [PHP] Not sure of my error
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-26003@lists.php.net to get a copy of this message
> > In my opinion, if you don't want to have errors on undeclared > > variables, then the only way to be sure you are not going to get > > errors is to declare them! > > What about just checking with > > if (isset($foo)) { > if (!$foo) > echo "nothing here"; > } I believe that it just depends on your application. I try and code so that I _DO_ know what is set and what is not. When I am up against odds and I need to do something like this, here is what I do (sometimes) and here is my reasoning. I use strlen() strlen() takes in a string value. Now, lets say that foo does not exist. strlen() expects a string, so you are sending it NULL. It will cound how long NULL is and we would expect that to be zero, right? Now, lest say that $foo is an integer. strlen() takes a string, the $foo integer gets turned into a string, and we all know that any number has at least one digit, that one digit gets turned into one character. Thus the return value of strlen has to be greater than 0. Now, lets say that $foo is a string. strlen() takes that string, and if it is not empty, there are characters in it and that would make it return greater than zero. If foo was declared a string and it is empty, then it is NULL and NULL is zero characters, so strlen returns a zero. Now, this all works out very nice, because we don't care what value greater than zero strlen() returns, all we care is that a greater than zero value is TRUE. And, alternatively, if strlen() returns zero, again we don't care except that zero is FALSE. See how nicely this works. But, I stand by what I said about declaring your veriables. You are a programmer, and you should be able to -- without a doubt -- know what your code is going to do, so if you can know what it is going to do by declaring your variables, then that should be what you do. I stress my self to produce bug free code. Sometimes, they get in -- and when they do, most of the time it is because someone elses code that I interface with is buggy. But for the most part, you have to be at ZEN with your software. You have to be able to KNOW what it does, not THINK you know what it does. PHP is extreamly forgiving, and on that note, you should take that forgiveness and know what it can and can't do -- because this learning curve is WAY lower than nearly any other language, but PHP is so powerful -- it is almost too good to be true! I want to give my respect to Rasmus and the PHP team for creating originally what has become arguably one of the best languages (not just web languages) available anywhere!!! -jeremy brand Jeremy Brand :: Sr. Software Engineer :: 408-245-9058 :: jeremy@nirvani.net http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html for more - - - - - - - - - - - - - - - - - - - - - - - - - I am pleased to see that we have differences. May we together become greater than the sum of both of us. -- Surak of Vulcan, "The Savage Curtain", stardate 5906.4

« previous php.general (#26003) next »