Riki Posted January 13, 2010 Share Posted January 13, 2010 Hello everyone, this is my first post and Im fairly new to PHP so forgive me if this is a dumb question. I have a variable that I want to define dynamically here is the code below: <?php $domain = "http://test.com"; $image = ""; $output_url = $domain . $image; ?> <img src="<?php $image = "/photo.jpg"; print $output_url; ?>" /> You can see my attempt there at the bottom to define the variable again, but I know thats not the right way to do it, Im overlooking something. It seems very simple, but Ive been searching for hours and Im really stuck. Thanks!!! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 13, 2010 Share Posted January 13, 2010 you have assigned a value to output_url before you 'update' image. this won't work - once something is assigned thats it until you re-assign it. Instaed use: <img src="<?php $image = "/photo.jpg"; print $domain . $image; ?>" /> Quote Link to comment Share on other sites More sharing options...
Riki Posted January 13, 2010 Author Share Posted January 13, 2010 oh wow, Im a little embarrassed how easy that was, haha. Thanks! So that works great, but I oversimplified my post to get quick answers, my actual work is much more complex than just 2 variables being combined..its more like 6. It still works with 6, but that means I have a long line of code for every single image that looks similar to this <img src="<?php $image = "/photo.jpg"; print $domain . $image; . $var3; . $var4; . $var5; . $var6; ?>" /> Is there a more clean way of doing this? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2010 Share Posted January 13, 2010 Well, that code has an error in that it appears you are wanting to concatenate those stings but you have a semicolon after each one. Your question is way too vague to provide any meninginful response. If this is a one-time event where you need to concatenate those variables, then just do it that one time. Personally, I would define the string in the logic of the code and just echo the value in the HTML content like so: <?php $domain = "http://test.com"; $imageSrc = $domain . $image . $var3 . $var4 . $var5 . $var6; ?> <img src="<?php echo $imageSrc; ?>" /> But, assuming you need to do this for many different images, then I would suggest a function. I would create parameters for the function based upon what values are variable. I would assume $domain is constant and that image name is variable. But, not sure about the other variables. Here is an example: <?php function imageSource($image) { global $domain, $var3, $var4, $var5, $var6; $imageSrc = $domain . "/" . $image . $var3 . $var4 . $var5 . $var6; return $imageSrc; } $domain = "http://test.com"; ?> <img src="<?php echo imageSource('photo.jpg'); ?>" /><?php function imageSource($image) { global $domain, $var3, $var4, $var5, $var6; $imageSrc = $domain . "/" . $image . $var3 . $var4 . $var5 . $var6; return $imageSrc; } $domain = "http://test.com"; ?> <img src="<?php echo imageSource('photo1.jpg'); ?>" /> <img src="<?php echo imageSource('photo2.jpg'); ?>" /> <img src="<?php echo imageSource('photo3.jpg'); ?>" /> Quote Link to comment Share on other sites More sharing options...
Riki Posted January 13, 2010 Author Share Posted January 13, 2010 I love this site, great response!! What you said works but there is a different issue. Your right in saying my example is too vague and I apologize. It looks like I will have to be extremely precise to get my next and probably final answer. The function idea is Perfect, and yes you are correct that I want the image name to be variable. As you can see Im doing a URL signing feature that requires a MD5 hash..and its causing a conflict (has isnt validating on the other side. So this doesnt work: <?php $passName = "pass"; $passPhrase = "password"; $expName = "exp"; $authName = "auth"; $domain = "http://test.com"; $expTime = time() + (100); $sigUrl = $image . "?" . $expName . "=" . $expTime . "&" . $passName . "=" . $passPhrase; $sig = MD5($sigUrl); function imageSource($image) { global $domain, $expName, $expTime, $authName, $sig; $sigUrl; $imageSrc = $domain . $image . "?" . $expName . "=" . $expTime . "&" . $authName . "=" . $sig; return $imageSrc; } ?> <img src="<?php echo imageSource('/image.jpg'); ?>" /> But this does work (problem is the image variable is "hard coded" <?php $passName = "pass"; $passPhrase = "password"; $expName = "exp"; $authName = "auth"; $domain = "http://test.com"; $image = "/image.jpg"; $expTime = time() + (600); $sigUrl = $image . "?" . $expName . "=" . $expTime . "&" . $passName . "=" . $passPhrase; $sig = MD5($sigUrl); $output_url = $domain . $image . "?" . $expName . "=" . $expTime . "&" . $authName . "=" . $sig; ?> <img src="<?php print $output_url; ?>" /> Quote Link to comment Share on other sites More sharing options...
laffin Posted January 13, 2010 Share Posted January 13, 2010 so does this $output_url = "{$domain}{$image}?{$expName}={$expTime}&{$authName }={$sig}"; if you have a lot of strings, using the string replacement with double quotes looks a lot cleaner Quote Link to comment Share on other sites More sharing options...
Riki Posted January 13, 2010 Author Share Posted January 13, 2010 so does this $output_url = "{$domain}{$image}?{$expName}={$expTime}&{$authName }={$sig}"; if you have a lot of strings, using the string replacement with double quotes looks a lot cleaner Thanks laffin, that does look a lot cleaner!! However Im still looking for a way to get that chunk of code above that one to work to be the ultimate. The problem looks like the the MD5 hashing is being generated before the function..but Im just not sure. :/ Quote Link to comment Share on other sites More sharing options...
laffin Posted January 13, 2010 Share Posted January 13, 2010 These are your variables $passName = "pass"; $passPhrase = "password"; $expName = "exp"; $authName = "auth"; $domain = "http://test.com"; $image = "/image.jpg"; $expTime = time() + (600); So question is which ones stay the same, and which ones change. Once you know which ones are the same, and which ones change, its easier to create a function. I reedited the function, so it generates the md5 as well function imageSource($image) { global $domain, $expName, $expTime, $authName, $sig; $sigUrl; $sigUrl = $image . "?" . $expName . "=" . $expTime . "&" . $passName . "=" . $passPhrase; $sig = MD5($sigUrl); $imageSrc = $domain . $image . "?" . $expName . "=" . $expTime . "&" . $authName . "=" . $sig; return $imageSrc; } Quote Link to comment Share on other sites More sharing options...
Riki Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks for the reply!! That didnt work, however if I put everything in the function it works!! Looks good enough to me, simple and effective I love when it comes together, thanks again mate. Quote Link to comment Share on other sites More sharing options...
Riki Posted January 13, 2010 Author Share Posted January 13, 2010 oh, and to answer your question, the only thing that changes is the image..nothing else. Cheers. Quote Link to comment Share on other sites More sharing options...
laffin Posted January 13, 2010 Share Posted January 13, 2010 Well long as it works. Quote Link to comment 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.