View PHP code after includes - php

Basically, I need to view the PHP code of a file, after includes. I am trying to see EXACTLY what PHP code is run. eg...
<?php // a.php
$a = 10;
?>
<?php // b.php
include('a.php');
$b = 20;
?>
If I was trying to get the code of b.php, it would display the following:
<?php
$a = 10;
$b = 20;
?>
Is that possible? If so, how?

// at the end of your script
<?php
$totalRunCode = '';
$scripts = get_included_files();
foreach($scripts as $script)
{
$totalRunCode .= file_get_contents($script);
}
// do whatever with totalRunCode
Though I don't know why you'd want to do this.

Related

echo variable with variable in included.php

I have two php files.
One is action.php and the other is include.php
I need to echo variable form include.php in my other scripts.
Basically in include.php there are this variables
$id= $web_id;
$start = "Hello, ";
$end = "works.";
In my action.php I have this variables
$web_id = "testing echo from variable in include.php";
echo $start; // variable is in include.php
echo $id; //variable is in include.php
echo $end; //variable is in include.php
Result is
Hello, works.
Wham I am trying to achieve is
Hello, testing echo from variable in include.php works.
This syntax echos only empty value for $id.
What is the right syntax for echoing variable with variable in included file?
You need to set your $web_id variable before you do your include. Take a look at this example:
action.php
$web_id = 'this'.
include 'include.php';
echo $start . $id . $end;
include.php
$id = $web_id;
$start = 'Hello ';
$end = ' works.';

Php enclosure as a variable?

A begginers question. I have this little code:
<?php
$content = 'hello there, hello!';
echo substr_count("$content","hello");
?>
How could i replace 'hello there, hello!' part with another php enclosure, like
<?php the_content(); ?>
for example. What is the correct way of doing this? Thanks!
<?php
echo substr_count(the_content(),"hello");
?>
This:
<?php
$a = 10;
?>
<?php
$b = 20;
?>
<?php
echo $a + $b;
?>
--output:--
30
is equivalent to:
<?php
$a = 10;
$b = 20;
echo $a + $b;
?>
If the_content does not return a meaningful value but rather echos things out, use output buffering:
ob_start();
the_content();
$captured_content = ob_get_clean();
echo substr_count($captured_content, "hello");
$content = the_content();
echo substr_count($content,"hello");
or
echo substr_count(the_content(),"hello");
It is really the basics, try to look at some tutorials ;)

calling a function with require but variables and arrays are undefined

Why aren't my variable seen when called with require ?
function.php
<?php
function paginator(){
$links = array("index.php", "services.php", "content.php","contact_us.php" );
$trimslug = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
foreach ($links as $key => $value) {
if ($value == $trimslug ) {
$GLOBALS['$page'] = $key;
}
}
$page = $GLOBALS['$page'];
$next = $page+1;
$previous = $page-1;
}
?>
content.php
<?php
session_start();
require './functions.php';
paginator();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pagination</title>
</head>
<body>
<h2>Now on Page : <?php echo $page?></h2>
<a href="<?php echo $links[$next] ?>" >Next</a>
<br><br><br>
<a href="<?php echo $links[$previous]?>" >Previous</a>
<br>
</body>
</html>
I would like to be able to see my variables, when using the require function as this piece of code will be on every page. This might be a very noobish concept to grasp but I would really like someone to illustrate the concept properly.
This seemed to work, Thank you everyone.
<?php
$links = array("index.php", "services.php", "content.php","contact_us.php" );
$trimslug = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
$page = null;
function paginator(){
global $links,$trimslug,$next,$previous,$page;
foreach ($links as $key => $value) {
if ($value == $trimslug ) {
// $GLOBALS['$page'] = $key;
$page = $key;
}
}
$next = $page+1;
$previous = $page-1;
}
?>
The variables inside paginator are only in the scope of the function, not the php file. If you want to access them outside that function, just move those variables outside of it. Eg
$page=null;
$links=...
function paginator(){
...
}
This is because the variables are defined in the scope of the function paginator();
If you want them to be accesible in the scope of content.php, either declare them like this:
global $variable = 'value';
Or, just declare them in function.php without the need of the function & it's subsequent call in content.php.
Variables in PHP are limited to the scope of the function, unless called via an argument or by adding to the global array.
Global arrays are bad practice, just sayin.
You could always make the variables into a private class and call it as needed, though that's pretty tricky for beginners.

PHP create page as a string after PHP runs

