Page 1 of 1
Using PHP Code inside echo
Posted: Sat Mar 14, 2009 2:16 am
by srednatonmi
I have the following code, which prints any html inside an echo, but refuses to process the PHP. What am I doing wrong?
Code: Select all
<?
include("tabs.php");
$tabs = new tabs("example1");
$tabs->start("Top Ten - This Week");
echo "Test
<?php if (function_exists('get_highest_rated_range')): ?>
<ul>
<?php get_highest_rated_range('7 days'); ?>
</ul>
<?php endif; ?><br/>";
$tabs->end();
$tabs->start("Top Ten - This Month");
echo "Test 2
<?php if (function_exists('get_highest_rated_range')): ?>
<ul>
<?php get_highest_rated_range('1 month'); ?>
</ul>
<?php endif; ?><br/>";
$tabs->end();
$tabs->start("Top Ten - All Time");
echo "Test 3
<?php if (function_exists('get_highest_rated')): ?>
<ul>
<?php get_highest_rated(); ?>
</ul>
<?php endif; ?><br/>";
$tabs->end();
$tabs->run();
?>
Can't echo run php functions as well?
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 2:23 am
by php_east
no, but in your case that is not the problem, you do not need to redeclare <?php inside your echos. you can swing in and out of php/html as many times, just take care not to loose step.
for example.
Code: Select all
echo "Test 3";
if ( function_exists('get_highest_rated') )
{ ?>
<ul>
<?php get_highest_rated(); ?>
</ul>
<?php
}
so the question really is not wether echo can execte php, but rather, are you in php or in html ?
<?php brings you into php
?> escapes you from php into html.
and it would help much if you get an editor with syntax highlighter capability.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 10:06 am
by JasonDFR
You can evaluate code inside an echo statement. For example:
Code: Select all
<?php
function get_highest_rated_range($var) {
return $var;
}
echo (function_exists('get_highest_rated_range')) ? get_highest_rated_range('7 days') : "function doesn't exist";
exit;
However, in srednatonmi's case, php_east was right, you were already inside php tags when used echo. You probably just need an if statement and echo everything inside it.
Good luck,
J
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 12:19 pm
by LanceEh
Both <ul>'s are going to execute regardless because they are not inside the <?php?> statement
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 12:31 pm
by LanceEh
I'm not sure what the function does, but if it prints a list, your code should look like:
Code: Select all
echo 'Test';
if (function_exists('get_highest_rated_range')) {
echo '<ul>';
get_highest_rated_range('7 days');
echo '</ul>';
}
Hope this helps.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 1:03 pm
by php_east
JasonDFR wrote:You can evaluate code inside an echo statement. For example:
Code: Select all
<?php
function get_highest_rated_range($var) {
return $var;
}
echo (function_exists('get_highest_rated_range')) ? get_highest_rated_range('7 days') : "function doesn't exist";
exit;
J
well i suppose you could, but hmmpph, i don't know. it just doesn't seem right.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 1:08 pm
by php_east
LanceEh wrote:Both <ul>'s are going to execute regardless because they are not inside the <?php?> statement
how are we to know which set of codes you are talking about.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 1:12 pm
by LanceEh
php_east wrote:LanceEh wrote:Both <ul>'s are going to execute regardless because they are not inside the <?php?> statement
how are we to know which set of codes you are talking about.
I meant all of them, but I am not sure of the <?php if?> <?php endif ?> statements, they may funtion differenly than just doing the old
<?php
if {
}
else {
{
?>
If so disregard what I said.
Cheers.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 1:57 pm
by php_east
LanceEh wrote:
If so disregard what I said.
Cheers.
yes, the if { ?> HTML <?php } ?> is normally used to induce PHP driven html output based on true/false. so i will disregard your remark, don't want to confuse @srednatonmi which works and which won't.
The example i posted applies. i was just trying to show @srednatonmi how to wiggle in and out of PHP/HTML, and this i think would be among the more difficult concepts to adjust to for one learning PHP. but certainly the most useful one and perhaps essential.
cheers.
Re: Using PHP Code inside echo
Posted: Sat Mar 14, 2009 4:27 pm
by LanceEh
yes, the if { ?> HTML <?php } ?> is normally used to induce PHP driven html output based on true/false. so i will disregard your remark, don't want to confuse @srednatonmi which works and which won't.
The example i posted applies. i was just trying to show @srednatonmi how to wiggle in and out of PHP/HTML, and this i think would be among the more difficult concepts to adjust to for one learning PHP. but certainly the most useful one and perhaps essential.
Gotcha, thanks

Guess I'm too old school sometimes

haha
Take care.
Re: Using PHP Code inside echo
Posted: Sun Mar 15, 2009 7:38 am
by JasonDFR
php_east wrote:
well i suppose you could, but hmmpph, i don't know. it just doesn't seem right.
I think it is absolutely right depending on the circumstances of course. It allows for much more concise and compact code. Which I hope everyone can agree is a good thing. This isn't something someone starting just starting out would be doing, but is a technique that should eventually be used.
For example this drawn out, although clear, code:
Code: Select all
<?php
session_start();
$_SESSION['user_name'] = 'php_east';
if ( !empty($_SESSION['user_name']) ) {
echo 'Hello ' . $_SESSION['user_name'];
} else {
echo 'Hello Guest';
}
As opposed to what I think is the better way to do it:
Code: Select all
<?php
session_start();
$_SESSION['user_name'] = 'php_east';
echo 'Hello ' . (!empty($_SESSION['user_name']) ? $_SESSION['user_name'] : 'Guest');
Re: Using PHP Code inside echo
Posted: Sun Mar 15, 2009 7:44 am
by Benjamin
JasonDFR wrote:You can evaluate code inside an echo statement.
Yes you can. It's best to keep the logic separate from the html as much as possible however, even if they are both in the same file.
Re: Using PHP Code inside echo
Posted: Sun Mar 15, 2009 8:31 am
by JasonDFR
astions wrote:JasonDFR wrote:You can evaluate code inside an echo statement.
Yes you can. It's best to keep the logic separate from the html as much as possible however, even if they are both in the same file.
I agree 100%. 90%of the code I write is MVC. View logic however is different than Controller logic.
I am interested to hear how you display a "Welcome user" type message.
Re: Using PHP Code inside echo
Posted: Sun Mar 15, 2009 10:39 am
by php_east
JasonDFR wrote:I think it is absolutely right depending on the circumstances of course. It allows for much more concise and compact code. Which I hope everyone can agree is a good thing. This isn't something someone starting just starting out would be doing, but is a technique that should eventually be used.
For example:
Code: Select all
<?php
session_start();
$_SESSION['user_name'] = 'php_east';
if ( !empty($_SESSION['user_name']) ) {
echo 'Hello ' . $_SESSION['user_name'];
} else {
echo 'Hello Guest';
}
As opposed to:
Code: Select all
<?php
session_start();
$_SESSION['user_name'] = 'php_east';
echo 'Hello ' . (!empty($_SESSION['user_name']) ? $_SESSION['user_name'] : 'Guest');
i understand what you say, but got thrown off track when i read the example.
do you mean is it better to code as in your first example better than ( as opposed to ) in your second. it contradicts your statement abut consice clear codes. the first one is clear to me, but the second is more concise. so i'm a little dizzy

Re: Using PHP Code inside echo
Posted: Sun Mar 15, 2009 11:00 am
by JasonDFR
Yeah, I probably should have put the second piece of code first. You're right, the as opposed to makes my point a bit blurry.
My point was to respond to you saying, "just doesn't seem right". I think that depending on the circumstances, evaluating code inside an echo statement can be a better way to go.