SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Assigning a void type!

    PHP Code:
    $comparison void
    Trhows an USER_ERROR:

    Use of undefined constant void - assumed 'void'
    How can the void type be assigned to a variable without throwing an error?

    PS. Keep in mind void is needed, not NULL.
    Last edited by REMIYA; Jun 10, 2007 at 01:10.

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the difference between void and null? Or better, in what context do you need to use void ?
    If you want an empty variable, initialize it according with it's type.

  3. #3
    ¨.¨ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What is void in other programming languages?

    What ever it is PHP does not have a void type. Can you elaborate on what you are doing?

    http://www.php.net/manual/en/language.types.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no void type in PHP.

  5. #5
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use it for TRUE/FALSE comparison
    PHP Code:
    $comparison void;

    if((
    $comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;}

    if((
    $comparison!=$result_boolean)===true){$comparison=true;}else{$comparison=false;}

    if((
    $comparison||$result_boolean)===true){$comparison=true;}else{$comparison=false;}

    if((
    $comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;} 
    With void it works very well, with NULL and false it does not work correctly!

  6. #6
    SitePoint Zealot Servyces's Avatar
    Join Date
    Jun 2007
    Location
    The Netherlands
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you just want to initialize the $comparison variable and have the checks actually set it? Then use this:

    Code php:
    $comparison = '';
     
    if(($comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;}
     
    if(($comparison!=$result_boolean)===true){$comparison=true;}else{$comparison=false;}
     
    if(($comparison||$result_boolean)===true){$comparison=true;}else{$comparison=false;}
     
    if(($comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;}
    Last edited by Servyces; Jun 13, 2007 at 09:44.
    Servyces.com
    Where itís all about you.
    Your partner in online solutions.
    Visit our website at http://www.servyces.com/

  7. #7
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Servyces View Post
    So you just want to initialize the $comparison variable and have the checks actually set it?
    Yes this is exacly what I need it for. To be void untill initialization, and then keep track of every next comparison.

    Quote Originally Posted by Servyces View Post
    PHP Code:
    $comparison ''
    This doesn't work.

  8. #8
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really don't get it. How does it work well with void, when void doesn't exist in php?
    what is wrong with
    Code:
     if ((bool)$result_boolean) ; //do something
    Can you provide some specific situation?

  9. #9
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, here is the whole code part. It is a bit long, checking every row of a table lookin for matches

    PHP Code:
        foreach($table_content as $table_row){
                    
    $comparison void;
                    
    //print_r($table_row);
                    
    foreach($sql as $sql_clause){
                        
    //Is clause matching
                        
    if($this->matches($table_row[$sql_clause['FIELD']],$sql_clause['OPERATOR'],$sql_clause['VALUE'])===true){
                            
    $result_boolean=true;
                        }else{
                            
    $result_boolean=false;
                        }
                        
    //Calculating comparison
                        
    if($sql_clause['TYPE']=="AND"){         if(($comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;}
                        }elseif(
    $sql_clause['TYPE']=="NOT"){    if(($comparison!=$result_boolean)===true){$comparison=true;}else{$comparison=false;}
                        }elseif(
    $sql_clause['TYPE']=="OR"){     if(($comparison||$result_boolean)===true){$comparison=true;}else{$comparison=false;}
                        }else{                                    if((
    $comparison&&$result_boolean)===true){$comparison=true;}else{$comparison=false;} }
                    }
                    if(
    $comparison===true){
                        continue;
                    }else{
                        
    $table_rows[count($table_rows)]=$table_row;
                    }
                } 
    void is working, but throwing USER_ERROR
    false, not working
    NULL, not working
    '', not working

    true, is working (at least for the AND clause)

  10. #10
    ¨.¨ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Here is some information on what is happening how PHP recovers from this error (NOTICE). It turns $comparison = void; into $comparison = 'void'; making it a string.

    Now I'm looking at your code and it look so illogical I can't make heads or tails of it.

    ($comparison || $result_boolean) Will always return true as $comparison is set as 'void';

    $comparison is always true
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •