简体   繁体   中英

Putting Function in an echo in PHP

i'm just wondering how are you suppose to put a function within an echo statement?

function Catselect($abc){
global $category;
if($category == $abc){
echo 'selected ="selected"';
}
}

echo'
        <select style="width: 260px;" name="category">
        <option value="01" '.Catselect("01").'>Site Related</option>
        <option value="02" '.Catselect("02").'>Tournaments</option>
        <option value="03" '.Catselect("03").'>Articles</option>
        <option value="04" '.Catselect("04").'>Interviews</option>
        <option value="05" '.Catselect("05").'>General</option>
        </select>
        ';

I'm trying to make a update page where you'll get to see what the previous category was, so let's say before that, the category is Tournaments with value 2, if you were to go to the update page, the tournament category would be automatically selected.

But what i get is just the word selected ="selected" on top of my header.

Any help ?

Instead of echo 'selected ="selected"'; try return 'selected ="selected"';

Change the echo in the function for a return , it should work.

To explain briefly, since you're already using the echo statement in your html to output your list items, you only need a return value in your function and the previous echo will take care to print it.

You need to return the value from the function, not echo it inside the function. What you're doing is you're concatenating a string with the return value of a function, which is perfectly fine. Like:
'A random number: ' . rand() 'A random number: ' . rand() . The function gets executed and whatever is returned from the function is concatenated to the string. If the function also outputs something by itself, that's output at the time the function is called, before its return value is concatenated to the string.

I'm sure someone will have a more elegant solution... but this will work.

function Catselect($abc){
global $category;
$sel1 = ''; $sel2=''; $sel3=''; $sel4=''; $sel5 = '';
switch($abc) {
    case '01': $sel1='selected'; break;
    case '02': $sel2='selected'; break;
    case '03': $sel3='selected'; break;
    default:
    case '04': $sel4 = 'selected'; break;
    case '05': $sel5 = 'selected'; break;
}
return $sel1; return $sel2; return $sel3; return $sel4; return $sel5;
}


echo'
    <select style="width: 260px;" name="category">
    <option value="01" '.Catselect("01").' '.$sel1.'>Site Related</option>
    <option value="02" '.Catselect("02").' '.$sel2.'>Tournaments</option>
    <option value="03" '.Catselect("03").' '.$sel3.'>Articles</option>
    <option value="04" '.Catselect("04").' '.$sel4.'>Interviews</option>
    <option value="05" '.Catselect("05").' '.$sel5.'>General</option>
    </select>
    ';

Rather than printing in your function, you'll want to return a value. If $abc is the selected category then you would write $return = "selected=\\"selected\\""; return $return; $return = "selected=\\"selected\\""; return $return; inside the function's if statement.

Instead of

echo 'selected="selected"'; 

Do

return 'selected="selected"';

That will return the string and in turn concatenate it to your output string.

You could also use ternary operators instead of using a function, like so:

echo'
    <select style="width: 260px;" name="category">
    <option value="01" '.($category == "01" ? 'selected="selected"' : '').'>Site Related</option>
    <option value="02" '.($category == "02" ? 'selected="selected"' : '').'>Tournaments</option>
    <option value="03" '.($category == "03" ? 'selected="selected"' : '').'>Articles</option>
    <option value="04" '.($category == "04" ? 'selected="selected"' : '').'>Interviews</option>
    <option value="05" '.($category == "05" ? 'selected="selected"' : '').'>General</option>
    </select>
    ';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM