简体   繁体   中英

In php pass a string as multiple parameters

in PHP i need to use this

$pdf->SetXY(56, 100);

but now i need the parameters in variables

$paramxy= "56, 100";

PHP doesn't seem to like me writing

$pdf->SetXY($paramxy);

it doesn't evaluate "$paramxw" as two separate values..

of course the easy solution would be :

$param[x]= 56; 
$param[y]= 100;
$pdf->SetXY($param[x],$param[y]);

but i'd like it to be shorter and more readable, because I need lots of these two lines..

is there a "parse" like function that I could use, for example like that ?

$pdf_paramxy= "56, 100";
$pdf->SetXY(parse($paramxy));

Native? Not. But you can create your own:

$paramxy= "56, 100";
$params = explode(",", $paramxy);

Then, according to this , you can:

call_user_func_array(array($pdf, "SetXY"), $params);

If values of the array being a string is a problem, you can parse it by each value:

foreach($params as $key => $value)
{
    $params[$key] = (int)$value;
}

call_user_func_array(array($pdf, "SetXY"), $params);

Didn't tested it. Hope it helps.

you can't set a value for a variable in the following manner $pdf_nomenfant= "56, 100"; To achieve your target you have two ways.

  1. create two parameters with different values like $param1 = "56"; and $param2 = "100"; $param1 = "56"; and $param2 = "100";

and the other way is to use an array for this task as in the following:

  1. '$paramxy= array ("56, 100"); $pdf->SetXY(parse($paramxy));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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