0

I recently started working with PHP and I am trying to create a function that will return the content what I want to show. So that means I need to store the value of these contents in a variable in each case block and then return that variable value at the end.

<?php
    function sidebarContent($id) {
        switch ($id) {
            case "21":
                // how to store these values in a variable
                <p>Hello ABC.</p>
                <p class="content-register"><a href="/abc">Abc</a></p>
                break;
            case "31":
                // how to store these values in a variable
                <p>Hello DEF.</p>
                <p class="content-register"><a href="/Def">Def</a></p>  
                break;
            case "41":
                // how to store these values in a variable              
                echo "Your favorite color is green!";
                <p>Hello GHI.</p>
                <p class="content-register"><a href="/Ghi">Ghi</a></p>          
                break;
            default:
                // how to store these values in a variable              
                <p>Hello World.</p>
        }

        // how do I return content here?
    }

    // call above function to test out
?>

I am not able to understand how to store content values in a variable and then return at the end?

4
  • Store your html, echo in a variable, and then return it Commented May 24, 2016 at 4:50
  • You don't have to store anything, you can indeed just output right away, unless you have further filter or handling steps to follow. Then indeed you can assign the strings to a variable by means of the = operator, or you can use "output buffering". Commented May 24, 2016 at 4:52
  • Actually I need to call this function from a different php file so that's why I am making a function which can return. Commented May 24, 2016 at 4:53
  • Even so you can directly output things if you want to. But you cannot simply mix php and html the way you did. That is invalid. Commented May 24, 2016 at 4:53

3 Answers 3

1

Assign the printable ones to a variable and then return them like this:

<?php

function sidebarContent($id) {
    switch ($id) {
        case "21":
        $return = '<p>Hello ABC.</p>
        <p class="content-register"><a href="/abc">Abc</a></p>';
        break;
        case "31":
        $return = '<p>Hello DEF.</p>
        <p class="content-register"><a href="/Def">Def</a></p>';
        break;
        case "41":
        $return = 'Your favorite color is green!";
        <p>Hello GHI.</p>
        <p class="content-register"><a href="/Ghi">Ghi</a></p>';
        break;
        default:
        $return = '<p>Hello World.</p>';
    }

    return $return;
}

echo sidebarContent(31);

Will prints:

Hello DEF.

Def

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to store anything, you can indeed just output right away, unless you have further filter or handling steps to follow. Then indeed you can assign the strings to a variable by means of the = operator, or you can use "output buffering", or, as shown below, just directly return the literal markup.

If you decide to return the payload, then I suggest that approach, it allows clean html markup that is still readable:

<?php
function sidebarContent($id) {
    switch ($id) {
        case 21:
            return <<<EOT
<p>Hello ABC.</p>
<p class="content-register"><a href="/abc">Abc</a></p>
EOT;
        case 31:
            return <<<EOT
<p>Hello DEF.</p>
<p class="content-register"><a href="/Def">Def</a></p>  
EOT;
        case 41:
            return <<<EOT
Your favorite color is green!
<p>Hello GHI.</p>
<p class="content-register"><a href="/Ghi">Ghi</a></p>          
EOT;
        default:
            return <<<EOT
<p>Hello World.</p>
EOT;
    }
}

echo sidebarContent(111);

And note the missing closing php tag (?>). Do not use it. It creates huge issues and does not really help in most cases...

Comments

0

It's a better approuch to keep the data stored in a variable as simple as possible in order to have less memory used. As for your switch statement I can figure out that most of the cases use same format, that leads me to separate the specific date for each of them.

<?php
function sidebarContent($id) {
    $name = 'World';
    $extra = '';

    switch ($id) {
        case "21":
            $name = 'Abc';
            break;
        case "31":
            $name = 'Def';
            break;
        case "41":
            $name = 'Ghi';
            $extra = 'Your favorite color is green!';
    }

    return $extra . ' <p>Hello ' . strtoupper($name) . '.</p>' . 
           '<p class="content-register"><a href="/' . $name . '">' . $name . '</a></p>';
}

So the above code says that for each case amend the data stored in the 2 variables (at least 1 for each of them) and then return my HTML code and the data of the stored variables. This way you keep your code clean and simple.

A call to this function could be like:

echo sidebarContent(21); // Output the template
$my_sidebar = sidebarContent(21); // Store the template for later statements

Note: If you keep this function within a PHP file without HTML code, you can remove the closing PHP tag: ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.