Jump to content

How to parse a small code?


11Tami

Recommended Posts

Hello, this works.

 

<?php

// current time

echo date('h:i:s');

?>

 

I need to put it inside other php and still get it to execute. Can someone tell me how? This is what I tried but it doesn't show the time, anyone know why? Please let me know, thank you very much.

<?php

$string = '</?php

echo date(\'h:i:s\');

?>';

echo $string;

?>

Link to comment
https://forums.phpfreaks.com/topic/46537-how-to-parse-a-small-code/
Share on other sites

I'm just trying to learn how to put and parse one php code inside another and still get it to work. Once I figure out how to do that I may be doing something else with it later, not sure yet.

 

Here it is again.

 

<?php

$string = '</?php

echo "test";

?>';

echo $string;

?>

 

You cannot put PHP inside PHP. If you have this:

<?php
$string = '<?php
echo "test";
?>';
echo $string;
?>

 

Then look at Glyde's post.

<?php
$string = "echo date('h:i:s');";
eval($string);
?>

 

You have to use eval.

 

eval parses PHP code in a string.

 

However if all you want to do is echo the date after a string then do this:

echo 'The date to day is: ' . date("D jS F Y");

You use the concatenation (concat for short) operater (.). To tie the string to a function that returns a string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.