0

I just started learning PHP. I watched a tutorial about building a theme in WordPress using PHP. I want to ask about the term "wrapper", as I know that a wrapper for PHP code is <?php ?>. First please look at this code:

<?php
        $lastBlog = new WP_Query('type=post&posts_per_page=1');

        if( $lastBlog->have_posts() ):

        while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>

            <?php get_template_part('content', get_post_format()); ?>


        <?php endwhile;

    endif;
        wp_reset_postdata();
    ?>

and this

<?php 
        $lastBlog = new WP_Query('type=post&posts_per_page=1');

        if( $lastBlog->have_posts() ):

        while( $lastBlog->have_posts() ): $lastBlog->the_post(); 

            get_template_part('content', get_post_format()); 


        endwhile;

    endif;
        wp_reset_postdata();
    ?>

There is a little difference in the way the wrappers used. I'm confused, which is better? Both are running the same way. Sorry if this question looks ridiculous.

I have searched for this topic, and I didn't answer to my question. Is it correct to say "wrapper" instead of "limiter"?

2

2 Answers 2

2

The official documentation calls them opening and closing tags:

When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.

A "code wrapper" is usually understood as a class, or a library, which encapsulates a number of lower-level operations under a simplified programming interface. For example, the decorator pattern is also known as wrapper, since the idea is to create a class that will add extra functionality to another class without changing structures of the other classes.

Example Wrapper (Decorator)

<?php
class Rectangle {
  private $width, $height;

  public function __construct($w, $h) {
    $this->width = $w;
    $this->height = $h;
  }

  public function getArea() { return $this->width * $this->height; }
  public function getWidth() { return $this->width; }
  public function getHeight() { return $this->height; }
}

// Decorator
class ImprovedRectangle {
  protected $rect;

  public function __construct(Rectangle $r) {
    $this->rect = $r;
  }

  public function getPerimeter() {
    return 2 * ($this->rect->getWidth() + $this->rect->getHeight());
  }
}

$rect = new Rectangle(2, 3);
$irect = new ImprovedRectangle($rect);
echo $irect->getPerimeter(); // 10

So it depends on context. If we are speaking about a template code, where the markup code is the major, it is quite correct to call the PHP tags "PHP wrappers". But in the context of a PHP model (a class, or a set of functions), the PHP tags (usually only the opening tag) are mandatory, and we primarily focus on the code constructs.

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

Comments

0

Everything inside the start tag <?php and the end tag ?> will be processed by the PHP processor. I have heard these called PHP Islands.Since in the example with more start and end tags there is not code outside the <?php ?> islands the code runs the same. Were you to put code outside the islands it would be printed as plain text and not sent to the PHP process.

In your example it doesn't make a difference but for readability I would only have one set of <?php ?> tags.

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.