PHP Concatenation using {} [duplicate] - php

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

Related

Eval Php variable with double quotes inside [duplicate]

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.

Why concatenate? [duplicate]

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.

PHP variable interpolation vs concatenation [duplicate]

This question already has answers here:
Should I use curly brackets or concatenate variables within strings?
(3 answers)
Closed 6 years ago.
What is the difference between the two following methods (performance, readability, etc.) and what do you prefer?
echo "Welcome {$name}s!"
vs.
echo "Welcome " . $name . "!";
Whatever works best for you works...
But if you want to go for speed use this:
echo 'Welcome ', $name, '!';
The single quotes tell PHP that no interpretation is needed, and the comma tells PHP to just echo the string, no concatenation needed.

Insert a function result in the middle of PHP heredoc [duplicate]

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

i got this PHP question from my friend about variable dollar sign [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
what does $$ mean in PHP?
what is the different between $thisvariable and $$thisvariable. as you notice, the first variable has one dollar sign while the second got two dollar signs.
$variable is a variable and $$variable is a variable variables,
$my_name = "anthony"; // variable $my_name
echo $my_name; // outputs "anthony"
$a_var = "my_name"; // assigning literal to variable $a_var
echo $$a_var; // outputs "anthony"
It may be a bit confusing, so let's break down that echo call,
$($a_var)
-> $(my_name)
-> $my_name = "anthony"
Please note that the above may not be what happens behind the scenes of the PHP interpreter, but it serves strictly as an illustration.
Hope this helps.
$thisvariable is a variable named $thisvariable:
$thisvariable = 'Hello';
print $thisvariable; // prints Hello
$$thisvariable is a variable variable:
$thisvariable = 'Hello';
$Hello = 'Greetings';
print $$thisvariable; // prints Greetings ($$thisvariable is equivalent to $Hello)
For the most part you should avoid using variable variables. It makes the code harder to understand and debug. When I see it it's a red flag that there's some bad design.

Categories