2

I have php file index.php

In this file to use html code I am doing:

index.php

echo '
<div>
<a class="fragment" href="">
<div>';

In href I want to put value of some php variable i.e. $url How could be done?

is this correct way?

 <a class="fragment" href="<?php echo $url; ?>">

6 Answers 6

11

You concatenate the string by ending it and starting it again:

echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';

Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:

?>
    <div>
        <a class="fragment" href="<?php echo $url; ?>">link</a>
    </div>
<?php
Sign up to request clarification or add additional context in comments.

6 Comments

What if the div is being imported with php from a different php file? By me, it just outputs "echo..." as text, in the html tag.
@DaniSpringer Makes no difference - You're probably using the wrong containing quotes or you're reading the php file rather than loading its content with include or require.
aha. I'm trying to import the meta tags and define the values with which to fill them, in the pages themselves. But when I write the variable names in the file that I want to import, I get an error saying those variables aren't defined.
@DaniSpringer Global variables have absolutely nothing to do with pasting variables into an URL.
|
6

Since you are printing several lines of HTML, I would suggest using a heredoc as such:

echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;

HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.

If you want to use arrays or similar you can escape the variable with {} as such:

echo <<<HTML
<div>{$someArray[1]}</div>
HTML;

Comments

5

if you are echoing php directly into html i like to do this

<div><?=$variable?></div>

much shorter than writing the whole thing out (php echo blah blah)

if you are writing html in php directly then there several options

$var = '<div>'.$variable.'</div>';   // concatenate the string
$var = "<div>$variable</div>";       // let php parse it for you.  requires double quotes
$var = "<div>{$variable}</div>";     // separate variable with curly braces, also requires double quotes

Comments

2

Do it like

<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";

6 Comments

Put single quotes around $url and I'll upvote, as I like interpolating rather than concatenating.
@Barmar, PHP can interpret variables inside quotes too right ?
@Barmar PHP doesn't expand variables in single quotes.
@h2ooooooo Single quotes inside double quotes don't prevent variable expansion.
@Barmar Ah sorry - misunderstood you. I thought you wanted him to enclose the entire thing in single quotes. It's getting late :-)
|
1

If you want to maintain the echo statement you can do either

echo '<a class="fragment" href="'.$url.'"><div>';

or

echo "<a class=\"fragment\" href=\"$url\">";

The first is better for performances and IMHO is more readable as well.

1 Comment

Ugh, why do people prefer \" instead of using single quotes?
1

If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:

echo <<<_EOI_
<div>
   <a class="fragment" href="$url">
<div>
_EOI_;

You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.

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.