Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It really depends on the context in which you're programming. If you're going to be passing something that may contain several values, or in order to use it, you need to implement some complex logic, then I would personally wrap that up inside an object.</p> <p><b> Array example </b></p> <pre><code>&lt;?php for($i = 0; $i &lt; 10; $i++) { ?&gt; &lt;p&gt;&lt;?php echo $arr[$i]; ?&gt;&lt;/p&gt;&lt;?php } ?&gt; </code></pre> <p>As you can see, all you're doing in the view is displaying it (as you should be). This means an array is fine, to my mind, because you've got simple code with an array and the complexity of the output doesn't justify the overhead of an object.</p> <p><b> Object Example </b></p> <pre><code>&lt;?php for($i = 0; $i &lt; 10; $i++) { ?&gt; &lt;p&gt;&lt;?php echo $titles[$i]; ?&gt;&lt;/p&gt; &lt;p&gt;&lt;?php echo $subHeadings[$i]; ?&gt;&lt;/p&gt; &lt;p&gt;&lt;?php echo $paragraphs[$i]; ?&gt;&lt;/p&gt; } ?&gt; </code></pre> <p>As you can see here, an object might make this a little more pleasant. Let's say we wrapped this up inside a <code>Section</code> object.</p> <pre><code>class Section { protected $title; protected $subHeading; protected $paragraph; function __construct($title, $subHeading, $paragraph) { this.title = $title; this.subHeading = $subHeading; this.paragraph = $paragraph; } function __toString() { return "&lt;p&gt;this.title&lt;/p&gt; &lt;p&gt;this.subHeading&lt;/p&gt; &lt;p&gt;this.paragraph&lt;/p&gt;"; } } </code></pre> <p>Then all you need to do is cycle through and do something like:</p> <pre><code>&lt;?php for($i = 0; $i &lt; 10; $i++) { ?&gt; &lt;p&gt;&lt;?php echo $sections[$i]; ?&gt;&lt;/p&gt; } ?&gt; </code></pre> <p><b>My Recommendation?</b></p> <p>If you want to play it safe, go for an <code>Object</code>. It is more versatile; will <em>always</em> result in the cleaner code and makes it far easier to maintain and read; not to mention that invaluable asset of decoupling your view completely from the back end of your application. In theory, that means you can change the entire look and feel of the app without ever changing the view, which is a nice touch.</p> <p>The downside? Sometimes it will be unnecessary and you'll incur an overhead, when you didn't need one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload