getting php as text on output - php

first of all this is my php code. i am trying to get this code as text on output. not sure how i do that. with html we can do \" to insert it inside php but how do i get this done like that on my code ?
<?php
$stringData = "
// Start here
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo 'Hello';
?>
// End here
";
?>
i have marked that part that i want to get it on output as text but when i put it like that on page i get syntax error not sure why.
EDIT #2
here below is my full code and i explain how my code works and for what.
my code is to create page and put something inside that page created
<form method="post">
<label>Page Name:</label><br>
<input type='text' name='filename' placeholder='page name'>
<label>Folders</label>
<select name="thisfolder">
<option value="">Default</option>
<option value="Folder1">Folder1</option>
<option value="Folder2">Folder2</option>
<option value="Folder3">Folder3</option>
</select><br><br>
<label>content</label><br>
<input type='text' name='strin' placeholder='content of created page'>
<input type='submit' value='Add Feed'>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// the name of the file to create
$filename=$_POST['filename'];
// the name of the file to be in page created
$strin=$_POST['strin'];
// the name of the folder to put $filename in
$thisFolder = $_POST['thisfolder'];
// make sure #thisFolder of actually a folder
if (!is_dir(__DIR__.'/'.$thisFolder)) {
// if not, we need to make a new folder
mkdir(__DIR__.'/'.$thisFolder);
}
// . . . /[folder name]/page[file name].php
$myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php";
$fh = fopen($myFile, 'w');
$stringData = "
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
";
fwrite($fh, $stringData);
fclose($fh);
}
?>
what i am trying to do is, passing that php code that is inside $stringData to that page that will be created

You'll want to escape the $'s in your text.
Set $stringData like this:
$stringData = "
// Start here
<?php
\$width = \$_GET['width'];
\$heigh = \$_GET['height'];
echo 'Hello';
?>
// End here
";

using highlight_string internal function
echo highlight_string($stringData);
or using htmlspecialchars
echo htmlspecialchars($stringData);
EDIT , as long as you don't want to print the php code literally to the output [as you've mentioned in your comment]
the problem here is that you are using (double quotes) to store values, which has special meaning in php
the solution is to store your text in single quotes ,
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
?>

