简体   繁体   English

如何在 PHP 中的函数中打印变量

[英]How to Print a variable in a function in PHP

I have a form, with post="ModelSelector", when submitted, we go through these codes.我有一个表单,带有 post="ModelSelector",提交时,我们会检查这些代码。 Issue I'm facing is, I want to check the value of $_POST, I know it is getting set by calling "isset()".我面临的问题是,我想检查 $_POST 的值,我知道它是通过调用“isset()”来设置的。

I just want to Print/alert/pushout the variable $productselection我只想打印/提醒/推出变量 $productselection

function selectProduct() {       
    // save the post in a variable
    $ProductSelections =  $_POST['ModelSelector'];

    // I want to print $ProductSelection to check its value
    $frmVars['ProductSelections'] = $ProductSelections;
    $frmVars['WindowSize']        = $WindowSize;
    $frmVars['PageNum']  = 1;
    saveFormValues(0,'RunDefMgr', $frmVars);

    // Clear the checkboxes         
    $sel = array();
    deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}


if(isset($_POST['ModelSelector'])) {
    selectProduct();
} 

I have tried ECHO, for some reason it is not printing the value in HTML.我试过 ECHO,由于某种原因它没有在 HTML 中打印值。 Thanks in advance.提前致谢。

I want to check the value of $_POST我想检查 $_POST 的值

$_POST will be an array. $_POST将是一个数组。

Use print_r($_POST) or var_dump($_POST) to view its contents.使用print_r($_POST)var_dump($_POST)查看其内容。

Your form method should be method="POST" , You can use the following edit to see if it works , as you have to pass $_POST (array) to the function to use it inside function.您的表单方法应该是method="POST" ,您可以使用以下编辑来查看它是否有效,因为您必须将$_POST (array)传递给函数才能在函数内部使用它。 function expects an parameter else , $_POST does not exists.函数需要一个参数 else , $_POST不存在。

And also enable the errors inside your file to check which type of errors you are getting by using : ini_set('display_errors',1);并且还可以使用文件中的错误来检查您遇到的错误类型: ini_set('display_errors',1); or error_reporting(E_ALL);error_reporting(E_ALL);

function selectProduct($_POST) { // create parameter $_POST which we get from isset condition.
    // save the post in a variable
    $ProductSelections =  $_POST['ModelSelector'];
    print_r($ProductSelections); // print the value.
    // I want to print $ProductSelection to check its value

    $frmVars['ProductSelections'] = $ProductSelections;
    $frmVars['WindowSize']        = $WindowSize;
    $frmVars['PageNum']  = 1;
    saveFormValues(0,'RunDefMgr', $frmVars);

    // Clear the checkboxes         
    $sel = array();
    deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}

if(isset($_POST['ModelSelector'])) {
    selectProduct($_POST); // pass the $_POST array to the selectProduct function.
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM