Transform an Array of Strings Into a Set of Paragraphs

The following fact is important:

$message is a mixed variable. It starts off being either a string or an array.

Notice how (if $message is a string) the string gets transformed into an array deap within this complex statement.

So what does this complex PHP statement do?

$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

It transforms $message into a string consisting of HTML paragraphs; where each paragraph is a single message from the original $message.

Details:

Q: What does this evaluate to?

implode('</p><p>', $array_of_strings )

Evaluates to a string consisting of a concatenation of all the strings from $array_of_strings separated by '</p><p>' .

Q: What does this evaluate to?

( ! is_array($message)) ? array($message) : $message)

It evaluates to an array. If $message was an array then that is the array. Otherwise, the array is a one element array; The value of that one element is the string $message.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in CI Source Code Explained, Code for View, PHP Code Snippets. Bookmark the permalink.

One Response to Transform an Array of Strings Into a Set of Paragraphs

  1. Pingback: Exceptions.php CI Explained | Sam's PHP

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s