cant write varibles into php file using file_put_contents PHP - php

I'm trying to edit a file using the file_put_contents
Everything works fine (the data shows up in the test.php, well most of it) but the variables do not.
heres an example of my problem
code for the file that will perform the file_put_contents:
<?php
file_put_contents("test.php","<?
$title = "title_name_goes_here"
echo $title;
?>");
?>
Code that shows up in the target file (test.php):
<?
= this_is_my_name
echo ;
?>
What should show up in test.php:
<?
$title = "title_name_goes_here"
echo $title;
?>
Also, I'm using Dreamweaver to write the code in and it seems to have problems (code errors) when i insert the quote for the $title's value so its ok if i used $title = title_name_goes_here but it doesn't like $title = "title_name_goes_here". When i say its okay, i mean dreamweaver doesn't show any code errors but it still doesn't do what it should.
any ideas?

Escape the quotes (and the dollar signs):
<?php
file_put_contents("test.php","<?
\$title = \"title_name_goes_here\"
echo \$title;
?>");
?>
If you don't want to see the ugly escapes just use single quotes:
<?php
file_put_contents('test.php",'<?
$title = "title_name_goes_here"
echo $title;
?>');
?>

Related

Echo php with echoed html php

I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

Echoing PHP TAG showing blank output HTML

I am testing this on windows 7 xammp 1.8.1 php 5.4.7
I am trying to show dynamic php codes in html as example
my code is
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
but output html is blank! i am not sure if its a bug, can some help me.thanks in advance
When you run this, this DOES produce an output, which is a blank page, because the output is:
<?php echo ?>
To a browser which renders html, it will look like an open tag with nothing in value.
Run your script and view the page source...
You need to use single quotes
i.e.
change
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
to
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Just change your double quotes to single quotes:
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Example: http://ideone.com/bUJAxb
There are two things you have to change. First, if you use double quotes PHP will evaluate variables in it, so your output will be <?php echo ?>:
$output='<?php echo $ti ?>';
Now the output will be <?php echo $ti ?>.
Next, the browser will interpret this as HTML and since it is only a single tag it will display nothing. You need to run this through htmlentities():
echo htmlentities($output);
This will output <? echo $ti ?;gt; which will be displayed by the browser in the way you intend it.
You need to escape the characters
This can be done by adding the entire line you want to output in an htmlentities function call, like:
$output = htmlentities("<?php echo \$ti ?>");

Trouble inserting text from a PHP text field into an other PHP function

I'm working with a custom autonav in the Concrete5 CMS - but I think this might be more of a general PHP question. I'm having a hard time figuring out if it is possible to do the following.
I have some autonav code that looks like this:
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<?php
$nav = BlockType::getByHandle('autonav');
$nav->controller->orderBy = 'display_asc';
$nav->controller->displayPages = 'custom';
$nav->controller->displayPagesCID = '135';
$nav->controller->displaySubPages = 'all';
$nav->controller->displaySubPageLevels = 'all';
$nav->render('tertiary');
?>
I also have a text field that outputs via the following code:
<?php if (!empty($field_4_textbox_text)): ?>
<?php echo htmlentities($field_4_textbox_text, ENT_QUOTES, APP_CHARSET); ?>
<?php endif; ?>
What I'd like to do is have the text output within this line of the autonav code:
$nav->controller->displayPagesCID = '135';
Instead of the hard coded 135, I'd like the text outputted by $field_4_textbox_text to display within those single quotes. Something like:
$nav->controller->displayPagesCID = 'echo $field_4_textbox_text';
But that doesn't work. Nothing I've done works. Is there something obvious I might be missing? I'm feeling clueless.
Thanks!
Use double quotes to parse variables, single quotes can never parse variables in php
$nav->controller->displayPagesCID = " $field_4_textbox_text";

How to use variable defined in one php tag in another php tag?

I wrote a php page which has two php tags and one script tag inside it .
<?php
$value = $_GET['hash'];
?>
<script>
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
echo $cmd;}
?>
}
</script>
I want to use $value inside another php tag ( like above it has the file I want to open ), but I am not able to do it.Is the scope of variable limited to one php tag ? if yes how can I solve this problem Please help
Your code works perfectly. The variables in one PHP tag is accessible from all other tags, unless you define them inside a PHP function.
The reason you are not seeing the echo on the screen is because the echo prints to the Javascript function.
If you view the source of the generated page, the file contents will be there.
Try this:
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
?>
alert( <?php echo $cmd; ?> );
<?php
}
?>
}
execute();
if $value is a get then you don't need to access it as a file, it should just be a short string.
just above line 7 (the one with $readfile = file...
type:
echo "alert(The hash value is: ".$value.")";
This will make an alert display (as it is in a script tag)
p.s you should have in your opening tag

Categories