IT story

이상한 PHP 오류 : '쓰기 컨텍스트에서 함수 반환 값을 사용할 수 없습니다'

hot-time 2020. 5. 19. 08:09
반응형

이상한 PHP 오류 : '쓰기 컨텍스트에서 함수 반환 값을 사용할 수 없습니다'


이 오류가 발생하여 머리 나 꼬리를 만들 수 없습니다.

정확한 오류 메시지는 다음과 같습니다.

치명적인 오류 : 48 행의 /home/curricle/public_html/descarga/index.php에 쓰기 컨텍스트에서 함수 반환 값을 사용할 수 없습니다.

48 행 :

if (isset($_POST('sms_code') == TRUE ) {

아무도 무슨 일인지 알고 ???

추신 : 여기에 도움이되는 경우 전체 기능이 있습니다.

function validate_sms_code() {

    $state = NOTHING_SUBMITED;

    if (isset($_POST('sms_code') == TRUE ) {
        $sms_code = clean_up($_POST('sms_code'));
        $return_code = get_sepomo_code($sms_code);

        switch($return_code) {

          case 1:
            //no error
            $state = CORRECT_CODE;
            break;

          case 2:
            // code already used
            $state = CODE_ALREADY_USED;
            break;

          case 3:
            // wrong code
            $state = WRONG_CODE;
            break;

          case 4:
            // generic error
            $state = UNKNOWN_SEPOMO_CODE;
            break;

          default:
            // unknown error
            $state = UNKNOWN_SEPOMO_CODE;
            throw new Exception('Unknown sepomo code: ' . $return_code);
            break;
        }

    } else {
        $state = NOTHING_SUBMITED;
    }
    dispatch_on_state($state);
}

네 말 뜻은

if (isset($_POST['sms_code']) == TRUE ) {

우연히 당신은 정말 의미

if(isset($_POST['sms_code'])) {

함수 반환에서 empty를 사용할 때도 발생합니다.

!empty(trim($someText)) and doSomething()

empty는 함수가 아니라 언어 구조이기 때문에 (확실하지 않음) 변수 만 가져옵니다.

권리:

empty($someVar)

잘못된:

empty(someFunc())

PHP 5.5부터는 변수 이상을 지원합니다. 그러나 5.5 이전에 필요하면을 사용하십시오 trim($name) == false. 에서 빈 문서 .


if (isset($_POST('sms_code') == TRUE ) {

이 줄을

if (isset($_POST['sms_code']) == TRUE ) {

You are using parentheseis () for $_POST but you wanted square brackets []

:)

OR

if (isset($_POST['sms_code']) && $_POST['sms_code']) { 
//this lets in this block only if $_POST['sms_code'] has some value 

for WORDPRESS:

instead of:

if (empty(get_option('smth')))

should be:

if (!get_option('smth'))

Correct syntax (you had a missing parentheses in the end):

if (isset($_POST['sms_code']) == TRUE ) {
                            ^

p.s. you dont need == TRUE part, because BOOLEAN (true/false) is returned already.


This can happen in more than one scenario, below is a list of well known scenarios :

// calling empty on a function 
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable

// using parenthesis to access an element of an array, parenthesis are used to call a function

if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)

This also could be triggered when we try to increment the result of a function like below:

$myCounter = '356';

$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable, a variable should hold the the return of the function then we can call ++ operator on it.

The problem is in the () you have to go []

if (isset($_POST('sms_code') == TRUE)

by

if (isset($_POST['sms_code'] == TRUE)

I also had a similar problem like yours. The problem is that you are using an old php version. I have upgraded to PHP 5.6 and the problem no longer exist.


Another scenario where this error is trigered due syntax error:

ucwords($variable) = $string;

i also ran into this problem due to syntax error. Using "(" instead of "[" in array index:

   foreach($arr_parameters as $arr_key=>$arr_value) {
        $arr_named_parameters(":$arr_key") = $arr_value;
    }

참고URL : https://stackoverflow.com/questions/1532693/weird-php-error-cant-use-function-return-value-in-write-context

반응형