0

I would like to get my whole site's header into a variable. So, for example, I would like to put these three lines into a single variable:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

And I'm writing a function for it:

function set_header(){
         //This is where i would like to set the variable for the 3 lines I mentioned earlier
}
1
  • 1
    You can also put the script tags in a header.php file and then include 'header.php'. Just saying; treating plain HTML as plain HTML makes sense. Commented Feb 22, 2013 at 8:47

2 Answers 2

4
function set_header(){
echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}

set_header();

OR

function set_header(){
return '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}

echo set_header();
Sign up to request clarification or add additional context in comments.

3 Comments

It's not cool to echo inside a function. Second variant is better.
It's general best practice. Functions are to be used to return something. Not to echo-ing it. Using echo preculdes using the function to programmatically build some HTML for output later on, or for further processing.
For example when you echo the text out of your function just like this: function yourStatus(){ echo ' Done'; } echo 'Status ='. yourStatus();
2
    function set_header(){
    $header = '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
                <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
    return $header;
}

echo set_header();

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.