Leahcim
Leahcim

Reputation: 41999

Strange PHP tags

I got the code below from a tutorial that teaches how to make wordpress plugins. It has some unusual PHP tags. For example,

a) 3rd line from the bottom, there is an opening <?php tag.

b) And, also, after $base_map_url is set, there is a closing ?> tag.

This is very different from what I've learned, yet the code works.

c) Also, in all the plugins the author has built, there is an opening <?php tag at the very top but not a final closing one.

What is happening here, can you explain?

<?php
/*
 Plugin Name: Map plugin using shortcode
 Plugin URI: http://example
 Description: This plugin will get a map of whatever parameter is passed
 Author: Drew
 Version: 1.0
 Author URI: http://www.blah 
 */

function smp_map_it($addr)
{
    $addr = "1600 Pennsylvania Ave. Washington, D.C.";
    $base_map_url = 'http://maps.google.com/maps/api/staticmap?sensor=false&size=256x256&format=png&center=';
    ?>
    <h2>Your map:</h2>
    <img width="256" height="256"
        src="<?php echo $base_map_url . urlencode($addr); ?>" />
    <?php 
}

add_shortcode('map-it','smp_map_it');

Upvotes: 1

Views: 181

Answers (5)

alex
alex

Reputation: 490413

It is just leaving a PHP block for HTML output.

Functions shouldn't really do that. That is why WordPress gets so much crap, e.g. the_content() that just dumps content (as if people are too incompetent to use echo).

The best way to do it is simply have a view file which handles your HTML and some basic PHP constructs and control structures such as echo, foreach, if, etc.

The trailing ?> in a file is unnecessary, and Zend coding standards ask you to never place it. It can often be the cause of Headers already sent when some pesky whitespace ends up after it.

Upvotes: 7

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

It is not strange, it is crap. He is embeding HTML in PHP (sin anyway), and he is doing it bad. Last PHP tag can remain open.

Upvotes: 1

Alp
Alp

Reputation: 29739

<?php marks the beginning of php code for the php parser

?> marks the end of php code for the php parser

/* marks the start of a comment which will be ignored by the parser

*/ marks the end of a comment which will be ignored by the parser

Upvotes: 2

Mark Grey
Mark Grey

Reputation: 10257

This function is sending output directly rather than using echo statements where the content falls outside the php tags.

While this kind of approach is often discouraged (coming out of the PHP context while in a function body isn't considered good practice) there isn't anything about it that would make it not function.

It is however, very bad code. Outputting in function bodies very often leads to header errors and other comforts when trying to perform redirects or session management.

Upvotes: 1

vbence
vbence

Reputation: 20333

When you close the PHP tag, it just becmes equal to:

echo ('<h2>Your map:</h2>
<img width="256" height="256"
    src="');

There are two approaces to coding PHP. One inserts PHP into HTML, this is used by frontend developers. Template languages use this style in general:

<img src="<?php echo ($url); ?>">

The other style is more like programming then template writing:

<?php
    echo (printf ('<img src="%s">', $url));

Upvotes: 2

Related Questions