Syntax error with a function

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
schlurfman2000
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Oct 03, 2020 12:26 am

Sat Oct 03, 2020 12:34 am

Hi everyone,

I just have a simple question, I have been searching in the php doc but I can't find the answer

I have these 2 lines of code :

Code: Select all

// THIS WORKS
$tmp = redirectIfNeeded($param);
if ($tmp !== false ){ return '';}
How can I have the same result but only with one line of code?

I tried this , however this single line does not work! (syntax error)

Code: Select all

// THIS DOESN'T WORK (SYNTAX ERROR)
if (redirectIfNeeded($param) !== false) {return '';}
Can you help me ?

Thank you very much for the help ! :)
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 396
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Sun Oct 04, 2020 5:16 am

schlurfman2000 wrote:
Sat Oct 03, 2020 12:34 am
Hi everyone,

I just have a simple question, I have been searching in the php doc but I can't find the answer

I have these 2 lines of code :

Code: Select all

// THIS WORKS
$tmp = redirectIfNeeded($param);
if ($tmp !== false ){ return '';}
How can I have the same result but only with one line of code?

I tried this , however this single line does not work! (syntax error)

Here's a little test script that I did

Code: Select all

// THIS DOESN'T WORK (SYNTAX ERROR)
if (redirectIfNeeded($param) !== false) {return '';}
Can you help me ?

Thank you very much for the help ! :)
I think I am missing something as I don't know why you are checking a redirect, but not seeing more of the code is probably the reason. Anyways, you logic is a little flawed as you should be checking for true NOT false.

Code: Select all

function redirectIfNeeded($param) {
    if ($param === "Daffy Duck") {
        return true;
    } else {
        return false;
    }
}

//$param = "Daffy Duck";
$param = "Bugs Bunny";

$tmp = redirectIfNeeded($param);
if ($tmp) { 
     echo "You don't have to shoot him now!";
} else {
    echo "He does to have to shoot me now!";
}
echo "\n";
echo (redirectIfNeeded($param)) ? "He does to have to shoot me now!" : "You don't have to shoot me now!"; 
To accomplish what you want to do (I think?) you would do the following:

Code: Select all

(redirectIfNeeded($param)) ? return ' '  :  null; 
Though I'm not 100 percent that will work?
Life is a fig newton of your imagination! https://www.miniaturephotographer.com
schlurfman2000
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Oct 03, 2020 12:26 am

Mon Oct 12, 2020 5:41 am

Thank you Strider64 !
Post Reply