This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling PHP functions within HEREDOC strings
I need to insert the results of functions into the middle of heredoc..
$r = func1();
$s = func2();
echo <<<EOT
line1
$r
$s
line3
EOT;
I am not sure if any better way to write it as I need to evaluate all the functions beforehand which make the code stupid.
Better if have something like..
echo <<<EOT
line1
{func1()}
{func2()}
line3
EOT;
Of course the above code does not work, but just want to show my idea...
No, there is no better way to do it.
Heredoc string syntax behaves much like double quote string syntax. You can't put a function name as an expression inside of the double quoted string any more than you can a heredoc. So what you're doing is fine and that's how you should be doing it. The only time you can get interpolation inside of string syntax in PHP is if the value is a variable.
For example...
$foo = function() { return 'value'; };
echo <<<SOMEHEREDOC
{$foo()}
SOMEHEREDOC;
// Is the same as
echo "{$foo()}";
// Is the ssame as
$foo = 'strtoupper';
echo "{$foo('hello world')}";
The above code would output
value
value
HELLO WORLD
Related
This question already has answers here:
How do I execute PHP that is stored in a MySQL database?
(7 answers)
Closed 4 years ago.
EDIT: This question has been edited from the original
I have a string in a database with HTML and PHP variable's inside. Some of the HTML has double quotes as if I try to use single quotes the database escapes it by adding a quote in front of it like so: ' '.
I want to query the string and assign it to variable $x. And then use eval("\$x = \"$x\";"); to parse the PHP variable, but it seems the double quote is ruining the eval(), and the variables are not parsing.
Is there a way to allow PHP to read the variable?
I am aware, but anyone reading this should also be aware that using eval() can be very dangerous!
Any help would be greatly appreciated!
If your SQL string looks like this: myVar then php:
$myVar = 'hello!';
echo $$var;
If your SQL string looks like this: 3 + 5 then php:
eval($var);
In first option we use Variable Variables
In second option we use eval to evaluate code in string.
This question already has answers here:
[PHP]: Error -> Too few arguments in sprintf();
(2 answers)
Closed 6 years ago.
I have a language.inc file which has a email template that looks like this:
$lang["email_templete"] = '
<style type="text/css">
*....more stylings here...*
</style>
<div>
<table>
*....more elements....*
%s
</table>
</div>
';
My problem is that whenever I'm trying to insert contents into my template using sprintf, it always hits the error "Too few arguments". I've tried removing all the excessive html codes and replaced it with a more simple html code and it works. Do you have any clue on this problem?
function add_to_email_template($pTargetEmail, $pSubject, $pContent) {
global $lang;
$vMessage = sprintf($lang["email_templete"], $pContent);
send_email($pTargetEmail, $pSubject, $vMessage);
}
Read the manual page for sprintf() and you'll see that the % sign is a format modifier of which your HTML and CSS code is filled with.
If you want to include % inside the string without getting it parsed, you need to escape it with itself such as %% will output %.
Note that it would probably be much simpler to user variables in your string definition than to use sprintf() the way you do, such as:
$variable = 'abc';
$myHTML = "Here is my variable: $variable";
This question already has answers here:
Should I use curly brackets or concatenate variables within strings?
(3 answers)
Closed 9 years ago.
I often see programmers on Youtube concatenating like: .$example.
Small question, I would like to know what the difference is between .$name.
and "$name" because they give the same output.
<?php
$name = 'Todd';
echo "Hello $name!";
echo "Hello " .$name. "!";
?>
When you use variables directly in a string literal, it is hard to read. You (usually) lose the benefit of your IDE showing you with different colors what is what. You can see this in the StackOverflow formatting of the code in your question.
If you're just using echo, consider using a list of strings instead:
echo 'Hello ', $name, '!';
No concatenation is needed, and each string is copied to the output buffer. In theory this is faster, but for typical small strings you certainly won't notice any speed difference.
Yeah both produces the same output.
In the below example, Variables inside the double quotes are interpreted.
$name = 'Todd';
echo "Hello $name!"; // prints "Hello Todd!"
See the same example when you need to show the same using single quotes.
$name = 'Todd';
echo 'Hello $name!'; // prints "Hello $name!"
In the above case , the concatenate operator comes to rescue. So you can echo 'Hello '.$name;// prints "Hello Todd!"
The concatenate operator has its own specialities , i just explained it for your context.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
curly braces in string
I still don't know what it's called. Like:
$name = 'xxx';
echo "This is a string {$name}";
What do you call that operation? Concatenating a variable using {} in to a string.
Thanks!
This is not concatenation ; this is variable interpolation ; see the Variable parsing section, in the PHP manual.
Basically, you can use any of the two following syntaxes :
echo "This is $variable";
Or :
echo "This is {$variable}";
And you'll get the same result in both cases -- except the second one allows for more complex expressions.
Concatenation would be something like this :
echo "This is my : " . $value;
Where the content of the variable $value is concatenated to the string, using the concatenation operator ..
It's often called string or variable interpolation.
How does {} affect a MySQL Query in PHP?
Don't let the question itself throw you - this answer gives you exactly what you are looking for.
And it's not concatenating; this is concatenating:
$myvar = "This is a string ".$name; // <<< Notice I'm concatenating the variable
// using the . operator
This question already has answers here:
What does $$ (dollar dollar or double dollar) mean in PHP?
(7 answers)
Closed 8 years ago.
what does two back to back $ behind a variable means. Like this
$$id
where can I find more information on that
Thanks
In PHP, $$ means you are about to inflict years of pain and suffering on at least one maintenance programmer. Note that you might wind up being that maintenance programmer.
It is a variable variable. Imagine this:
$quux = 'bar';
$foo[$quux] = "baz";
echo $foo['bar']; //prints baz
if there was no such thing as arrays, you might try something like this:
$quux = 'bar';
$$quux = "baz";
echo $bar; //prints baz
luckily we do have arrays so please don't use variable variables unless you are doing something convoluted and magical* and have no other choice.
*: Please don't do convoluted magical things, either.
These are called variable variables.
$foo = 'bar';
$id = 'foo';
echo $id; // prints foo
echo $$id; // prints bar
in the PHP manual of course
http://www.php.net/manual/en/language.variables.variable.php
note that it's obsolete and senseless syntax and you should always use arrays instead.