2014-04-16 2 views
0

사용자를 데이터베이스에 등록 할 등록 함수로 작업하고 있습니다.함수에 전달 된 변수가 비어 있는지 확인하십시오.

해당 인수에 인수가 비어 있는지 확인하는 검사기가 필요합니다. 문제를 단순화 했으므로 이것이 소매상이 아닙니다.

<?php 

create_user($_POST["username"], $_POST["epost"]); 

function create_user($username, $epost){ 

// Pull all the arguments in create_user and check if they are empty 


    // Instead of doing this: 

    if(empty($username) || empty($epost)){ 

    } 

}이 그래서 단순히 함수에 다른 인수를 추가 할 수 있으며 그것은 빈 밤은 있는지 자동으로 확인하다 만들기위한

이유.

단락 질문 :

어떻게 함수의 밤은 모든 인수가 비어 있는지 확인합니까?

답변

3
function create_user($username, $epost){ 

    foreach(func_get_args() as $arg) 
    { 
    //.. check the arg 
    } 
} 
+0

처럼 사용할 수 있습니다! 고마워 – Jacob

+0

몇 분 더 기다려야 겠지만 걱정하지 마라, 나는 항상한다;) – Jacob

0

또한 array_filterarray_map 기능을 사용할 수 있습니다. 예를 들어
위 함수는 두 개의 매개 변수를 사용할

<?php 
function isEmpty($items, $length) { 
    $items = array_map("trim", $items); 
    $items = array_filter($items); 

    return (count($items) !== (int)$length); 
} 
?> 

이하와 같은 함수를 생성한다.

$items = array of arguments, 
$length = the number of arguments the function accepts. 

당신은 마법처럼 일했다

아래
<?php 
create_user($_POST["username"], $_POST["epost"]); 

function create_user($username, $epost) { 
    if (isEmpty(func_get_args(), 2)) { 
    // some arguments are empty 
    } 
} 
?> 
관련 문제