<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Para</title>
    <description>The latest articles on DEV Community by Michael Para (@michael_para_188fc36258ec).</description>
    <link>https://dev.to/michael_para_188fc36258ec</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2601464%2F38584d40-7f5c-4585-b91b-cdb84dda17e4.jpg</url>
      <title>DEV Community: Michael Para</title>
      <link>https://dev.to/michael_para_188fc36258ec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michael_para_188fc36258ec"/>
    <language>en</language>
    <item>
      <title>PHP array_map Function: Detailed Guide</title>
      <dc:creator>Michael Para</dc:creator>
      <pubDate>Sat, 14 Jun 2025 15:03:20 +0000</pubDate>
      <link>https://dev.to/michael_para_188fc36258ec/php-arraymap-function-detailed-guide-4kkl</link>
      <guid>https://dev.to/michael_para_188fc36258ec/php-arraymap-function-detailed-guide-4kkl</guid>
      <description>&lt;p&gt;The PHP array_map function applies a callback to each element of one or more arrays. &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax of PHP array_map Function
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://flatcoding.com/tutorials/php/array_map/" rel="noopener noreferrer"&gt;PHP array_map function&lt;/a&gt; returns a new array containing the results. This function helps process array elements without using loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_map(callback, array1, array2, ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;callback: A function name or an anonymous function. It takes the array element(s) as argument(s).&lt;/li&gt;
&lt;li&gt;array1, array2, ...: One or more arrays to process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The function calls the callback on every element from the input arrays. It passes corresponding elements from each array to the callback. Then it collects the callback return values into a new array.&lt;/p&gt;

&lt;p&gt;If arrays have different lengths, the function stops at the shortest array's length.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$numbers = [1, 2, 3, 4];
$squares = array_map(function($n) {
    return $n * $n;
}, $numbers);

print_r($squares);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array
(
    [0] =&amp;gt; 1
    [1] =&amp;gt; 4
    [2] =&amp;gt; 9
    [3] =&amp;gt; 16
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example squares every number in the input array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Multiple Arrays
&lt;/h2&gt;

&lt;p&gt;You can provide multiple arrays to array_map. The callback receives one argument per array, each representing the corresponding element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

$sum = array_map(function($a, $b) {
    return $a + $b;
}, $array1, $array2);

print_r($sum);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array
(
    [0] =&amp;gt; 5
    [1] =&amp;gt; 7
    [2] =&amp;gt; 9
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function adds corresponding elements from both arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Named Callback Functions
&lt;/h2&gt;

&lt;p&gt;Instead of &lt;a href="https://flatcoding.com/tutorials/php/php-anonymous-functions/" rel="noopener noreferrer"&gt;PHP anonymous functions&lt;/a&gt;, you can use existing function names as callbacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function double($n) {
    return $n * 2;
}

$numbers = [1, 2, 3];
$doubled = array_map('double', $numbers);

print_r($doubled);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array
(
    [0] =&amp;gt; 2
    [1] =&amp;gt; 4
    [2] =&amp;gt; 6
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using Built-in PHP Functions as Callback
&lt;/h2&gt;

&lt;p&gt;You can use built-in PHP functions with array_map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$strings = ['apple', 'banana', 'cherry'];
$upper = array_map('strtoupper', $strings);

print_r($upper);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array
(
    [0] =&amp;gt; APPLE
    [1] =&amp;gt; BANANA
    [2] =&amp;gt; CHERRY
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;array_map returns an array with the mapped results. It does not modify the original arrays.&lt;/p&gt;

&lt;p&gt;Here are important notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The original arrays stay unchanged.&lt;/li&gt;
&lt;li&gt;The returned array keys match the input arrays' keys.&lt;/li&gt;
&lt;li&gt;If callback is null, array_map returns the arrays merged into one multidimensional array.&lt;/li&gt;
&lt;li&gt;The function stops processing at the shortest input array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$array1 = [1, 2, 3];
$array2 = ['a', 'b', 'c'];

$result = array_map(null, $array1, $array2);
print_r($result);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array
(
    [0] =&amp;gt; Array
        (
            [0] =&amp;gt; 1
            [1] =&amp;gt; a
        )

    [1] =&amp;gt; Array
        (
            [0] =&amp;gt; 2
            [1] =&amp;gt; b
        )

    [2] =&amp;gt; Array
        (
            [0] =&amp;gt; 3
            [1] =&amp;gt; c
        )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The function groups elements with the same keys from each array.&lt;/li&gt;
&lt;li&gt;Transform all elements in an array.&lt;/li&gt;
&lt;li&gt;Combine multiple arrays element-wise.&lt;/li&gt;
&lt;li&gt;Format or sanitize input data.&lt;/li&gt;
&lt;li&gt;Change array values using a function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The array_map function lets you transform arrays with a callback function. It works well for simple element-wise operations without explicit loops. It handles one or more arrays and returns a new array with mapped results. Keep original arrays safe and keys preserved.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Elvis Operator in PHP</title>
      <dc:creator>Michael Para</dc:creator>
      <pubDate>Wed, 04 Jun 2025 15:30:33 +0000</pubDate>
      <link>https://dev.to/michael_para_188fc36258ec/elvis-operator-in-php-46d8</link>
      <guid>https://dev.to/michael_para_188fc36258ec/elvis-operator-in-php-46d8</guid>
      <description>&lt;h2&gt;
  
  
  What Is the Elvis Operator?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://flatcoding.com/tutorials/php/elvis-operator/" rel="noopener noreferrer"&gt;Elvis operator is a shorthand&lt;/a&gt; for a simple &lt;code&gt;if-else&lt;/code&gt; expression. PHP does &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; have a native Elvis operator, but you can simulate it using the ternary operator without the middle part.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s1"&gt;'Guest'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This checks if &lt;code&gt;$input['name']&lt;/code&gt; is truthy. If yes, it assigns it to &lt;code&gt;$username&lt;/code&gt;. If not, it sets &lt;code&gt;'Guest'&lt;/code&gt; as default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax and Behavior
&lt;/h2&gt;

&lt;p&gt;You can write ternary syntax like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$condition&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nv"&gt;$trueValue&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$falseValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elvis-style syntax removes the middle part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nv"&gt;$default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It checks if &lt;code&gt;$value&lt;/code&gt; is truthy&lt;/li&gt;
&lt;li&gt;If true, it returns &lt;code&gt;$value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If false, it returns &lt;code&gt;$default&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to Use It
&lt;/h2&gt;

&lt;p&gt;Use the Elvis-style operator when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to provide a default value&lt;/li&gt;
&lt;li&gt;You want to simplify a long &lt;code&gt;if&lt;/code&gt; block&lt;/li&gt;
&lt;li&gt;You handle user input or optional data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s1"&gt;'no-reply@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user gives an email, it uses it. If not, it falls back to &lt;code&gt;'no-reply@example.com'&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important Notes
&lt;/h2&gt;

&lt;p&gt;PHP treats these as &lt;strong&gt;&lt;em&gt;falsy values&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;''&lt;/code&gt; (empty string)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;'0'&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$number&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Returns 100, not 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can cause bugs if &lt;code&gt;0&lt;/code&gt; is valid. To avoid this, use &lt;code&gt;isset()&lt;/code&gt; or the null coalescing operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison with Null Coalescing
&lt;/h2&gt;

&lt;p&gt;Use the null coalescing operator (&lt;code&gt;??&lt;/code&gt;) when you want to check &lt;strong&gt;&lt;em&gt;only null&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;??&lt;/code&gt; checks for &lt;code&gt;null&lt;/code&gt; only&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?:&lt;/code&gt; checks for all falsy values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choose based on what counts as "missing" in your logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PHP simulates the Elvis operator using &lt;code&gt;?:&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It helps assign fallback values quickly&lt;/li&gt;
&lt;li&gt;Be careful with values like &lt;code&gt;0&lt;/code&gt; or empty strings&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;??&lt;/code&gt; if you only want to check for &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Elvis pattern saves time and makes code shorter—but it needs thoughtful use.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://flatcoding.com/tutorials/php/elvis-operator/" rel="noopener noreferrer"&gt;flatcoding.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
