4

I want to show this sentence:

?>
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php

I have tried:

echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>';

Without success.

If a line contains both HTML and PHP code, what is the proper format to display it?

1
  • Do you want to showcase the output as PHP code completely or just want to print date? Commented Nov 6, 2013 at 11:04

8 Answers 8

9

You have a "dot mess".

You can concatinate PHP variables and strings like

<?pho echo 'string' . $var . 'string' . $var1 . $var2; // and so on ?>

So correct solutions would be

<?php

echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'<br></h2>';
echo '<h2>Date ' , $day , '/', $month ,'/', $year ,'<br></h2>';

echo "<h2>Date $day/$month/$year<br></h2>";
echo "<h2>Date {$day}/{$month}/{$year}<br></h2>";

?>

You can even use commas , for echoing strings and variables that's a bit faster. Cause PHP does not concatinate all parts together to a single string and echos it but echos every part separatly (so less operations are needed).

Note that if you put a string in double quotes " you can use $var or {$var} inside the string cause PHP is looking vor PHP variables inside a double quoted string and replaces them by their containing value.

But the fastest solution is the , separated version.

or

As lonesomeday mentioned there is also a C like solution possible in PHP

<?php printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year); ?>

or (but not recommended by me)

The complicated and none readable version

<h2>Date <?php echo $day; ?> / <?php echo  $month; ?> / <?php echo $year; ?><br></h2>

And the shorted version of the above code (just for completeness, short tags have to be enabled in PHP versions lower than 5.4 to work)

<h2>Date <?= $day; ?> / <?=  $month; ?> / <?= $year; ?><br></h2>

For even more information about string concatination read the offical PHP doc site about String Operations.

Sign up to request clarification or add additional context in comments.

4 Comments

My code was a mess indeed, thank you for showing the appropriate format. Much easier to read too!
Using the echo tag version would be better in the last case: <h2>Date <?= $day ?>/<?= $month ?>/<?= $year ?><br></h2> Not great, though, I agree.
<?= can be used even when short tags are not enabled from PHP 5.4 onwards, FWIW.
@lonesomeday Thx for poining me to this. Fixed it in my answer.
7

The nicest way to do this is probably with printf, which substitutes variables into your string according to placeholders:

printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year);

2 Comments

Keep in mind that %d is an integer. This means you will lose any leading 0s if you had any. %s is probably better.
@FritsvanCampen That's a good point. Fingers ahead of brain! I've updated my answer.
2
?>
<h2>Date <?php echo $day."/".$month."/".$year."<br>"; ?></h2>
<?php

You don't need quotes when using variables.

Comments

1

Try

echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'</h2>';

Comments

0

Try this... This will display the code as it is in your browser

<?php

echo htmlspecialchars('<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>');

?>

O/P:

<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>

Comments

0

Can you try this,

  echo "<h2>Date $day / $month / $year <br></h2>";

Comments

0

I think this is your answer

<h2>Date <?php echo "\$day"."/"."\$month"."/"."\$year"."<br>"; ?></h2>

or

<?php echo '<h2>Date '.$day.'/'.$month.'/'.$year.'<br></h2>'; ?>

Comments

0
try this
echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day\"."/".\"$month\"."/".\"$year\".\"<br>\"; ?></h2>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.