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
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;

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.