Jump to content

[NEWBIE ALERT] Putting functions into echo statements/other functions?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi all again,

Please see this bit of code:

$sentence = 'Lucas and Amanjot were dancing in the streets.';

$pos = strpos($sentence, "dancing");


echo($sentence) . "<br />";

echo("The word dancing is in the following position: $pos");

Instead of assigning the strpos() function to a variable, is there a way to put it into the echo statement directly? I tried a few times and wasn't getting anywhere. It didn't seem that it was recognizing that it was a function instead of text. However, I thought the double " instead of ' would parse it correctly. And further to this point, functions within other functions (within other, other, other functions) work the same way?

Thoughts?

Thanks again for all the clarification and help!

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
$sentence = 'Lucas and Amanjot were dancing in the streets.'; 


echo $sentence . "<br />"; 

echo "The word dancing is in the following position: " . strpos($sentence, "dancing");  


#3
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi John,

Thanks for the response. It's working great. I think I need to stop getting into the habit of doing echo() instead of echo " "... the extra brackets (and their placement) is messing me up I think in the syntax.

I have a friend who always does echo("This is text") instead of echo "This is text" and its getting me confused.

Cheers!

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
there is nothing wrong doing:
echo ("The word dancing is in the following position: " . strpos($sentence, "dancing"));
It's as I said in the other thread. It's just a matter of taste.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks Orjan. I realize preference can play a role - but does efficiency play a part in this for optimizing code/data?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users