How do I use the result of a function in a string?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Stokestack
New php-forum User
New php-forum User
Posts: 7
Joined: Tue Apr 26, 2011 5:43 pm

How do I use the result of a function in a string?

Postby Stokestack » Mon Mar 12, 2012 4:02 pm

Hey guys,

I'd expect to find this pretty easily, but so far haven't. I'm building a string for an SQL query, and want to use the result of a function in the middle of it. How do you do it? I tried enclosing the function call in braces in the middle of the string, but it doesn't like it. It's a global function, and it works fine if I assign its result to a variable and use the variable in braces in the string.

Thanks.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: How do I use the result of a function in a string?

Postby simplypixie » Tue Mar 13, 2012 10:09 am

Please post the string that you want to extract from and advise which part you want to extract so that we can help.

Stokestack
New php-forum User
New php-forum User
Posts: 7
Joined: Tue Apr 26, 2011 5:43 pm

Re: How do I use the result of a function in a string?

Postby Stokestack » Tue Mar 13, 2012 1:56 pm

Thanks, but I'm not extracting from a string. I want to embed the result of a function INTO a string.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: How do I use the result of a function in a string?

Postby simplypixie » Tue Mar 13, 2012 10:58 pm

We still need an example of what you are trying to do in order to help.

LarryMade
New php-forum User
New php-forum User
Posts: 41
Joined: Wed Mar 14, 2012 5:21 am

Re: How do I use the result of a function in a string?

Postby LarryMade » Wed Mar 14, 2012 5:28 am

"SQL QUERY ".function(args)." MORE SQL QUERY"

You can't embed an instruction in a string like you can a variable, you just need to chain it.

Stokestack
New php-forum User
New php-forum User
Posts: 7
Joined: Tue Apr 26, 2011 5:43 pm

Re: How do I use the result of a function in a string?

Postby Stokestack » Wed Mar 14, 2012 9:57 pm

Here's what I ended up doing, since so far there has been no answer:

Code: Select all

      $GMTOffset = timeOffsetFromGMT();

      $query = "SELECT itemID, stashID, itemFilename, itemLocator, addedByID, (SELECT DATE_ADD(addDate, INTERVAL {$GMTOffset} HOUR)) AS 'addDate', caption, reviewed, ";


The question is how to put timeOffsetFromGMT() into the string directly.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: How do I use the result of a function in a string?

Postby simplypixie » Wed Mar 14, 2012 11:00 pm

You had not had an answer as you had not provided the information as I asked you to do. Now you have provided the code showing what you are trying to do, I understand and can help.

You do it by enclosing in quote marks as such:

Code: Select all

$query = "SELECT itemID, stashID, itemFilename, itemLocator, addedByID, (SELECT DATE_ADD(addDate, INTERVAL '".timeOffsetFromGMT()."' HOUR)) AS 'addDate', caption, reviewed, ";

Stokestack
New php-forum User
New php-forum User
Posts: 7
Joined: Tue Apr 26, 2011 5:43 pm

Re: How do I use the result of a function in a string?

Postby Stokestack » Sat Apr 06, 2013 4:18 pm

In retrospect, the concatenation seems kind of obvious. Thanks.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest