Solved

simple php question, how do i pass value from a function into another?

Posted on 2007-11-29
12
248 Views
Last Modified: 2008-02-01
This is in my index.php
<? include("body.php"); body("home"); ?>

As you can see its calling a function called "body" from a file called body.php
This is the body.php...
<? function body($pageaddress){
?>
     <? include("\"" ?> <? .$pageaddress. ?> <? .php"\""); ?>
<? } ?>

Can anyone point out where i've gone wrong? I'm very new to php programming.
0
Comment
Question by:Alan-Yeo
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 4

Expert Comment

by:simonkin
ID: 20375010
Try this...
<?php
 

	function body($pageaddress) {

	

		include("\\" . $pageadress . ".php");

	

	}

	

?>

Open in new window

0
 
LVL 17

Expert Comment

by:nplib
ID: 20375013
<?php
function body($pageaddress){
     include($pageaddress.".php"); ?>
}
?>
0
 
LVL 4

Expert Comment

by:simonkin
ID: 20375018
Sorry...


<?php
 

	function body($pageaddress) {

	

		include($pageadress . ".php");

	

	}

	

?>

Open in new window

0
 
LVL 1

Expert Comment

by:bestondoa
ID: 20375024
Hi Alan,

Try this instead of your current body function:

<?php function body($pageaddress){
      
      include("\.".$pageaddress.".php");
}
?>
0
 
LVL 17

Expert Comment

by:nplib
ID: 20375025
unless you want the slashes, but in a url slashes go the other way.
and if you have a slash at the beginning of your address it symbolizes the root of the website
0
 
LVL 17

Expert Comment

by:nplib
ID: 20375032
such as
<?php

function body($pageaddress){

     include("/".$pageaddress.".php"); ?>

}

?>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Alan-Yeo
ID: 20375062
sorry should make things a little clearer...
the function would actually look more like this...

<? function body($pageaddress){
?>
<table>
   <tr>
      <td>
         <? include("\"" ?> <? .$pageaddress. ?> <? .php"\""); ?>
      </td>
   </tr>
</table>
<? } ?>
0
 
LVL 17

Expert Comment

by:nplib
ID: 20375090
do this

<?php

function body($pageaddress){

 $html .= "<table>";

 $html .= "<tr>";

 $html .= "<td>";

 include("/".$pageaddress.".php");

 $html .= "</td>";

 $html .= "</tr>";

 $html .= "</table>";

 return $html;

}

?>
 
 

then to use it
 

echo body("home");

Open in new window

0
 

Author Comment

by:Alan-Yeo
ID: 20375139
include(/.php) [function.include]: failed to open stream: No such file or directory in
0
 
LVL 17

Accepted Solution

by:
nplib earned 400 total points
ID: 20375179
add this just to see if the variable is being passed
<?php

function body($pageaddress){

 echo $pageaddress;

 $html .= "<table>";

 $html .= "<tr>";

 $html .= "<td>";

 include("/".$pageaddress.".php");

 $html .= "</td>";

 $html .= "</tr>";

 $html .= "</table>";

 return $html;

}

?>

Open in new window

0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 100 total points
ID: 20375386
That will not work as expected because include will execute as soon as it is called you need something a bit more complex.
function body($pageaddress){

 ob_start();

 include($pageaddress.".php");

 $inc = ob_get_contents();

 ob_end_clean();

 $html .= "<table>";

 $html .= "<tr>";

 $html .= "<td>";

 $html .= $inc;

 $html .= "</td>";

 $html .= "</tr>";

 $html .= "</table>";

 return $html;

}

Open in new window

0
 
LVL 17

Expert Comment

by:nplib
ID: 20375819
yes your right.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CA single sign on 2 73
PHP extract HTML using DOMXpath from nested tables 8 48
WordPress Themes 10 34
PHP warning 4 30
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now