2011-08-14 6 views
1

안녕 같은 것을 할 수있는 더 좋은 방법이 있다면 난 그냥 궁금 해서요 : 당신의 목표는 PHP의 통지를하지 않도록하는 경우볼 수있는 더 좋은 방법 만약 변수는 isset PHP는

$openid = $_SESSION['openiduserdata']; 
if (isset($openid['namePerson/friendly'])) 
    {$username = $openid['namePerson/friendly'];} 
if (isset($openid['namePerson/first'])) 
    {$firstname = $openid['namePerson/first'];} 
if (isset($openid['namePerson/last'])) 
    {$lastname = $openid['namePerson/last'];} 
if (isset($openid['birthDate'])) 
    {$birth = $openid['birthDate'];} 
if (isset($openid['contact/postalCode/home'])) 
    {$postcode = $openid['contact/postalCode/home'];} 
if (isset($openid['contact/country/home'])) 
    {$country = $openid['contact/country/home'];} 
if (isset($openid['contact/email'])) 
    {$email = $openid['contact/email'];} 
+0

그것은 당신이 당신의 예제 코드에서 기본적으로 처리하는 방법을 불분명 (나중에 존재를 다시 각 변수를 조사해야 할 것 같습니다). 그러나 개별 if 문 대신 배열 맵을 사용하고자 할 수 있습니다. – mario

+0

나는 질문이있다. 왜 같은 값을 세 번 메모리에 넣어야 하는가? $ _SESSION [ 'openiduserdata'], $ openid 및 $ username 등. – corretge

답변

2

, 그냥 배열을 접두사를 @와 변수 : 당신이 기본 값으로 배열에 설정되어 있지 만 옵션을 설정하려는 경우

$username = @$openid['namePerson/friendly']; 
+3

정말 와우 멋쟁이? 이것은 끔찍한 관례이며, 나중에 "화이트 페이지 디버깅"의 긴 밤을 가져올 수 있습니다. – AlienWebguy

+1

@AlienWebguy : 실제로는 그 반대입니다. OPs 원본 코드 스 니펫 *** ***은 단일 추적 메모를 방출하지 않습니다. 그러나 @ -suppressed notices는 필요할 때 (사용자 정의 오류 처리기로 로그로) 다시 표시 할 수 있습니다. ***이 경우 중요한 ***이 아닙니다. – mario

+0

전심으로 마리오에 동의하지 않습니다. 당신은 무언가가 작동하지 않을 때마다 OP가'@'를 찾지 않을 것이라고 생각합니다. 적절한 오류 검사 및 장애 조치 대신 알림/경고를 표시하지 않는 것이 좋지 않은 습관입니다. – AlienWebguy

5
$variables = array('openid' => 'openiduserdata', 'username' => 'namePerson/friendly', 'firstname' => 'namePerson/first', 'lastname' => 'namePerson/last', 'birth' => 'birthDate', 'postcode' => 'contact/postalCode/home', 'country' => 'contact/country/home', 'email' => 'contact/email'); 

foreach ($variables as $name => $key) 
    if (isset($openid[$key])) 
    $$name = $openid[$key]; 
+0

가변 변수 ... 나는 아마도 최선의 아이디어 일지라도 그것을 사용하는 솔루션을 upvote 할 수는 없습니다. –

+0

나는 위장감에 대해 당신을 비난하지 않지만,이 경우 변수 이름은 하드 코드 된 배열에서 오는 것이지 사용자 입력이 아니거나 비슷한 위험한 것입니다. 프로그래머는 변수 이름을 똑같이 정의했습니다. –

+0

그래, 알아,하지만 여전히 ... 우. –

1

, 하나 개의 솔루션은 모든 기본값을 포함하는 배열을 생성하는 것입니다, 그런 다음 병합 들어오는 배열을 기본 배열로 가져옵니다.

<?php  
$defaults = array('name' => 'Anonymous','gender' => 'n/a'); 
$data = array_merge($defaults, $_POST); 
// now data includes all the post parameters, however, those parameters that don't exist will be the default value in $data 
+0

+1 나에게서. 나는 똑같은 아이디어가있는 나의 것을 게시하기 전에 당신의 대답을 잘못 읽었다. – Paul

0

시도 그냥 이런 식으로 함수를 만드는 :

function get_value_or_default($array, $key, $default = null) 
{ 
    if (array_key_exists($key, $array)) 
    { 
     return $array[$key]; 
    } 
    else 
    { 
     return $default; 
    } 
} 

$username = get_value_or_default($openid, 'namePerson/friendly'); 
0
$openid = array_merge(
    array('namePerson/friendly' => NULL, // Or an empty string if you prefer. 
     'namePerson/first' => NULL, 
     'namePerson/last'  => NULL), // etc. 
    $_SESSION['openiduserdata']); 

// Now you know that the keys are set. 
// Then if you really need them separate: 
$username = openid['namePerson/friendly']; 
$firstname = openid['namePerson/first']; 
$lastname = openid['namePerson/last']; 
// etc. 
관련 문제