SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Include same block of code multiple times

    Hi everyone,


    I need to use the same block of code in multiple places on the same page. Can I assign the following code to a variable, or is a function more appropriate or perhaps heredoc? It is just to simplify things and reduce the possibility of errors.


    Thank you very much!!!


    PHP Code:
    if (isset($name)){
    echo 
    '&name='.$name.'';
    }
    elseif(isset(
    $_GET['name'])) {
    $getname explode(','$_GET['name']); 
    foreach (
    $getname as $key => $value) {
              echo 
    '&name='.$value.'';
    }


  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You could put it in a function. Or, if the variables always have the same values in the page, you could put the code on top of the page, and store the result in a variable instead of echoing it out. And then, in the multiple places in the page, echo out the variable.

    By the way, if this code creates a part of the query string, then the else doesn't make much sense. a querystring containing &name=eric&name=john&name=paula would pass only one 'name' to the receiving script.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Guido,

    I inserted the above code into a function:

    Code:
    function myfunction() {
    if (isset($name)){
    echo '&name='.$name.'';
    }
    elseif(isset($_GET['name'])) {
    $getname = explode(',', $_GET['name']); 
    foreach ($getname as $key => $value) {
              echo '&name='.$value.'';
    }
    }
    }
    and then used myfunction() to call the function but nothing happened.


    And then, in the multiple places in the page, echo out the variable.
    That works.

    By the way, if this code creates a part of the query string, then the else doesn't make much sense. a querystring containing &name=eric&name=john&name=paula would pass only one 'name' to the receiving script.
    I'll need to check that out.


    Thank you for your help

    Ciao

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Instead of echo inside the function, try having it return a string. Then echo myfunction();

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try passing $name as a parameter to the function.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  6. #6
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mittineague and John_Betong,

    thank you for your input!

    I assigned '&name='.$name.''; to a variable which I echo out if it is set. The other techniques you recommended didn't work but it doesn't matter right now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •