Winamp & Shoutcast Forums

Winamp & Shoutcast Forums (http://forums.winamp.com/index.php)
-   General Discussions (http://forums.winamp.com/forumdisplay.php?f=1)
-   -   PHP Functions (http://forums.winamp.com/showthread.php?t=185497)

ryan 4th July 2004 17:56

PHP Functions
 
I'm trying to create a function that will let me have as many arguments as I want, that does the same thing as empty() (returns true if any of the values are empty, false if they're all not empty)

PHP Code:

function empty_extended() {
$arguments func_get_args();
foreach(
$arguments as $value) {
    if(empty(
$value)) { $result "true"; }
    else { 
$result "false"; }
}
if(
$result "true") { return true; }
else { return 
false; }


Is what I've been messing with for a while, but it always returns false.

Any help would be appreciated.

baafie 4th July 2004 21:22

PHP Code:

function empty_extended() 
{
    
$arguments func_get_args();
    
    foreach(
$arguments as $value
    {
            if(empty(
$value)) return true;
    }
    return 
false;


Note that if the function is called with no arguments, the function will return false anyway. So, you may want to change it to this..

PHP Code:

function empty_extended() 
{
    if (
func_num_args() == 0) return true;
    
    
$arguments func_get_args();
        
    foreach(
$arguments as $value
    {
            if(empty(
$value)) return true;
    }
    return 
false;



ryan 5th July 2004 02:29

Much thanks baaf ^__^

*Gives you a cookie*

griffinn 5th July 2004 13:55

I prefer this incomprehensible one-liner:
PHP Code:

function empty_extended() {
  return (
array_reduce(func_get_args(),create_function('$i,$j','return $i+(empty($j)?0:1);'),0)!=func_num_args());


(No kidding. It works. But it's computationally more expensive than what you already have.)

xzxzzx 6th July 2004 22:55

Can I play too?

PHP Code:

function empty_extended() 

    foreach(
func_get_args() as $value) { 
        if(empty(
$value)) return true;
    } 
    return (
func_num_args() == 0);



ryan 7th July 2004 00:07

This would make a fun contest :P

xzxzzx 7th July 2004 00:32

Mine would win the "least computationally expensive" award, I'm pretty sure.

griffinn 7th July 2004 03:25

PHP Code:

function empty_extended() {
  
$args func_get_args();
  return empty(
$args) || (array_search(FALSE,$args) !== FALSE);


This function terminates earlier if called with no arguments; I win.

xzxzzx 7th July 2004 08:36

PHP Code:

function empty_extended(){
    return !(
$a func_get_args()) || in_array(false$a);


Bow to the master, foolish mortals. :p

Mine is the most concise, and the least computationally expensive out of those on this thread.

Beat that. :D

mark 7th July 2004 08:47

10 Party
20 Get started
30 goto 10


i win

griffinn 7th July 2004 10:08

/me bows to xzxzzx while muttering, "I'll get you next time, grrr..." under the breath

But well done. :)

ryan 7th July 2004 10:57

PHP Code:

function empty_extended() {
//Function coming soon


You guys are good :)

baafie 7th July 2004 11:03

PHP Code:

function empty_extended($arg 0){
    return !
$arg || !($a func_get_args()) || in_array(false$a);


Slightly bigger but (probably) computationally faster.

:D

xzxzzx 7th July 2004 18:43

baaf: Yup, it is computationally faster (I tested it). Damn. I still have the most concise, though. :p

Jaak 7th July 2004 19:14

this would get fun if php had inline assembler :p


All times are GMT. The time now is 10:48.

Copyright © 1999 - 2010 Nullsoft. All Rights Reserved.