How to echo php code and use it? - php

<?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".

Related

Run PHP code while echoing PHP

I'm using the echo command in PHP, but I want to enter PHP code like <?php echo $variable [id_login]?>, while echoing something else out, but this does not work.
Is this possible to do, and if so, how would I do it?
echo "<script>location='member.php?&id=<?php echo $taruh[id_login] ?></script>";
You cannot use echo or open/cloce php twice like you did, you might want to try something like the line below,
echo '<script>location=member.php?id=' . $taruh[id_login] . '</script>';
after echo you can write enything you'd like, even if it's a php variable, just use single quote and dot where you need it (like I do here), as you can see, echo is only used once..
For example:
<?php
$your_variable = 'some text';
$other_variable = 'some PHP code';
echo 'I wrote: ' . $your_variable . ' and ' . $other_variable . '!';
?>
Output will be:
I wrote: some text and some PHP code!
I hope this will bring you into the right direction..
EDIT
Also important: if you use query string in URLs, the first 1 can be a ? every other part after should be a & for example see the url below
http://www.example.com/index.php?id=12345&coder=yes&country=usa
before id I used a quest sign, for all others I didn't use the quest sign...

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 ?>");

PHP echo function is not outputing strings in the format '<something.somethong_else>'

PHP echo function is not outputing strings in the format of html tag like <something. somethong_else>, may be because it is like HTML tags, is there any way to display it?
echo 'hi<h.i>';
Eg : this displays as
echo 'hi';
try using
<?php
echo htmlentities('hi<h.i>');
?>
You need to encode the string if you what the text to appear
echo htmlentities("hi<h.i>");
There is a thing called HTML. Where strings in <something.somethong_else> format have some meaning. Go figure.
PHP can echo out tags.
Example
<?php
echo '<p>Hello World</p>';
?>
Keep in mind, the PHP will echo where it is called. So you can also do this
<p>
<?php echo 'Hello World'; ?>
</p>
UPDATE
Since new information is sent. You can make < into < and > into > Look at HTML entities.
You probably need to use htmlentities(), try this:
echo htmlentities('hi<h.i>');

cant write varibles into php file using file_put_contents 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;
?>');
?>

PHP eval() code in between <?php ?> from database

I want to be able to put PHP into the database and run it. I have to do this because I store page layouts in the database and each our different for each other, however in some cases I want to use dynamic content for some of the pages.
Assume $query_from_db is the string returned from the database. PHP should only eval() the code in between <?php and ?>
$query_from_db = '<div>
<?php
//php to run
function dosomething() {
//bleh
}
?>
</div>
';
php echo eval($query_from_db);
How can I do this? I'm aware this is not recommended.
I'm not arguing about the sense or nonsense of this approach. To some extend, this is a valid question.
See the documentation:
To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
So you have to do:
eval('?> ' . $query_from_db . ' <?php ');
DEMO
Also note that eval is outputting directly to the browser. It does not return a value. Have a look at Output Control Functions for buffering.
You are aware that this is not recommended and I strongly urge everyone to review the comments to this question.
But to provide an answer:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?'); // will output "hello world";
be aware that this however will not work:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?php'); // error will be thown
This works again:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?> '.$string.' <?php '); // will output "hello world";
i am not really sure why.
following up on your comment to grab the output you can do:
<?php
$string = 'hello <?php echo "world"; ?>';
ob_start();
eval('?> '.$string.' <?php '); // will output "hello world";
$output = ob_get_clean(); // $output will now contain "hello world". No text will have ben printed.
If you want to avoid the eval stigmata, you can alternatively use:
include("data:,$query_from_db");
It's just another name for eval which doesn't upset people as much. It depends on the php.ini setting allow_url_include however.
What you are doing is functionally equivalent to include("$template/$by_name.php"); and just differs in that you didn't put the database content into a file before. (But that's the other workaround: file_put_contents && include).

Categories