How can I print php variables in text file? - php

I want to print php variable in a text file.
Example :
<?php
$name='x' ;
?>
I want to put name value in text file for print it as hard copy .

Following is how to accomplish it.
<?php file_put_contents('filename', 'content');
Have a look at this function at PHP.net

Since your question does not include anything you have tried on your own (Which is generally recommended). Here is somewhere for you to start - the php file_put_contents function.
You can try something like this:
<?php
$file = 'some-file.txt';
$name = "x";
// Write the contents to the file
file_put_contents($file, $name);
?>
A few Google searches will surely provide some insight here as well.
Hope it helps!

Related

how to get the content of another php file without execution? [duplicate]

How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php

How to read raw variables from a file in PHP?

This may be simple to do, sorry if this is quite broad. I am fine with looking for any alternatives, but I would just like some help.
I need to be able to read variables from a file in PHP.
For instance, I would have a file like this:
$test = 10
$greeting = "Hello"
And PHP would be able to take these values and actually use them. The "code" above would be the entire file, add or take a few variables. The reason why I need to do it like this is so that other PHP files can also write variables to it. Is there any method that can do this, or something similar to this.
Thanks
Edit: This file would be a TXT file; it's not meant to be .php
Read lines from your text file:
<?php
$myfile = fopen("variables.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
//echo fgets($myfile) . "<br>";
// <Your code logic here.>
}
fclose($myfile);
?>
For each iteration of the while loop do whatever it is you need to do with the values read in from the text file.
I found a method. I'm still fairly new to PHP, so I'm using it quite strictly. I've decided to make a never ending PHP file, with all my variables in it:
<?php
//File name is variables.php
$test = 10;
$greeting = "Hello";
So then other PHP files can still write to it and other PHP files can read from it:
<?php
include "variables.php";
echo $greeting;
Output would be Hello.
Thank you for all your help everyone.

Read a .php file using php

How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php

Include text in file without <?php ?> tags [file_get_contents]

I have noobish question for you guys. I'm making my private sandboxie where you can test your code. So for now what I'm making is that you write the code, code is executed and you can see the result BUT i want the code you written in the textbox.
Now the problem is, I want that text from .php file to be shown without tags. I might be idiot but I just don't remember how to do it.
so actually code is like
if($_GET['f']){
echo file_get_contents("files/".$_GET['f'].".php");
}else{
echo "echo \"Hello world\";";
}
if you don't understand what I want I can post more info etc but I think its obvious:)
Do you mean something like
<?php
echo str_replace(array('<?php', '?>'), '', file_Get_contents('file.php'));
?
The include function :)
include("files/" . $_GET['f'] . ".php");
But! Never read a file directly from $_GET. Someone could put in ../../sensitiveinformation.txt or something.
Use include(); and require(); would help you include the file. Use eval(); to execute the code.
To see the actual source of the file and strip the <?php ?> tags do this
$file = file_get_contents('time.php');
$ll = array('<?php','?>');
echo str_replace($ll, "", $file);

Include whole content of a file and echo it

I need to echo entire content of included file. I have tried the below:
echo "<?php include ('http://www.example.com/script.php'); ?>";
echo "include (\"http://www.example.com/script.php\");";
But neither works? Does PHP support this?
Just do:
include("http://www.mysite.com/script.php");
Or:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
This may slow down your page due to network latency or if the other server is slow.
This requires allow_url_fopen to be on for your PHP installation. Some hosts turn it off.
This will not give you the PHP code, it'll give you the HTML/text output.
Shortest way is:
readfile('http://www.mysite.com/script.php');
That will directly output the file.
Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it
include ('http://www.mysite.com/script.php');
You don't need to print out PHP source code, when you're writing PHP source code.
Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.
If "script.php" is a local file, you could try something like:
$file = file_get_contents('script.php');
echo $file;
This may not be the exact answer to your question, but why don't you just close the echo statement, insert your include statement, and then add a new echo statement?
<?php
echo 'The brown cow';
include './script.php';
echo 'jumped over the fence.';
?>
Matt is correct with readfile() but it also may be helpful for someone to look into the PHP file handling functions
manual entry for fpassthru
<?php
$f = fopen($filepath, 'r');
fpassthru($f);
fclose($f);
?>

Categories