2014-03-03 3 views
0

이것은 알프레드 워크 플로를위한 것입니다. 내 첫 번째거야.PHP 함수 복수 반환

나는 여러 개의 반환 값을 가진 함수를 만들고 있습니다. 각 함수에서 배열을 만들고 인덱스 페이지의 연관 배열을 통해 반환 값에 액세스하려고하므로 여러 값을 가진 배열을 반환해야합니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 내가 제대로하고 있니? 출력 할 수없는 것 같습니다.


functions.php

function firstRun() 
{ 
    // Check to make sure the data folders exist; otherwise, make them. 
    check_data_folder($w->data()); 

    // Define the settings file path. 
    $settings_file = $w->data() . "/settings.json"; 


    // Checks to see if the settings file exists and is valid 
    $check = check_settings($settings_file); 

    // Check for user error 
    if ($check === 'user') { 
     // Username not set, so, make them set it. 
     call_set_username(); 
     die(); 

    } 

    // Check for password error 
    if ($check === 'password') { 
     // No password was set, so make them set it. 
     call_set_password(); 
    } 



    // ------------------------------------------------------------------ 
    // DID I DO THIS CORRECTLY? 
    $output = 
    [ 
     // Read the settings file into an associative array so that we can use it. 
     'settings' => json_decode(file_get_contents($settings_file) , TRUE); 

     // grab the username 
     'username' => $settings['username']; 

     // grab and decrypt the password 
     'password' => decrypt_string($settings['password']); 
    ]; 
    return $ouput; 
    // ------------------------------------------------------------------ 
} 

/** 
    * Pulls the Data out of the JSON file and stores it in an array called $gists 
    * 
    * @return boolean TRUE 
    */ 
function json_to_array($data) 
    { 
     // ------------------------------------------------------------------ 
     // DID I DO THIS CORRECTLY? 
     $output = 
     [ 
      // Cache is still fresh, so why not use it? 
      'gists' => json_decode(file_get_contents($data) , TRUE), 
      // use the cache 
      'usecache' => TRUE 
     ]; 
     return $ouput; 
     // ------------------------------------------------------------------ 

    } 

index.php를

// Run through an initial check to make sure 
$firstRun = firstRun(); 
// 1: The data files and folders exist 

// 2: Settings file exists and set it as an associative array 
$settings = $firstRun['settings']; 

// 3: The User has input their Username 
$username = $firstRun['username']; 

// 4: The User has input their Password 
$password = $firstRun['password']; 



// If the data folders & files exist locally 
if (check_data_folder($w->data())): 

    $data = $w->data() . "/gist-cache.json"; 

    // And they are not fresh 
    if (! is_fresh($data)): 

     // Then Update the local gists 
     update_local_data(); 

    // Otherwise 
    else: 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// DID I DO THIS CORRECTLY? 

     // Store the output into an associative array called $array 
     $array = json_to_array($data); 

     // Then store the gist data into an array called $gists 
     $gists = $array['gists']; 

     // And relay whether the local cache was used or not 
     $usecache = $array['usecache']; // Boolean 

     // return $array; // (A) Do I need to return this array like this? 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    endif; 

endif; 

답변

1

사용 쉼표 대신 배열 선언 세미콜론. 더 낮은 버전의 PHP와 호환되도록 array() 구문을 사용해야합니다.

$output = array(

    // Read the settings file into an associative array so that we can use it. 
    'settings' => json_decode(file_get_contents($settings_file) , TRUE), // <- Commas not semicolons 

    // grab the username 
    'username' => $settings['username'], // <- Commas not semicolons 

    // grab and decrypt the password 
    'password' => decrypt_string($settings['password']) 
);