My data are usually taken from mysql however that's not the issue. My problem is i want to be able to call few functions inside an echo. I dont even know if it is possible. Im almost new to php Please any help is appreciated.
<?php
echo"
<div class='container'>
<select name='science'>
<?php science_facts(); ?>
</select>
</div>
";
?>
or
<?php
echo"
<div class='container'>
<select name='science'>
science_facts();
</select>
</div>
";
?>
Both not working.
You have to break up the string into its static and dynamic parts. There are numerous ways to do this but the two most common are to use multiple echo statements:
echo "<div class='container'>";
echo "<select name='science'>";
echo science_facts(); // Note no quotes!
echo "</select>";
echo "</div>";
Or use the .
string concatenation operator to join strings on the fly:
echo "<div class='container'>" .
"<select name='science'>" .
science_facts() .
"</select>" .
"</div>";
Or, the same in one line:
echo "<div class='container'><select name='science'>" . science_facts() . "</select></div>";
Or start/stop PHP execution explicitly:
...
?>
<div class='container'>
<select name='science'>
<?php echo science_facts(); ?>
</select>
</div>
<?php
...
(Note: This assumes that your science_facts()
function returns its output rather than echoing it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With