can anyone please convert php function into array for me?

Discussion in 'PHP' started by host-portal, Dec 14, 2007.

  1. #1
    I found a function to check requirements from a tutorial of script.
    i want to convert it into an array , can anyone please please convert it into array ? i will highly appriciate it..
    function is :
    
    function check_requirements() {
       $requirements = array ();
       #PHP Vesion
       $result        = array ('req' =>('PHP Version >= 4.1'));
       $result['ok']  = @ version_compare (@ phpversion(), '4.1', '>=');
       $result['txt'] = '('.@ phpversion().')';
       if (!$result['ok'])
          $result['txt'] .= ('script may not work. Please upgrade!');
    
       $requirements[] = $result;
    
       #Server API
       $result       = array ('req' => ('Server API'));
       $result['ok'] = php_sapi_name() != "cgi";
       if ($result['ok'])
          $result['txt'] = '('.php_sapi_name().')';
       else
          $result['txt'] = ('CGI mode is likely to have problems.');
    
       $requirements[] = $result;
    
       #GD support
       $result       = array ('req' => ('GD Support (for visual confirmations)'));
       $result['ok'] = extension_loaded ('gd');
       if ($result['ok'])
       {
          ob_start();
          @ phpinfo(8);
          $module_info = @ ob_get_contents();
          @ ob_end_clean();
          if (preg_match ("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches))
             $result['txt'] = '('.$matches[1].')';
          unset ($module_info, $matches);
       }
       else
          $result['txt'] = ('Visual confirmation functionality will not be available.');
    
       $requirements[] = $result;
    
       #Session Save Path writable?
       $result = array ('req' => ('Session Save Path writable?'));
       $sspath = @ ini_get ('session.save_path');
       if (preg_match ("`.+;(.*)`", $sspath, $matches))
       {
          $sspath = $matches[1];
          unset ($matches);
       }
    
       if (!$sspath)
       {
          $result['ok']  = false;
          $result['txt'] = ('Warning: <span class="item">session.save_path ('.$sspath.')</span> is not set.');
       }
       elseif (is_dir ($sspath) && is_writable ($sspath))
       {
          $result['ok']  = true;
          $result['txt'] = '<span class="item">('.$sspath.')</span>';
       }
       else
       {
          $result['ok']  = false;
          $result['txt'] = ('Warning: <span class="item">##sspath##</span> not existing or not writable.');
          $result['txt'] = str_replace ('##sspath##', $sspath, $r['txt']);
       }
       $requirements[] = $result;
    
       #MySQL Support
       $result       = array ('req' => ('MySQL Support'));
       $result['ok'] = function_exists ('mysql_connect');
       if (!$result['ok'])
       {
          $result['txt']   = ('Not available.');
          $result['fatal'] = true;
       }
       else
       {
          $mysql_version = @ mysql_get_server_info();
          if (empty ($mysql_version))
          {
             @ ob_start();
             @ phpinfo(8);
             $module_info = @ ob_get_contents();
             @ ob_end_clean();
             if (preg_match ("/\bClient\s+API\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches))
                $mysql_version = $matches[1];
          }
          $result['txt']   = '('.(!empty ($mysql_version) ? trim ($mysql_version) : ('Unknown MySQL server version')).')';
       }
       $requirements[] = $result;
    
       #./include/config.php writable?
       $result = array ('req' => ('./include/config.php writable?'));
       $fn = INSTALL_PATH.'include/config.php';
       if (!is_writable ($fn))
          @ chmod ($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'include/config.php is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       #./temp writable?
       $result = array ('req' => ('./temp writable?'));
       $fn = INSTALL_PATH.'/temp';
       if (!is_writable ($fn))
          @chmod($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'temp is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       #./temp/templates writable?
       $result = array ('req' => ('./temp/templates writable?'));
       $fn = INSTALL_PATH.'temp/templates';
       if (!is_writable ($fn))
          @ chmod ($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'temp/templates is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       return $requirements;
    }
    
    PHP:
    any help would be great :)
    thanks..
     
    host-portal, Dec 14, 2007 IP
  2. pwaring

    pwaring Well-Known Member

    Messages:
    846
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    135
    #2
    You can't convert a function into an array, they're two completely different things...
     
    pwaring, Dec 15, 2007 IP
  3. host-portal

    host-portal Banned

    Messages:
    227
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so.. can you make an array for me that will perform similar function ?
     
    host-portal, Dec 16, 2007 IP
  4. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #4
    $requirements[] = $result;
    
       return $requirements;
    }
    PHP:
    This function is already returning an array. Try following

    
    $my_arr = check_requirements(); 
    print_r($my_arr);// print array returned from function
    
    PHP:
    It should display all array elements if function is working without errors.
     
    greatlogix, Dec 16, 2007 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    I think this is what you want... The following works because the function returns an array

    
    $array = check_requirements();
    print_r($array);
    
    function check_requirements() {
       $requirements = array ();
       #PHP Vesion
       $result        = array ('req' =>('PHP Version &gt;= 4.1'));
       $result['ok']  = @ version_compare (@ phpversion(), '4.1', '>=');
       $result['txt'] = '('.@ phpversion().')';
       if (!$result['ok'])
          $result['txt'] .= ('script may not work. Please upgrade!');
    
       $requirements[] = $result;
    
       #Server API
       $result       = array ('req' => ('Server API'));
       $result['ok'] = php_sapi_name() != "cgi";
       if ($result['ok'])
          $result['txt'] = '('.php_sapi_name().')';
       else
          $result['txt'] = ('CGI mode is likely to have problems.');
    
       $requirements[] = $result;
    
       #GD support
       $result       = array ('req' => ('GD Support (for visual confirmations)'));
       $result['ok'] = extension_loaded ('gd');
       if ($result['ok'])
       {
          ob_start();
          @ phpinfo(8);
          $module_info = @ ob_get_contents();
          @ ob_end_clean();
          if (preg_match ("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches))
             $result['txt'] = '('.$matches[1].')';
          unset ($module_info, $matches);
       }
       else
          $result['txt'] = ('Visual confirmation functionality will not be available.');
    
       $requirements[] = $result;
    
       #Session Save Path writable?
       $result = array ('req' => ('Session Save Path writable?'));
       $sspath = @ ini_get ('session.save_path');
       if (preg_match ("`.+;(.*)`", $sspath, $matches))
       {
          $sspath = $matches[1];
          unset ($matches);
       }
    
       if (!$sspath)
       {
          $result['ok']  = false;
          $result['txt'] = ('Warning: <span class="item">session.save_path ('.$sspath.')</span> is not set.');
       }
       elseif (is_dir ($sspath) && is_writable ($sspath))
       {
          $result['ok']  = true;
          $result['txt'] = '<span class="item">('.$sspath.')</span>';
       }
       else
       {
          $result['ok']  = false;
          $result['txt'] = ('Warning: <span class="item">##sspath##</span> not existing or not writable.');
          $result['txt'] = str_replace ('##sspath##', $sspath, $r['txt']);
       }
       $requirements[] = $result;
    
       #MySQL Support
       $result       = array ('req' => ('MySQL Support'));
       $result['ok'] = function_exists ('mysql_connect');
       if (!$result['ok'])
       {
          $result['txt']   = ('Not available.');
          $result['fatal'] = true;
       }
       else
       {
          $mysql_version = @ mysql_get_server_info();
          if (empty ($mysql_version))
          {
             @ ob_start();
             @ phpinfo(8);
             $module_info = @ ob_get_contents();
             @ ob_end_clean();
             if (preg_match ("/\bClient\s+API\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches))
                $mysql_version = $matches[1];
          }
          $result['txt']   = '('.(!empty ($mysql_version) ? trim ($mysql_version) : ('Unknown MySQL server version')).')';
       }
       $requirements[] = $result;
    
       #./include/config.php writable?
       $result = array ('req' => ('./include/config.php writable?'));
       $fn = INSTALL_PATH.'include/config.php';
       if (!is_writable ($fn))
          @ chmod ($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'include/config.php is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       #./temp writable?
       $result = array ('req' => ('./temp writable?'));
       $fn = INSTALL_PATH.'/temp';
       if (!is_writable ($fn))
          @chmod($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'temp is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       #./temp/templates writable?
       $result = array ('req' => ('./temp/templates writable?'));
       $fn = INSTALL_PATH.'temp/templates';
       if (!is_writable ($fn))
          @ chmod ($fn, 0777);
    
       $result['ok'] = is_writable ($fn);
       if (!$result['ok'])
       {
          $result['txt']   = ('Fatal: '.INSTALL_PATH.'temp/templates is not writable, installation cannot continue.');
          $result['fatal'] = true;
       }
       $requirements[] = $result;
    
       return $requirements;
    }
    
    PHP:
     
    Kaizoku, Dec 16, 2007 IP
  6. host-portal

    host-portal Banned

    Messages:
    227
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok now , but how to call this array ? i mean how to show the resultS?
     
    host-portal, Dec 27, 2007 IP
  7. pwaring

    pwaring Well-Known Member

    Messages:
    846
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    135
    #7
    You don't 'call' arrays, you call functions, which then return a result (this may be an array). I'm not sure if you should be messing around with PHP if you don't know the difference between these two concepts, but to print the contents of an array you can use:

    var_dump($array);
    PHP:
     
    pwaring, Dec 27, 2007 IP
  8. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    or
    <?php
    echo '<pre>'.print_r( $array, 1 ) .'</pre>'.PHP_EOL;
     
    coches, Dec 28, 2007 IP
  9. icfire

    icfire Peon

    Messages:
    321
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Don't learn php the hard way, go get a book, you will learn more reading the book than hacking around in 1 year
     
    icfire, Dec 28, 2007 IP