Jump to content

Defining a variable dynamically


Riki

Recommended Posts

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!!!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'); ?>" />

 

Link to comment
Share on other sites

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; ?>" />

 

Link to comment
Share on other sites

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. :/

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.