I'm stuck on how to write the test.php page result (after php has run) to a string:
testFunctions.php:
<?php
function htmlify($html, $format){
if ($format == "print"){
$html = str_replace("<", "<", $html);
$html = str_replace(">", ">", $html);
$html = str_replace(" ", "&nbsp;", $html);
$html = nl2br($html);
return $html;
}
};
$input = <<<HTML
<div style="background color:#959595; width:400px;">
<br>
input <b>text</b>
<br>
</div>
HTML;
function content($input, $mode){
if ($mode =="display"){
return $input;
}
else if ($mode =="source"){
return htmlify($input, "print");
};
};
function pagePrint($page){
$a = array(
'file_get_contents' => array($page),
'htmlify' => array($page, "print")
);
foreach($a as $func=>$args){
$x = call_user_func_array($func, $args);
$page .= $x;
}
return $page;
};
$file = "test.php";
?>
test.php:
<?php include "testFunctions.php"; ?>
<br><hr>here is the rendered html:<hr>
<?php $a = content($input, "display"); echo $a; ?>
<br><hr>here is the source code:<hr>
<?php $a = content($input, "source"); echo $a; ?>
<br><hr>here is the source code of the entire page after the php has been executed:<hr>
<div style="margin-left:40px; background-color:#ebebeb;">
<?php $a = pagePrint($file); echo $a; ?>
</div>
I'd like to keep all the php in the testFunctions.php file, so I can place simple function calls into templates for html emails.
Thanks!
You can use output buffering to capture the output of an included file and assign it to variable:
function pagePrint($page, array $args){
extract($args, EXTR_SKIP);
ob_start();
include $page;
$html = ob_get_clean();
return $html;
}
pagePrint("test.php", array("myvar" => "some value");
And with test.php
<h1><?php echo $myvar; ?></h1>
Would output:
<h1>some value</h1>
This may not be exactly what you're looking for but it seems you want to build an engine of sorts for processing email templates into which you can put php functions? You might check out http://phpsavant.com/ which is a simple template engine that will let you put in php functions directly into a template file as well as basic variable assignment.
I'm not sure what printPage is supposed to be doing but I would re-write it like this just to make it more obvious because the array of function calls is a bit complicated and I think this is all that is really happening:
function pagePrint($page) {
$contents = file_get_contents($page);
return $page . htmlify($contents,'print');
};
and you might consider getting rid of htmlify() function and use either of the built-in functions htmlentities() or htmlspecialchars()
Seems like my original method may not have been the best way of going about it. Instead of posing a new question on the same topic, figured it was better to offer an alternate method and see if it leads to the solution I am after.
testFunctions.php:
$content1 = "WHOA!";
$content2 = "HEY!";
$file = "test.html";
$o = file_get_contents('test.html');
$o = ".$o.";
echo $o;
?>
text.php:
<hr>this should say "WHOA!":<hr>
$content1
<br><hr>this should say "HEY!":<hr>
$content2
I'm basically trying to get $o to return a string of the test.php file, but I want the php variables to be parsed. as if it was read like this:
$o = "
<html>$content1</html>
";
or
$o = <<<HTML
<html>$content1</html>
HTML;
Thanks!

Imported file being added to the start of the page

I'm using the following function to open a file:
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
Here's the code where it's called:
<?php
include($_SERVER['DOCUMENT_ROOT']."/class_lib.php");
$page = new Page();
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
$page->meta = array
(
'title' => 'snip',
'description' => 'snip'
);
$page->content = "
snipsnipsnipsnipsnipsnip
<div id=\"foo\">
<pre>
".example()."
</pre>
</div>
<br/>snipsnip
";
$page->Display();
?>
For some reason, even though the function is called within the pre element, it appears at the start of the page (the file is output, then the html is loaded). Same thing happens when I use include(). I must overlooking something obvious... any ideas?
Here's the class_lib.php if it's needed: http://pastebin.com/7euqEWNq
You use echo when reading your file, so it's directly printed out.
You can used output buffering : ob_start
Or simply get the file content and use it instead or you function call : file_get_contents
What you should to is let the example method return a value as the result and than echo that at the position you are calling it from. Like so:
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
return $foo2;
}
}
$exampleData = example();
$page->content = "snipsnipsnipsnipsnipsnip
<div id=\"foo\">
<pre>".$exampleData."</pre>
</div>
<br/>snipsnip";
I guess that's because your echo sentence, the first thing to be sent to the browser is the echo sentence output, so it shows first no matter where you place it.
You should return not echo, something like this:
function example() {
$output = '';
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
$output .= $foo2;
}
return $output;
}

Categories