Mixing a PHP variable with a string literal
echo "{$test}y";
You can use braces to remove ambiguity when interpolating variables directly in strings.
Also, this doesn't work with single quotes. So:
echo '{$test}y';
will output
{$test}y
PHP variables in HTML string
If you want to add vars into variable without concat then maybe you are looking for something like this
$information = "Info";
$msg = "<html><body>
<p>Bla-bla-bla $information</p></body></html>";
use your double quotes
Be aware if your var $information is array like this:
$information['info'] = "Info";
$information['id_info'] = 1;
$msg = "<html><body>
<p>Bla-bla-bla $information[info]</p>
<a href\"somepage.php?id=$information[id_info]\">Link</a></body></html>";
Double and single quotes
$email = 'someone@example.com';
$var = 'My email is $email'; // My email is $email
$var = "My email is $email"; // My email is someone@example.com
isset using mixed variable with string literal
you have to use variable variables. use
<?php
$label_test = "Hello";
$value = "test";
echo (isset(${"label_{$value}"})) ? "label_{$value}" : $label;
?>
PHP - concatenate or directly insert variables in string
Between those two syntaxes, you should really choose the one you prefer :-)
Personally, I would go with your second solution in such a case (Variable interpolation), which I find easier to both write and read.
The result will be the same; and even if there are performance implications, those won't matter 1.
As a sidenote, so my answer is a bit more complete: the day you'll want to do something like this:
echo "Welcome $names!";
PHP will interpret your code as if you were trying to use the $names
variable -- which doesn't exist.
- note that it will only work if you use "" not '' for your string.
That day, you'll need to use {}
:
echo "Welcome {$name}s!"
No need to fallback to concatenations.
Also note that your first syntax:
echo "Welcome ".$name."!";
Could probably be optimized, avoiding concatenations, using:
echo "Welcome ", $name, "!";
(But, as I said earlier, this doesn't matter much...)
1 - Unless you are doing hundreds of thousands of concatenations vs interpolations -- and it's probably not quite the case.
PHP string to contain and output another PHP variable
You cannot store strings with PHP interpolation symbols. Double-quoted strings are interpolated at runtime.
I'd say you have three options...
Store the various parts in your database and construct them in your query. For example (assuming MySQL)
SELECT
CONCAT(VideoProviders.embed_prefix, Article.video_url) AS embedcode,
-- etcwith
VideoProviders.embed_prefix
containing'https://www.youtube.com/embed/'
,'https://player.vimeo.com/video/'
, etcStore the URL with placeholders for the video id and (again assuming MySQL) use the
REPLACE
function. For exampleVideoProviders.embedcode = 'https://www.youtube.com/embed/{VIDEO_ID}'
and
SELECT
REPLACE(VideoProviders.embedcode, '{VIDEO_ID}', Article.video_url) AS embedcode,
-- etcThis would be the preferred solution if the video id doesn't always appear at the end of the URL.
Store a
printf
style pattern inembedcode
, eg'https://www.youtube.com/embed/%s'
and put it together with PHP, eg$embedcode = sprintf($row['embedcode'], $row['video_url');
Made a php variable and now can't run it
Try:
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
It should work.
When you write:
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
$quizinfo[…]
is not interpreted. It is taken as a string.
See also:
- Php variables inside strings
- PHP - concatenate or directly insert variables in string
- PHP: variables in strings without concatenation
- PHP: Beware of Variables Inside Strings
- Mixing PHP variable with string literal
Unterminated string literal - passing php variable to javascript function, from onclick handler, in echo block
Encode PHP variables as JSON before outputting in order to turn them into valid JavaScript literals.
e.g.
var s = <?php echo json_encode($val); ?>
Related Topics
PHP: How to Remove Specific Element from an Array
Is There a PHP Library For Email Address Validation
Why Does PHP 5.2+ Disallow Abstract Static Class Methods
Unformat Money When Parsing in PHP
Paypal Access - Ssl Certificate: Unable to Get Local Issuer Certificate
How to Login in With Curl and Ssl and Cookies
How to Execute Ssh Commands Via PHP
Extract Jsonp Resultset in PHP
PHP Get All Subdirectories of a Given Directory
Codeigniter Htaccess and Url Rewrite Issues
Creating Jquery Ajax Requests to a PHP Function
Creating a Thumbnail from an Uploaded Image
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions