将预定义变量作为函数参数传递

时间:2015-07-25 07:46:55

标签: php function parameters

的index.php

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        PopulateComboBox();
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.

        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.
    }

    public class stbPlateSource
    {
        public int tiPlateSourceId { get; set; }
        public string cCountryCode { get; set; }
        public string vcPlateSourceDesc { get; set; }
        public string vcPlateSource
        {
            get
            {
                if (!IsEnglish)
                {
                    return nvcPlateSourceArbDesc;
                }
                else
                {
                    return vcPlateSourceDesc;
                }
            }
        }
        public static bool IsEnglish { get; set; }
        public string nvcPlateSourceArbDesc { get; set; }
        public int tiDisplayOrder { get; set; }

    }
    public class stbPlateCateg
    {
        public int Id { get; set; }
        public int tiPlateCategId { get; set; }
        public int tiPlateSourceId { get; set; }
        public string vcPlateCategDesc { get; set; }
        public string nvcPlateCategArbDesc { get; set; }
        public static bool IsEnglish { get; set; }
        public string vcPlateCateg
        {
            get
            {
                if (!IsEnglish)
                {
                    return this.nvcPlateCategArbDesc;
                }
                else
                {
                    return this.vcPlateCategDesc;
                }
            }
        }

        public stbPlateCateg() { }
    }

    private void PopulateComboBox()
    {
        // Step1
        List<stbPlateSource> Emirates = new List<stbPlateSource>();
        for (int i = 0; i < 250; i++)
        {
            Emirates.Add(new stbPlateSource() {
                 cCountryCode = "05" + i,
                 tiDisplayOrder = i+1,
                 tiPlateSourceId = 101 + i,
                 vcPlateSourceDesc = "asd" + i,
                 nvcPlateSourceArbDesc = "dsa" + i
            });
        }
        stbPlateSource.IsEnglish = true;
        EmirateComboBox.ItemsSource = Emirates;

        List<stbPlateCateg> PlateCategory = new List<stbPlateCateg>();
        for (int i = 0; i < 250; i++)
        {
            PlateCategory.Add(new stbPlateCateg()
            {
                Id = 201 + i,
                tiPlateCategId = i + 1,
                tiPlateSourceId = 101 + i,
                vcPlateCategDesc = "asd" + i,
                nvcPlateCategArbDesc = "dsa" + i
            });
        }
        stbPlateCateg.IsEnglish = true;
        CategoryComboBox.ItemsSource = PlateCategory.OrderBy(x => x.tiPlateCategId);


        EmirateComboBox.SelectedIndex = 1;
        CategoryComboBox.SelectedIndex = 0;
    }


}

modulename.php

require 'modulename.php';
$keyword = $_GET['q'];
getResults();

...但是这会引发错误:

  

缺少getResults()的参数2,调用   在xx行上的index.php,在modulename.php中定义

...似乎函数没有使用已定义的变量,我该如何使用它们?

3 个答案:

答案 0 :(得分:1)

您创建了函数getResults($config, $service, $keyword),它需要调用三个参数,但您在没有任何参数的情况下调用它。 你可以试试这个

index.php中的

require 'modulename.php';
$keyword = $_GET['q'];
getResults($keyword );

<强> modulename.php

function getResults($keyword = '') {
    $config = ... ;
    $service = ... ;
    ...
}

答案 1 :(得分:0)

您的getResults();需要参数

getResults($config, $service, $keyword);

如果您没有参数,则默认设置为false

getResults($config=false, $service=false, $keyword)

答案 2 :(得分:0)

错误消息指出arg 2缺失 - 在您的函数中将引用$ keyword。在您提供的示例代码中,您定义了$ config&amp; $ service但不是$ keyword - 因此,通过提供函数参数的默认值,您可以消除错误。但是,如果需要每个参数,那么通过测试它们的值不是假(这是该函数版本中的默认值),那么函数将不执行任何操作。

或者,在调用函数之前定义每个变量: -

$config='';
$service='';
$keyword='';


function getResults($config=false, $service=false, $keyword=false) {
   if( $config && $service && $keyword ){
       ...
   }
}