SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about functions and passing variables

    Hello

    I am new to PHP and I am unsure of how to pass variables between functions between files.

    For example I am currently working on a php script to mask my cj URL's for my web store.

    I have the main store script which normally generates the links including a cj PID. Now I want to call a function within the main script and pass two variables within the function MASK($link and $title). Link and title are generated by the main script. However the function MASK is located in a separate file called shop.php.

    When I call MASK($link, $title) in the main script it goes directly to the function in shop.php and prints an echo "test" on the website. However, when I setup variables to assign the incoming variables too, it does not appear the data is being sent. I try and echo the new variables and nothing appears.

    Here is a code example


    <------------>
    Main Script Php

    if ($buyprice == $price)

    {
    $fake='http://www.something.net/products/';
    Mask($link,$title);
    $f='<tr class="row'.$rowclass.'">

    <td><div style="width:80px;overflow:hidden;"><a href="'.$fake.'" target="_blank" rel="nofollow"><img src="'.$img.'" alt="'.$title.'" /></a></div></td>



    <---------------------->
    Shop.php code

    <?php
    function Mask()
    {

    $link2=$link;
    $title2=$title;
    echo $title2;
    }
    ?>

    No output is generated. What am I doing wrong?

    Thanks for your help.

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You need to tell the function to expect parameters or else it won't know they exist.
    Code:
    function Mask($link,$title)
    {
        $link2=$link;
        $title2=$title;
        echo $title2;
    }

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great thanks for the help, it worked.


    Are there any security concerns when passing variables between files the way I have done?

    Thanks again.

  4. #4
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Nope. That is a normal and common way to do it.


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
  •