Re: Unexcepted $this

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

 



Philip Thompson wrote:
On Mar 10, 2008, at 8:32 AM, Ray Hauge wrote:

Murat BEŞER wrote:
I can't under stood but PHP gaves me an error:
"UnExcepted $this" for " || $this->getFileExtension($file) == 'jpg' "
When I removed jpg extension check it's okay... PHP script runs well.
What is the problem :)
public function loadImages($folder) {
       $result = $this->filemanager->fecthFiles($folder);
       $images = array();
       if (sizeof($result)>=1 && $result !== false) {
           foreach ($result as $file) {
if ($this->getFileExtension($file) == 'gif' || $this->getFileExtension($file) == 'png' || $this->getFileExtension($file) == 'jpg') {
                   $images[] = array('name'=>$file);
               }
           }
       }
       return $images;
   }

Try storing the value in a variable first. It'll also have the side-effect of being marginally faster too.

Good call.


$extension = $this->getFileExtension($file);

if ($extension == 'gif' || $extension || 'png' || $extension == 'jpg') {

Correction:

... $extension == 'png' ...

Personally, I like the in_array() version better - it cleaner and you can expand upon it more more easily.

$extensions = array('gif, 'png', 'jpg' [, 'm4a'[, 'etc']]);
if (in_array($extension, $extensions)) { ... }


    // do something
}

That might help, but I would think that the way you had it would also work. Let us know what happens when you use the variable like I showed above.

Thanks,
--
Ray Hauge


HTH,
~Philip

"Personally, most of my web applications do not have to factor 13.7 billion years of space drift in to the calculations, so PHP's rand function has been great for me..." ~S. Johnson



Thanks for the catch. I work from home and I hadn't had my coffee yet in the morning. I usually check my news and email right after I get up and before I go to work :)

I have been starting to use in_array more often as well. Far too many times I'll have to go back and add more options later, then the IF statement gets huge. Someday I'll kick the habit for good though.

Oh, it might also be a bit easier to store the acceptable extensions in an array before the IF statement

$ext = $this->getFileExtension($file);
$acceptableExtensions = array('png', 'gif', 'jpg');

if ( in_array($ext, $acceptableExtensions) ) {
	// do something
}

That way the conditional doesn't grow as more and more values get added.

--
Ray Hauge
www.primateapplications.com

--
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