You're using double quotes (") which lets you use $variables inside the string where single quotes (') will not.
like so:
$color = 'red';
$string_one = "My car is $color."
echo $string_one; // My car is red.
$string_two = 'My car is $color.'
echo $string_two; // My car is $color.
So to fix your code you simply need to change the double quotes to single quotes (and escape [put a backslash before] the other single quotes).
Like so:
<?php
$stringData = '
// Start here
<?php
$width = \$_GET[\'width\'];
$heigh = \$_GET[\'height\'];
echo \'Hello\';
?>
// End here
';
?>

In your code I added:
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
echo $stringData;
?>
When I opened this phap page, I had in browser:
// Start here // End here
In Page Source View I had:
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
There is no error! I see now what you wont.

This code with "(string) $price" working:
<?php
$price = 10;
$stringData = "start here (string) $price end here";
echo $stringData;
echo "(string) $price";
?>

Your code
?>
// End here must be in out put
";
?>
There is not start php delimiter <?php
Correct is:
?>
// End here must be in out put
<?php
";
?>
You missed one more php delimiter, total 2:
<?php
$stringData = "
**?>**
// Start here must be in out put
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
// End here must be in out put
**<?php**
";
?>

Related

how do i put my output into a session

i have this code that receive form input example below code my problem is how do i put this output into session so even when i refress or reopen this page i we still have my output
code:
session_start();
if (isset($_POST["chedit"]))
{
$text = $_POST["text"];
$mumu = $_POST["edit"];
$_SESSION["text"] = $text;
$_SESSION["mumu"] = $mumu;
$text = $_SESSION["text"];
$mumu = $_SESSION["mumu"];
echo $text;
echo $mumu;
}
every thing is working fine but if i try to reopen this page i got my recent output lost can any fixed this? Big thanks
This way $_POST['text] and $_POST['edit'] will be stored in $_SESSION['text'] and $_SESSION['mumu'] and always printed:
<?php
session_start();
if (isset($_POST["chedit"]))
{
$text = $_POST["text"];
$mumu = $_POST["edit"];
$_SESSION["text"] = $text;
$_SESSION["mumu"] = $mumu;
}
if (isset($_SESSION["text"]) && isset($_SESSION["mumu"]))
{
echo '<p>' . $_SESSION["text"];
echo '<p>' . $_SESSION["mumu"];
}
?>
But of course you can use that variables however you want. They will be stored as long as you don't close the navigator.

PHP statement within echo

I am essentially trying to combine to PHP statements.
<?php echo ROOT_PATH; ?>
<?php echo file_get_contents( "../css/themes/subtitle.php"); ?>
I want to achieve something like this:
<?php echo file_get_contents( "ROOT_PATH/css/themes/subtitle.php"); ?>
If you call constants inside single or double quotes then it will be always picked as a string.
You need to add like below:
<?php echo file_get_contents( ROOT_PATH."/css/themes/subtitle.php"); ?>
Its pretty simple you can change it to following
//full path of the file
$file_name = ROOT_PATH."/css/themes/subtitle.php";
echo file_get_contents($file_name);
it should work
. operator is used to concatenating.
Like:
$str = "Hello";
echo $str;
echo "World";
can be written as
$str = "Hello";
echo $str."World";

How to print "" in php varible in html file

Okay so when I do:
<br>
$line9 = "<button onclick='window.location.href='//home-pc/';'/>GoBack</button>";
$phpfiletxt
"$line1\n$line2\n$line3\n$line4\n$line5\n$line6\n$line7\n$line8\n$line9\n$line10\n$line11\n$line12\n$line13\n$line14\n$line15";<br>
$myfile = fopen("$folder/index.html", "w") or die("Unable to open file!");<br>
fwrite($myfile, $phpfiletxt);<br>
fclose($myfile);<br>
$wordlink = "<a class='list' target='_top' href='$folder'>$word</a>";<br>
$myfile = fopen("wordslist.php", "a") or die("Unable to open file!");<br>
fwrite($myfile, $wordlink);<br>
header("Location: $folder");<br>
where the button is i want to make it work because it doesn't do anything becuase the ' or " arent placed correctly
so i need it so this is a varible that i can print:
onclick='window.location.href='//home-pc/';'/>GoBack</button>";
into an html file. Thanks
In relation to :
How to print "" in php varible in html file
You do as follows:
$string = "The man said\"Hello\" and ran away!";
echo $string;
result: The man said"Hello" and ran away!
It's called escaping. http://php.net/manual/en/language.types.string.php
<button onclick="window.location.href='/home/pc'">GoBack</button>;
Is what you need to replace that with.
Try this
$line9 = "<button onclick=\"window.location.href='//home-pc/';\">GoBack</button>";
I always use ' instead of ", because html has so many " need to be escape like \"...\"
$string = '<a class="text-success" id="success">success></a>'

PHP change directory according to parameter ($GET from url)

I try to run project and get localhost:8000/fishes.php and then change it to localhost:8000/fishes.php?fish=pike (or trout).
I have directories on same project /info/pike (or trout) and in these directories there is info.txt where first line has fish latin name and second line has average size.
My question is, How can I get text from that file to "site". Code doesn't include html-code, I don't have the code right now with me. But it is fine and runs normally.
Thanks
<?php
$species = $_GET["fish"];
if (file_exists("info.txt")) {
$array = explode("/n", file_get_contents('$species/info.txt'));
$name = $array[0];
$size = $array[1];
} else {
}
global $name;
global $size;
?>
<h1><?php$name?> (<?php$size?>)</h1>
In your Markup, you're opening the php tag, and calling a variable. This variable is not actually printing to the STDOUT. You have a few options:
<?php echo $name; ?>
or
<?php print $name; ?>
or if you have shorttags enabled
<?=$name;?>
You need to use echo or print for variables.
<?php
$name = '';
$size = '';
$species = $_GET["fish"];
if (file_exists("info.txt")) {
$array = explode("/n", file_get_contents('$species/info.txt'));
$name = $array[0];
$size = $array[1];
} else {
//Code for Else
}
//global $name; //No Need of Globals if you have html in same file
//global $size; //No Need of Globals if you have html in same file
?>
<h1><?php echo $name?> (<?php echo $size?>)</h1>
I think you need to use double quotes in "$species/info.txt"
if you server support short tags you can:
<h1><?=$name;?> (<?=$size;?>)</h1>

PHP, Line break when writting to a file

I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
Can you try appending the newline character (\n) to the $thetext variable like this:
$thetext=$_POST['sometext'] . "\n";
Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.
$thetext."\n"
in php you concatenate strings using ".", you use "+" in javascript.
Use newline "\n" instead of the br's which is for html
$text = $text."\n" ?
Err here's some more text to fill out the answer
fwrite($myfile, $data); // added + "\n" here and responses turned 0
the concat string operator is (.) not (+)
you can also simplify your script thusly
echo nl2br(get_file_contents($file));

Categories