functions within print or echo

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
wallabee
Forum Newbie
Posts: 20
Joined: Mon Apr 21, 2003 10:01 pm

functions within print or echo

Post by wallabee »

is this possible? for instance:

Code: Select all

print("<textarea name="textarea$file" cols="40" rows="8">FillTextArea()</textarea>");
FillTextArea takes the text from a text file and simply fills the text area with said text (and suddenly my two recent posts seem to connect).

-ben
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

did you run it yet?
wallabee
Forum Newbie
Posts: 20
Joined: Mon Apr 21, 2003 10:01 pm

Post by wallabee »

it just puts, verbose, the words FillTextArea() in the text area.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

hmm..
Last edited by m3mn0n on Sat Apr 26, 2003 10:48 am, edited 1 time in total.
airo
Forum Commoner
Posts: 31
Joined: Thu Apr 24, 2003 4:07 pm

Post by airo »

or split t up into three echos. Techniquelly there is a way to do it in one line, but it gets alittle messy that way, and i prefer to split it up into 3 echos.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

<?php
function your_function() {
$string = "your displayed text";
return $string;
}



echo "Our function returns: " . your_function . "<br /><br />\n";
?>
Image Image
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Slight alteration to Phice's code (don't forget the parenthesis on the function name):

Code: Select all

echo "Our function returns: " . your_function() . "<br /><br />\n";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

wallebee - with your code you can do:

Code: Select all

print '<textarea name="textarea'.$file.'" cols="40" rows="8">'.FillTextArea().'</textarea>';
or, if you want double quotes around it

Code: Select all

print "<textarea name="textarea$file" cols="40" rows="8">".FillTextArea()."</textarea>";
Not a big fan of escaping attribute quotes myself but some people don't like having to concenate in their variables... General principle is the same though - you have to leave the string to run the function and then return to it after to add the rest of it.

Mac
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

I'm a old C programmer, so correct all the programmers i'm working with to use quotes as they sould.

The ' is for char's. Not for strings.
The " is for strings.

And if a user has something like

Code: Select all

echo "test $value $bool";
i correct it to

Code: Select all

echo "test ".$value." ".$bool;
Just make it more readable and understeadable for the progammer and the program.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

But in PHP, stuff in single quotes isn't parsed so this:

Code: Select all

echo 'test '.$value.' '.$bool;
would make more sense than

Code: Select all

echo "test ".$value." ".$bool;
in that you wouldn't be making unecessary work for the parser.

Mac
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

There is a differents? :?

So your say'ing that the ' things are faster to compile for php then " ?

Learned something, and that in the weekeind :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[]InTeR[] wrote:There is a differents? :?
Yup, check out:
http://uk.php.net/manual/en/language.types.string.php
[]InTeR[] wrote:So your say'ing that the ' things are faster to compile for php then " ?
probably won't make too much of a difference in small scripts but is more efficient.

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and since today's topic if efficiency:

Code: Select all

echo 'test ', $value, ' ', $bool;
doesn't even waste time and memory for string concatenations.

btw:
<smartass>
  • it's not that much about compile but runtime efficiency.

    Code: Select all

    echo "test $value $bool";
    the substitution takes place the moment the statement is executed.
</smartass>
;)
airo
Forum Commoner
Posts: 31
Joined: Thu Apr 24, 2003 4:07 pm

Post by airo »

I never knew there was a diffrence between ' and "... thanks mac!
Post Reply