Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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.
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 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.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

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.
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 simplypixie » Tue Mar 13, 2012 10:58 pm

We still need an example of what you are trying to do in order to help.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

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.
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 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.
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 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, ";
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

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.
Stokestack
New php-forum User
New php-forum User
 
Posts: 7
Joined: Tue Apr 26, 2011 5:43 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 8 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron