Michdd Posted November 18, 2008 Share Posted November 18, 2008 I tried this before, but I deleted it after I made it and it didn't work, but it wasn't anything big, so I'll show you what I tried, I have an image creating script using GD, and there are several different outcomes, so I used a switch. However I'm repeating the thing in the file over and over with every case, and there's only 1 thing different with each case, so I decided that it would be best to use a function. I tried something like this: function createpet($url){ $im = imagecreatefrompng('$url'); $bg = imagecolorallocate($im, 255, 255, 255); $textcolor2 = imagecolorallocate($im, 0, 0, 0); $img_w = imagesx($im); $img_h = imagesy($im); imagestring($im, $font_size, round(($img_w/2)-((strlen($name)*imagefontwidth($font_size))/2), 1), 5, $name, $textcolor2); imagestring($im, $font_size, round(($img_w/2)-((strlen($levelout)*imagefontwidth($font_size))/2), 1), round(($img_h)-25), $levelout, $textcolor2); header('Content-type: image/png'); $x = imagecolorat($im, 0,0); imagecolortransparent($im, $x); imagepng($im); imagedestroy($im); } //Then using the switch, I tried this.. switch($type){ case "moglo": function createpet("moglo.png"); break; } Am I doing something wrong with the function? Quote Link to comment https://forums.phpfreaks.com/topic/133205-solved-function-help/ Share on other sites More sharing options...
bobbinsbro Posted November 18, 2008 Share Posted November 18, 2008 $im = imagecreatefrompng('$url'); should be $im = imagecreatefrompng($url); single quotes around a var make php treat it as a string and not a var. Quote Link to comment https://forums.phpfreaks.com/topic/133205-solved-function-help/#findComment-692793 Share on other sites More sharing options...
premiso Posted November 18, 2008 Share Posted November 18, 2008 switch($type){ case "moglo": function createpet("moglo.png"); break; } should be switch($type){ case "moglo": createpet("moglo.png"); break; } Quote Link to comment https://forums.phpfreaks.com/topic/133205-solved-function-help/#findComment-692797 Share on other sites More sharing options...
Michdd Posted November 18, 2008 Author Share Posted November 18, 2008 switch($type){ case "moglo": function createpet("moglo.png"); break; } should be switch($type){ case "moglo": createpet("moglo.png"); break; } Opz, I just made that mistake when rewriting it, I didn't have that before. And that problem the other person mentioned, would that only cause problems with functions?Because when I don't use the function, and just rewrite the code for every case, there's no problem. Quote Link to comment https://forums.phpfreaks.com/topic/133205-solved-function-help/#findComment-692802 Share on other sites More sharing options...
bobbinsbro Posted November 18, 2008 Share Posted November 18, 2008 single-quoting a var would cause trouble regardless of where it is positioned in the code. if when you rewrite the code, you subsitute $url with an actual path, then yeah, you'd only have the problem in the function. EDIT: correction - if the singel-quotes are in turn double-quoted, the var will be treated properly. example: $var = "bla"; echo '$var'; //outputs $var echo "'$var'"; //outputs 'bla' Quote Link to comment https://forums.phpfreaks.com/topic/133205-solved-function-help/#findComment-692807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.