String interpolation for built-in functions
You could do it like so:
$foo = 'bar';
$func = "strtoupper";
echo "test: {$func($foo)}";
//or for assignments:
$path = sprintf(
'%s/%s',
$dir,
basename($file)
);
example here
But really, you shouldn't: it obfuscates what you're actually doing, and makes a trivial task look a lot more complex than it really is (debugging and maintaining this kind of code is a nightmare).
I personally prefer to keep the concatenation, or -if you want- use printf
here:
printf(
'Test: %s',
strtoupper($foo)
);
Php string interpolation
The string interpolation rules are described thoroughly in the PHP manual. Importantly, there are two main styles:
- One with plain variables in the string, like
"the $adjective apple"
, which also supports some expressions like$foo->bar
in"the $foo->bar apple"
- One with curly brackets, which supports more complex expressions, like
{$foo->bar()}
, but the expression must start with a$
When PHP sees your string:
- it first sees
{count(
, but because the character after{
isn't$
, it doesn't mean anything special - it then sees the
$this->movies
, which is valid for the first syntax, so it tries to output$this->movies
as a string - because
$this->movies
is an array, you get a Warning and the string"Array"
is used
The result is that the string will always say "Your collection exists of {count(Array} movies."
.
There is no interpolation syntax currently that supports function calls like count()
, so you'll have to use concatenation or an intermediate variable:
return "Your collection exists of " . count($this->movies) . " movies.";
or
$movieCount = count($this->movies);
return "Your collection exists of {$movieCount} movies.";
or
$movieCount = count($this->movies);
return "Your collection exists of $movieCount movies.";
How can I manually interpolate a string?
Here is a possible solution. I am not sure in your particular scenario if this would work for you, but it would definitely cut out the need for so many single and double quotes.
<?php
class a {
function b() {
return "World";
}
}
$c = new a;
echo eval('Hello {$c->b()}.');
?>
How do I use PHP string interpolation and square brackets in a MySQL regexp?
Add braces: $search .= "and title REGEXP '[[:<:]]{$q}[[:>:]]'";
-- otherwise it thinks you are referencing the $q array.
PHP allows array lookups in strings, such as "... $q[...]
..." It also allows expresssions: "... {$a+$b} ..."
if you add the braces. Putting those together solves this problem.
Constants inside quotes are not printed?
Because "constants inside quotes are not printed". The correct form is:
echo "This is a constant: " . CONSTANT;
The dot is the concatenation operator.
What is the difference between these PHP string interpolation syntaxes
The first one is interpolation plus variable variable (dynamic variable), meaning you can use expressions here to define the name of the variable you want to interpolate "${func()}"
While the second one syntax is used to distinct variable from the text "some{$variable}text"
. You can actually combine them:
function func(){
return 'foo';
}
$foo = 'bar';
echo "some{${func()}}text";
Outputs: somebartext
PHP: Interpolating a PHP variable in a string block
Yes, it's called heredoc syntax:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
How to Access Magento User's Session from Outside Magento
PHP and Microsoft Access Database - Connection and Crud
Use Curly Brackets to Structure Code in PHP
Pdo Error: SQLstate[Hy000]: General Error: 2031
Call to Undefined Function Odbc_Connect() Message While Connecting Sap Hana Database
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce
Cron Command to Run Url Address Every 5 Minutes
How to Properly Use Bearer Tokens
Wordpress: Loading Multiple Scripts with Enqueue
Jquery Mobile: Sending Data from One Page to the Another
Differencebetween PHP Require and Include
PHP Sample Script for Pagination
Call to Undefined Method Domdocument::Getelementsbyclassname()