results in double quoted strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulkd
Forum Newbie
Posts: 1
Joined: Sat Aug 25, 2007 7:14 am

results in double quoted strings

Post by paulkd »

I'm from a ColdFusion background. I would like to know if it is possible to display the results of a function within a double quoted string. ie not using a dot to concatenate.

Coldfusion:

Code: Select all

<cfset astring="todays date is #now()#">
PHP:

Code: Select all

$astring = "todays date is getDate()";
I know I can assign a variable and embed it in the double quoted string, but I would like to simply embed the results of the function.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Did you check the manual? I'm sure it has an answer for you, but as far as I know, you can't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: results in double quoted strings

Post by superdezign »

paulkd wrote:I'm from a ColdFusion background. I would like to know if it is possible to display the results of a function within a double quoted string. ie not using a dot to concatenate.
No. When you put data into a double quoted string, PHP first looks for the '$' token (or a combination of braces and the '$' token, i.e. '{$...}') up to the first invalid character for a variable (or the closing bracket if you use it), which consists of non-alphanumeric characters. Then, it replaces that portion of the string with the data in the variable being referred to.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Your looking for:

Code: Select all

$astring = "todays date is " . getDate();
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: results in double quoted strings

Post by califdon »

paulkd wrote:I'm from a ColdFusion background. I would like to know if it is possible to display the results of a function within a double quoted string. ie not using a dot to concatenate.

Coldfusion:

Code: Select all

<cfset astring="todays date is #now()#">
PHP:

Code: Select all

$astring = "todays date is getDate()";
I know I can assign a variable and embed it in the double quoted string, but I would like to simply embed the results of the function.
You can't do it like you showed as an example, but you can do it this way, using the concatenation operator . :

Code: Select all

$astring= "todays date is " . getDate();
Post Reply