简体   繁体   中英

variable in php function name

I'm wanting to utilise PHP's built-in imagejpeg and imagepng functions. I have a variable $type which defines the filetype jpeg or png.

I wanted to have one function call. Something like this:

image.$type($image, null, 100);

But that doesn't work. Any ideas?

It should works if you use the full function name in a var

$func = "image".$type;
$func($image,null,100);

Careful : imagepng use a quality between 0-9 (not 0 and 100)

for completeness: you should use an approach more solid, using a OOP method with Factory method pattern . However I don't know your situation and your code. So it's just a note.

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