Forum Moderators: coopster

Message Too Old, No Replies

Using echo statement within include

Using echo statement within include

         

Sofia_A

2:15 pm on Dec 18, 2010 (gmt 0)

10+ Year Member



I know this is basic, which is why it's irritating me that I can't work it out.

My statements are thus:


<?php include ("authorspage.php"); ?>

<?php echo "$thisAuthor"?>

I want to do this:

<?php include ("<?php echo "$thisAuthor"?>.php"); ?>

or so you can see more clearly: <?php include ("$thisAuthor.php"); ?>


But obviously I can't put the echo like that within this statement.

Help? :)

jatar_k

1:24 pm on Dec 19, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can build the filename into a variable

$myfile = $thisAuthor . '.php';
include $myfile;

or as one statement

include $thisAuthor . '.php';

Matthew1980

3:44 pm on Dec 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Sophia_A,

The example as you have given isn't that far from what you actually want, the only thing is the way as you construct the instruction.

Your example

<?php include ("<?php echo "$thisAuthor"?>.php"); ?>

What is should read!

<?php include($thisAuthor.".php"); ?>

The key in this is the use of the full stop and the double quote, essentially what's happening is called concatenation, where you join php statements together, then in required use the " to add a string to the end of a variable, or at the start of a variable:-

echo "This is joined text to a ".$variable;

And as Jatar_K points out with his example, include statements don't necessarily require the parenthesis to work, there are a few instructional statements like that within php, exit; is another one, you can use it like: exit(); or just like: exit; Either of these will do the same thing. It's purely a preference thing, and something as you will come do do automatically when coding new projects.


I hope that this makes things clearer for you.

Cheers,
MRb

Sofia_A

5:19 pm on Dec 19, 2010 (gmt 0)

10+ Year Member



There was another reply to this friday, and I'd solved it, where did those messages go to? Weird.

My actual final code was <?php include("$thisAuthor.php"); ?>


This (what you put) <?php include($thisAuthor.".php"); ?> was what I initially tried (though mistyped it in my question, doh) and it didn't work.

How come there are TWO threads for this post, each with different people responding?

Thanks for your help anyways ;)

Sofia_A

5:20 pm on Dec 19, 2010 (gmt 0)

10+ Year Member



Here's the toehr thread:

[webmasterworld.com...]

I only posted it once though... odd. Apols for wasting your time gentlemen!

Matthew1980

5:59 pm on Dec 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need to apologise, you got there in the end.

Have fun with the rest of your project!

Cheers,
MRb