1 Replies - 206 Views - Last Post: 03 February 2012 - 05:03 AM Rate Topic: -----

Topic Sponsor:

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Problem passing a second parameter into a PHP function

Posted 03 February 2012 - 04:22 AM

I'm trying to parse a second argument (xmlItemTagID) into my get_currency_rate function as below.
<?php

function get_currency_rate($currencyRssUrl, $xmlItemTagID) {

// Get XML data from source
    if (isset($currencyRssUrl)) {
        $feed = file_get_contents($currencyRssUrl);
    } else {
        echo 'Feed not found.  Check URL';
    }

    checkFeedExists($feed);
    $xml = new SimpleXmlElement($feed);
    $rate = get_date($xml);
    $rate = get_rate($xml, $xmlItemTagID); //EUR

    return $exchange;
}
?>


I set the second argument in a separate PHP script called cityConfig.php

<?php

// Feed URL's //
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';

// City 3 //
$city = 'Paris';

$currencyRateHeading3 = '1 Euro';
$currencyRateFeedCode = 'EUR';
$currencyRate3RssUrl = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';
$xmlItemTagID = 15; 
?>



However, the get_currency_rate function errors saying that a second argument is expected. Any ideas where I'm going wrong?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Problem passing a second parameter into a PHP function

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1759
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Problem passing a second parameter into a PHP function

Posted 03 February 2012 - 05:03 AM

Where are you calling the function? You'd need to pass the second argument into the function every time you call it. - This line in you program simply tells PHP what input to expect when calling your function:
function get_currency_rate($currencyRssUrl, $xmlItemTagID) { ... }


In this case, you are telling PHP to expect two values. Declaring a $xmlItemTagID in the global scope has no effect on how this is processed.

You can set a default value for parameters, if you are upgrading a function but want your preexisting code to be able to use it without having to be updated, or if most of the calls will use the same value.
function get_currency_rate($currencyRssUrl, $xmlItemTagID=15) { ... }


Now you can call the function with only one argument at it will use 15 as the default value for the $xmlItemTagID variable.


Or, ff you want to import a global variable into your function, use the global keyword instead of a parameter.
function get_currency_rate($currencyRssUrl) {
    global $xmlItemTagID;

    // etc...
}



You could also define constant, if the $xmlItemTagID is a constant value:
// Anywhere before the get_currency_rate function is defined.
define("XML_ITEM_TAG_ID", 15)

// And then you can simply do:
function get_currency_rate($currencyRssUrl) {
    $rate = get_rate($xml, XML_ITEM_TAG_ID); //EUR
}



Any of these options should work for you, just pick the one that makes most sense to you. Personally I would either go with the parameter with the default value, or the constant. The global option always seemed a tad crude to me.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1