Using PHP Code inside 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
srednatonmi
Forum Newbie
Posts: 15
Joined: Wed Mar 19, 2008 10:01 pm

Using PHP Code inside echo

Post 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?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using PHP Code inside echo

Post 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.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Using PHP Code inside echo

Post 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
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Using PHP Code inside echo

Post by LanceEh »

Both <ul>'s are going to execute regardless because they are not inside the <?php?> statement
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Using PHP Code inside echo

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using PHP Code inside echo

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using PHP Code inside echo

Post 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.
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Using PHP Code inside echo

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using PHP Code inside echo

Post 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.
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Using PHP Code inside echo

Post 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.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Using PHP Code inside echo

Post 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');
Last edited by JasonDFR on Sun Mar 15, 2009 11:02 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using PHP Code inside echo

Post 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.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Using PHP Code inside echo

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using PHP Code inside echo

Post 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 :crazy:
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Using PHP Code inside echo

Post 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.
Post Reply