Insert GET variable into string in PHP [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
This works:
Welcome <?php echo $_GET["name"]; ?><br>
But this doesn't:
'html' => 'Hello $_GET["name"];',
How should I code this?

it should be echo "Hello $_GET['name']";, single quotes show exact values, you can use double quotes if you are using variables.

echo "Hello {$_GET["name"]}";
This should do.
Correction:
Its not a good practice to use double quotes inside double quotes as pointed out in comments. So the correct thing should be Hello {$_GET['name']}

Related

PHP Using curly braces to print variable works different than expected [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I want to print a string in PHP that contains html, my code is:
<? if (!empty($dias)) {echo 'value="{$dias}"';}?>
but it prints value="{$dias}"
What I want to achieve is something similar to
<? if (!empty($dias)) {echo 'value="'.$dias.'"';}?>
to print value="10". Of course I dont want to concatenate.
Change quotes:
<? if (!empty($dias)) {echo "value='{$dias}'";}?>
// or
<? if (!empty($dias)) {echo "value=\"{$dias}\"";}?>
For more details see: What is the difference between single-quoted and double-quoted strings in PHP?
Substitution only happens with strings enclosed in double quotes, so you'd use:
<? if (!empty($dias)) {echo "value=\"{$dias}\"";}?>

What's better to use: ' or " [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
What is better to use in PHP?
' or "? for something like this:
<?=$json["response"]["players"][0]["personaname"];?>
And this:
$steamid = "";
"Better" really depends on your use cases. In both your example, the single quote is better for the perfomance because you have no $variable interpolation needed.
Otherwise "better" may be a question of style (and a little of performance).
Read the documentation for more : http://php.net/manual/en/language.types.string.php
Better? Well, let's just say that an empty string with single quotes is the most basic string literal you can use. In your example shown, I would use single quoted strings for array elements and the empty string. PHP will know that the string literal is only a string literal, and that nothing else needs to be "interpolated." Better? Who knows.
One place to use "" is with escape sequences such as "\n" .

What does echo <<<output do? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I came across some code in a file I was working that basically has
echo <<<output
"some html here"
output;
What does this do? Normally I do something like
echo "some html here";
but I've run into some cases where I have to use both ' and " and then it breaks the statement to echo, I think the first method would be a way around this but I would like to know what it's actually doing.
Thanks in advance.
You are looking at heredoc notation.
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

How to enter variable into string? [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
Here's my code:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=$name');
It doesn't appear to be using $name correctly. How would I add that variable into my string like I'm trying to do?
Change the single quotes to double quotes. PHP variables are not interpolated when in single quotes.
$jsonData = file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=$name");
You can also use concatenation here:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=' . $name);
Either change the single quotes around your string to double quotes:
"http://example.com/bin/serp.php?engine=google&phrase=$name"
Or use string concatenation with the . operator:
'http://example.com/bin/serp.php?engine=google&phrase=' . $name
Both of these techniques are mentioned on PHP's Strings documentation.

PHP programming help - Am I doing something wrong? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
In PHP, do both ( " ) and ( ' ) have the same effect? I'm new to PHP and I've been using them interchangeably. Can I?
Yes you can use single quoted or double quoted strings but they have some differences. Take a look at php string type.
You you can use them interchangeably, however following are differences:
Inside double quotes, php is able to parse variables for example
"Hello $name" // result: Hello [whatever stored in $name eg John]
Inside single quotes php is not able to parse variables:
'Hello $name' // result: Hello $name
Since php does not parse variables from single quotes, using single quotes is slightly faster.
More Information:
http://php.net/manual/language.types.string.php
$name = 'Amar';
echo "Hello, $name"; // outputs Hello, Amar
echo 'Hello, $name'; // outputs Hello, $name
Single quotes ( ' ) are faster by a very small margin since they don't need to scan strings for variables.

Categories