This question already has answers here:
php curly braces replace values
(3 answers)
Closed 1 year ago.
i'm having a json file containing some strings for echoing.
Now i want to put variable contents between these strings e.g.:
$event_config = json_decode(file_get_contents("event_config.json"), TRUE);
$output_string = $event_config['e_header']['e_welcome_text'];
$name = value_lookup("f_personalien_vorname");
echo("Hello {$name},<br>Thanks. We have just received your query<br>");
echo ($output_string);
This one works fine, {$name} is replaced correctly with whats stored in $name.
echo("Hello {$name},<br>Thanks. We have just received your query<br>");
This version with the same string loaded from json isn't working. Instead of {$name} being replaced it just gets printed with the whole string.
echo ($output_string);
For reference my json currently looks like:
{
"e_header": {
"e_welcome_text": "Hello {$name},<br><br>Thanks. We have just received your query<br>",
"e_information": "Some string"
}
}
Does somebody have an idea about this?
Solved it.
Thanks to #El_Vanja giving me this hint.
I changed my json to:
{
"e_header": {
"e_welcome_text": "Hello {{name}},<br><br>Thanks. We have just received your query<br>",
"e_information": "Some string"
}
}
Note the double braces {{name}}.
Then came up with the following which is replacing the upper expression.
$output_string = $event_config['e_header']['e_welcome_text'];
echo(str_replace('{{name}}', $name, $output_string));
you should escape the quotes and append to have a variable pass.
$event_config = json_decode(file_get_contents("event_config.json"), TRUE);
$output_string = $event_config['e_header']['e_welcome_text'];
$name = value_lookup("f_personalien_vorname");
echo("Hello " . $name . ",<br>Thanks. We have just received your query<be>");
echo($output_string);
Related
This question already has an answer here:
Output code as text in php
(1 answer)
Closed 4 years ago.
I am using Sendgrid to send an email via PHP but I need to include a PHP script in the HTML content. I do not know how to perform the concatenation, it seems really delicate.
Here's my code:
$content = new SendGrid\Content("text/html", '
/*
some HTML content
*/
/*PHP script starts here*/
'.myPHP Script {
}//end of script
.'
/*some more HTML content
');
The '..' concatenation does not work and the entire page fails to load. How do I go about this? I am not trying to output the code. I want it to actually execute.
Don't make life difficult. What ever string you need to concat, put it in a variable and concat that. also note you can do string interpolation in PHP using the double quotes "$variable".
$concatHTML = '';
$prodList = [...]; // assume we have products list here
for ($i = 0, $max = count($prodList); $i < $max; $i++) {
$concatHTML += '<div>'. $prodList[$i] .'</div>';
}
$content = new SendGrid\Content("text/html", "
//some html content here
$concatHTML
");
EDIT
Concatenation could be done using the operator . (e.g.) echo 'hi ' . $name; where $name = 'some string' or using string string interpolation using double quotes and and placing (e.g.) echo "hi $name" you can add curly braces to make separate the variable from the other string like echo "hi Mr.{$name}".
For the php code you want to include in the email is probably not valid.
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I have an url that I call in this form :
http://MyIP/MyServer/php_scripts/test_sp.php?param1=key¶m2=xyz5
and I would like to retrieve the value of the 'param1' and 'param2' parameters. Up to now, I was not successful, because all I get is an empty value.
Sure that I am certainly doing something wrong there, but is it with the way I am calling the url or in my php code itself? Any help will be highly appreciated. Thanks a lot for reading me.
The code below is the contents of my 'test_sp.php' file :
<html>
<head>
</head>
<body>
<?php
mainProcess();
function mainProcess()
{
$mavalue1 = "";
$mavalue2 = "";
$parts = parse_url($url, PHP_URL_QUERY); // << It looks like I also have a problem with this line.
parse_str($query, $params);
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
echo "Valeur de mavalue1 : " . $mavalue1; // <<< This is where I'm getting the empty value
echo "Valeur de mavalue2 : " . $mavalue2; // <<< Same problem here
}
?>
</body>
</html>
The $_GET array is an array with all the arguments passed in the URL.
You can use $_GET['param1']; to get the value of "param1" in the URL.
Have a nice day.
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
This question already has answers here:
How to echo a hyperlink with a variable? [closed]
(3 answers)
Closed 9 years ago.
I am a bit rusted in PHP and currently struggling to do something that should be quite simple.
$value = "PUPPY";
$html .= '<td>{$value}</td>';
echo $html; // Doesn't work, prints {$value}.... Should be... PUPPY
I know there's a simple way for doing this but i just forgot and cannot find a definitive method on Google or StackOverflow.
I need to place the html in a variable cause once it is all done, it gets used by tcpdf::writeHtml()
I already tried :
$html .= "<td align='right' nowrap>".$value."</td>";
echo $html; // But this outputs a TD tag with attribute nowrap="" and this is not valid HTML... tcpdf:writeHtml rejects this.
UPDATE: The bug was in fact caused by a bad usage of the "nowrap" attribute deprecated some time ago.
Single quoted strings are not interpolated in php. Use double quotes instead.
$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;
$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;
Use " instead of '
Take a look at the documentation about Strings on PHP
It should be
$html .= "<td>{$value}</td>";
Double quotes echo variables and single quotes do not.
Instead of '<td>{$value}</td>' put "<td>{$value}</td>".
It should work fine
I'm attempting to run the script referenced here
<?php
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
echo "Variables:\n";
print_r($vars);
$id = reset(explode(':', $vars['id']));
echo "The id is $id\n";
$id = intval($vars['id']);
echo "Again, the id is $id\n";
Unlike the example shown - which works - on my station, the variable array shows that "&" is encoded to "amp;" causing that script not to work for me.
When I output the variable array from the example, I get variables like [amp;id]
How can that scriptbe modified with the "&" decoded so it will work on my station?
Thanks for your help
simple solution is
$url = html_entity_decode('index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44');
I'm having trouble describing this issue, which is probably why I can't find the answer on google.. so I figured I would try getting help here. If I'm repeating this question, feel free to direct me to a link to the thread.
So basically the issue I'm having is I am trying to pass a variable to a function that contains some php code to be eval'd.
Here's the simplified version of the code:
function senduser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
echo eval($body);
}
}
$body = 'Hello $row[\'user_first_name\'] <br>';
sendUser($body);
--
For some reason, the output isn't putting out what I want. I've gotten a few whitespace errors, and a few times I've gotten the code to output the plain text of the variable $body.
Any help is appreciated. Let me know if I need to clarify the issue further.
I would change it to this:
function sendUser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
echo $body($row);
}
}
And then call it like this (php 5.3+):
$body = function ($row) {
return "Hello ".
htmlspecialchars($row['user_first_name'], ENT_QUOTES, 'UTF-8').
"<br />";
};
sendUser($body);
In php <= 5.2, it's a lot messier:
$body = create_function(
'$row',
'return "Hello ".'.
'htmlspecialchars($row["user_first_name"], ENT_QUOTES, "UTF-8").'.
'"<br />";'
);
sendUser($body);
That isn't now eval works; it returns null unless you explicitly return a value. You're also missing quotes around the string, and a semicolon at the end of the statement.
To get it to echo something, you'd have to pass the echo as part of the code to be evaluated:
$body = 'echo "Hello $row[\'user_first_name\'] <br>";';
or, to get your code working as written, you'd have to return the formatted string:
$body = 'return "Hello $row[\'user_first_name\'] <br>";';
This is a pretty contrived use of eval. You'd be far better off passing in a printf-style format string and using sprintf to substitute values into it, and returning that string for printing. As it stands you seem to be mixing your display logic with your database logic, which is a bad thing.
Your code, as is, will never work. Removing the mysql portion:
<?php
function senduser($body) {
$row['user_first_name'] = 'Fred';
echo eval($body);
}
$body = 'Hello $row[\'user_first_name\'] <br>';
sendUser($body);
Gives me:
PHP Parse error: syntax error, unexpected T_VARIABLE in /home/marc/z.php(5) : eval()'d code on line 1
Anything you pass in to eval() must be raw PHP code. It can't be plaintext with embedded <?php ?> PHP blocks - it has to be actual PHP code. When you fix up $body to account for this:
$body = 'echo "Hello {$row[\'user_first_name\']} <br>";';
Then you get:
Hello Fred <br>
I'm not exactly positive what you're trying to do, but I think your problem is in the definition of $body and your use of eval.
$body = 'Hello $row[\'user_first_name\'] <br>';
is not a valid line of php, and eval won't know what to do with it.
See if this fits what you want:
function senduser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
eval($body);
}
}
$body = 'echo "Hello {$row[\'user_first_name\']} <br>";';
sendUser($body);