2
$pet ='dog'; 
$action='My '. $pet . 'likes to run.'; 
//The part I would like to modify
$pet ='cat'; 
//Modify the $pet variable inside the $action variable 
//after it has been defined.
echo $action;

This will output: My dog likes to run

Can I make it output: My cat likes to run

I also tried:

$pet ='dog';  
$action=sprintf('My %s likes to run.', $pet);
$pet ='cat';
echo $action;

I do know that this could be achieved by creating a function with an argument like below. But I'm a php beginner and curious about the other methods.

function action($pet = "dog") {
    $p = 'My'. $pet . 'likes to run.';
    return $p;
}
$pet = 'cat';
echo action($pet);
3
  • 10
    The resulting string has effectively no memory of the parts it was built from. If you want one of the parts to be different, you'll have to rebuild it like you did with the function. Commented Oct 12, 2016 at 0:06
  • I was just writing a function for you to achieve this but I'm blind and didn't see yours. I would say that's the best way to do it. Commented Oct 12, 2016 at 0:28
  • This type of task is basically what functions are for. Return something based on a condition. Stick to the function. Commented Oct 12, 2016 at 0:29

2 Answers 2

3

Well, I know that you probably weren't looking for this, but it is late and I am bored.

Disclaimer: this is a bad idea.

class MyString
{
    private $format, $pet;

    public function __construct($format, &$pet)
    {
        $this->format = $format;
        $this->pet = &$pet;
    }

    public function __toString()
    {
        return sprintf($this->format, $this->pet);
    }
}

$myString = new MyString('This is my little %s, cool huh?', $pet);

$pet = 'cat';
echo $myString."\n";

$pet = 'dog';
echo $myString."\n";

$pet = 'goldfish';
echo $myString."\n";

Output:

This is my little cat, cool huh?
This is my little dog, cool huh?
This is my little goldfish, cool huh?

Demo: https://3v4l.org/XmUEZ

Basically this class stores a reference to the $pet variable in it's fields. As such, when the $pet variable is updated, the reference in the class is updated as well.


Another one for good measure:

function mysprintf($format, &$variable)
{
    return function() use ($format, &$variable) {
        return sprintf($format, $variable);
    };
}

$print = mysprintf('This is my little %s, cool huh?', $pet);

$pet = 'cat';
echo $print()."\n";

$pet = 'dog';
echo $print()."\n";

$pet = 'goldfish';
echo $print()."\n";

https://3v4l.org/KJTKj

(Ab)uses closures to save the reference. Probably even worse.


Why is this a bad idea, you ask?

Consider solely this statement:

$pet = 'goldfish';

This is a simple assignment. Most programmers assume that this statement has no side-effects. That means that this statement changes nothing in the execution flow besides creating a new variable.

Our MyString or mysprintf violate this assumption. What should be a simple assignment, now has side-effects. It violates a programmer's expectation in the worst way possible.

Sign up to request clarification or add additional context in comments.

1 Comment

In real-world cases, this code is a nightmare for maintenance. Anyway, very clever use of variable reference with &$pet
2

You are trying to create a Dynamic String Variable, which is not possible in php for now. You can use the functional approach which you have implemented already :) .

function action($pet = "dog") {
    $p = 'My'. $pet . 'likes to run.';
    return $p;
}
$pet = 'cat';
echo action($pet);

if you use the below approach, unfortunately it will only covers printing part not when we need to reassign value of variable.

$pet ='dog';  
$action=sprintf('My %s likes to run.', $pet);
$pet ='cat';
echo $action;

So, just stick with the function way :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.