0

I'm new to PHP and I was wondering which is the ideal method to turn the text in the else statement below into code. I think my old way to escape every quote with a \" seems not to be the best way so how would you do it?

<?php
if (strlen($finalArray['Lat']) <= 0){

} 
else 
{
<a name="openframe"></a>
Loc: <br/>
<a href="#xmap" onclick="spoiliut('pmap');" title="<?php echo $finalArray['Name']; ?>" target="_self"><strong> <?php echo $finalArray['Name']; ?> </strong>
</a>
?>

Thanks for tips.

3 Answers 3

3

Two options, the first looks like: (It works because if statements carry between php tags)

<?php
if (strlen($finalArray['Lat']) <= 0)
{

} 
else 
{
?>

<a name="openframe"></a>
Loc: <br/>
<a href="#xmap" onclick="spoiliut('pmap');" title="<?php echo $finalArray['Name']; ?>" target="_self"><strong> <?php echo $finalArray['Name']; ?> </strong>
</a>

<?php
}
?>

And the second is the Heredoc syntax
It looks like:

<?php
if (strlen($finalArray['Lat']) <= 0)
{

} 
else 
{
    echo <<<EOF
    <a name="openframe"></a>
    Loc: <br/>
    <a href="#xmap" onclick="spoiliut('pmap');" title="{$finalArray['Name']}" target="_self"><strong> {$finalArray['Name']} </strong>
    </a>
EOF;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

PHP can be mixed with HTML. You could do this:

<?php if (strlen($finalArray['Lat']) <= 0) : ?>
    <p>some html</p>
<?php else : ?>
    <a name="openframe"></a>
    Loc: <br/>
    <a href="#xmap" onclick="spoiliut('pmap');" title="<?= $finalArray['Name'] ?>" target="_self"><strong> <?= $finalArray['Name'] ?> </strong>
    </a>
<?php endif ; ?>

2 Comments

Wow this will speed up my workflow! Thank you all for the great examples.
@Michael, You could show your appreciation with an up vote. ;) BTW, I've edited my answer to show the PHP echo short tag. That will speed things up too!
0
<?php
if (strlen($finalArray['Lat']) <= 0){

} 
else 
{
?>
<a name="openframe"></a>
Loc: <br/>
<a href="#xmap" onclick="spoiliut('pmap');" title="<?php echo $finalArray['Name']; ?>" target="_self"><strong> <?php echo $finalArray['Name']; ?> </strong>
</a>

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.