Easy PHP help needed

Discussion in 'PHP' started by crazyryan, Nov 28, 2006.

  1. #1
    I have this:
    $description = $file['description'];
    PHP:
    The description is set on gamepages, but not pages without games. So, I would need something that says if the description is empty, then the description is "no description"

    So I guess I would need to use IF and whatever, like, if $description = empty;
    set the $description = "description was empty";

    if you get me?
     
    crazyryan, Nov 28, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $description = $file['description'] ? $file['description'] : 'no description';
    
    PHP:
     
    nico_swd, Nov 28, 2006 IP
    sarahk likes this.
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    
    $description = $file['description'];
    
    if($description == ''){
    $description = "description was empty";
    }
    
    PHP:
     
    jestep, Nov 28, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is a handy function from osCommerc:

    
    function tep_not_null($value) {
          if (is_array($value)) {
            if (sizeof($value) > 0) {
              return true;
            } else {
              return false;
            }
          } else {
            if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) {
              return true;
            } else {
              return false;
            }
          }
        }
    
    PHP:
    In your case:

    if(!tep_not_null($description){ //Note the !
    $description = 'no description';
    }
    PHP:
    Unlike jestep's suggestion this allows for null, spaces, false etc.
     
    T0PS3O, Nov 28, 2006 IP
  5. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    TOPS30,

    Try this function instead:
    
    	function notNull($value) {
    		switch(gettype($value)){
    			case 'boolean':
    			case 'object':
    			case 'resource':
    			case 'integer':
    			case 'double':
    				return true;
    				break;
    			case 'string':
    				if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)){
    					return true;
    				} else {
    					return false;
    				}
    				break;
    			case 'array':
    				if (sizeof($value) > 0){
    					return true;
    				} else {
    					return false;
    				}
    				break;
    			case 'NULL':
    			default:
    				return false;
    				break; 
    		} # end switch
    	} # end function
    
    PHP:
    Bobby
     
    Chemo, Nov 28, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There's always one geek being a smart ass :D

    Needless to say El Bobbo's code is far superior to anything that has HpdL towards the top ;)
     
    T0PS3O, Nov 28, 2006 IP