0

I want to insert a PHP code into a variable.

I want to replace "Some text" with a PHP code.

I found some examples on the internet, but when I try to run my script it isn't working.

This is my current script: $purchaseID = "Some text";

I want to change "Some text" with the following code:

<?php
  echo date('y/m/d');
  echo "O";
  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("db", $link); 
  $result = mysql_query("SELECT * FROM table", $link);
  $num_rows = mysql_num_rows($result);echo "$num_rows"+1;
?>

I have tried:

$purchaseID = <<<END
echo date('y/m/d');
echo "O"; 
$link = mysql_connect("localhost", "root", "");
mysql_select_db("db", $link);
$result = mysql_query("SELECT * FROM table", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows"+1;
END;

When I run this I get a error.

How can I fix this and replace "Some text" with the PHP code?

2
  • Why do you want to have "some code into a variable", what you expect is to have $purchaseID: "2016/06/24O5" (if $num_rows = 4) is that right? Commented Jun 25, 2016 at 2:18
  • Yes, that is what I want to have! Commented Jun 25, 2016 at 2:21

4 Answers 4

0

If i understand well your need, you just need to concatenate the different part of the string:

$purchaseID = date('y/m/d');
$purchaseID .= "O"; 
$link = mysql_connect("localhost", "root", "");
mysql_select_db("db", $link);
$result = mysql_query("SELECT * FROM table", $link);
$num_rows = mysql_num_rows($result);
$purchaseID .= $num_rows + 1;

You will get (considering $num_rows to be 4)

2016/06/24O5

Do you really need the heredoc syntax here?

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

5 Comments

I get a error message. When I vardump $purchaseID ill get the right string. When I vardump $requestTransaction ($purchaseID is a part of this) I get: "Transaction.purchaseId does not match format."
Edit your question to explain what is $requestTransaction and what do you mean by $purchaseID is part of this. But this is another problem right? Does your initial problem is fixed?
I dont know if my problem is fixed. The script is still not working by some reason. It is also possible that the script is not working because the solution I tried is not working
We can't really help you since we don't know nothing about $requestTransaction
No "snake questions" please. Let new aspects like $requestTransaction go into a completely new question.
0

You are using the heredoc syntax, that interpolates variables. You might want the herenow syntax without interpolation:

$purchaseID = <<<'END'
    echo $variable;
END;

// $purchaseID == "echo $variable;"

Enclose the delimiter into single qoutes. Interpolation is not done in this case. To execute the code, you have to use eval($purchaseID);. Be aware, that eval has a dangerous potential when relying on external data.

2 Comments

I get a error message. When I vardump $purchaseID ill get the right string. When I vardump $requestTransaction ($purchaseID is a part of this) I get: "Transaction.purchaseId does not match format."
You should create a new question targeting the problem to form a proper transaction format. Provide all the necessary code with the new question. You can simplify the example code by hardcoding the expected value into $purchaseID
0

try but i don't recommend you eval

There are some side effects using eval() refer and learn here

<?php


$time =  '<?php echo time(); ?>'; 

eval('?>'.$time.'<?php;');


?>

Comments

0

You would have to escape the variables, like this:

$purchaseID = <<<END
echo date('y/m/d');
echo "O"; 
\$link = mysql_connect("localhost", "root", "");
mysql_select_db("db", \$link);
\$result = mysql_query("SELECT * FROM table", \$link);
\$num_rows = mysql_num_rows(\$result);
echo "\$num_rows"+1;
END;

That's it!

Cheers!

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.