<?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>Forem: Ramin Omrani</title>
    <description>The latest articles on Forem by Ramin Omrani (@omr4ni).</description>
    <link>https://forem.com/omr4ni</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%2F1564098%2Fb95ec695-5c74-424b-843f-d30f267ad22d.jpg</url>
      <title>Forem: Ramin Omrani</title>
      <link>https://forem.com/omr4ni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/omr4ni"/>
    <language>en</language>
    <item>
      <title>Why I Use the empty() Language Construct More than isset() in PHP and You Should Too</title>
      <dc:creator>Ramin Omrani</dc:creator>
      <pubDate>Tue, 22 Oct 2024 11:20:47 +0000</pubDate>
      <link>https://forem.com/omr4ni/why-i-use-the-empty-language-construct-more-than-isset-in-php-and-you-should-too-dkg</link>
      <guid>https://forem.com/omr4ni/why-i-use-the-empty-language-construct-more-than-isset-in-php-and-you-should-too-dkg</guid>
      <description>&lt;p&gt;When developing in &lt;strong&gt;PHP&lt;/strong&gt;, handling variable checks effectively is crucial to ensure robust and error-free code. Two frequently used language constructs for this purpose are &lt;code&gt;empty()&lt;/code&gt; and &lt;code&gt;isset()&lt;/code&gt;. However, in many cases, choosing &lt;code&gt;empty()&lt;/code&gt; might be more advantageous than &lt;code&gt;isset()&lt;/code&gt;, primarily because &lt;code&gt;empty()&lt;/code&gt; covers nearly all scenarios that &lt;code&gt;isset()&lt;/code&gt; does, along with additional checks. This article will argue why it is generally better to use &lt;code&gt;empty()&lt;/code&gt; consistently in PHP and will demonstrate this with multiple code examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;code&gt;isset()&lt;/code&gt; and &lt;code&gt;empty()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Before delving into specifics, let’s clarify what these language constructs do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;isset()&lt;/code&gt;: This language construct checks if a variable is set and is not &lt;code&gt;NULL&lt;/code&gt;. It returns &lt;code&gt;true&lt;/code&gt; if the variable exists and its value is not &lt;code&gt;NULL&lt;/code&gt;; otherwise, it returns &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$var = 0;
echo isset($var); // Outputs: 1 (true)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;empty()&lt;/code&gt;: This language construct determines whether a variable is considered “empty”. A variable is considered empty if it does not exist or if its value equals &lt;code&gt;false&lt;/code&gt;. &lt;code&gt;empty()&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt; if the variable is empty according to the conditions as mentioned earlier; otherwise, it returns &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$var = 0;
echo empty($var); // Outputs: 1 (true)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why I Like &lt;code&gt;empty()&lt;/code&gt; More?
&lt;/h2&gt;

&lt;p&gt;The primary advantage of using &lt;code&gt;empty()&lt;/code&gt; over &lt;code&gt;isset()&lt;/code&gt; lies in its ability to handle multiple checks simultaneously. &lt;code&gt;empty()&lt;/code&gt; will check if a variable is set and if its value is empty (&lt;code&gt;""&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;0.0&lt;/code&gt;, &lt;code&gt;"0"&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, and an empty array). This eliminates the need to use both &lt;code&gt;isset()&lt;/code&gt; and a value check separately, reducing code complexity and potential errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Example 1: Checking Default Values
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$input = $_POST['data'] ?? '';

// Using isset()
if (isset($input) &amp;amp;&amp;amp; $input !== '') {
    echo "Input is set and not empty.";
} else {
    echo "Input is not set or empty.";
}

// Using empty()
if (!empty($input)) {
    echo "Input is set and not empty/falsy.";
} else {
    echo "Input is not set or empty/falsy.";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, &lt;code&gt;empty()&lt;/code&gt; simplifies the check by consolidating it into a single condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Example 2: Working with Arrays
&lt;/h2&gt;

&lt;p&gt;When working with arrays, especially with keys that may or may not exist, &lt;code&gt;empty()&lt;/code&gt; is particularly useful to avoid notices about undefined indexes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$data = ['username' =&amp;gt; 'JohnDoe'];

// Using isset()
if (isset($data['username']) &amp;amp;&amp;amp; $data['username'] !== '') {
    echo "Username is set and not empty.";
} else {
    echo "Username is missing or empty.";
}

// Using empty()
if (!empty($data['username'])) {
    echo "Username is set and not empty/falsy.";
} else {
    echo "Username is missing or empty/falsy.";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Example 3: Boolean Checks
&lt;/h2&gt;

&lt;p&gt;When checking boolean flags, especially those that might not be set, &lt;code&gt;empty()&lt;/code&gt; can avoid extra conditional checks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$options = ['enabled' =&amp;gt; false];

// Using isset()
if (isset($options['enabled']) &amp;amp;&amp;amp; $options['enabled']) {
    echo "Feature is enabled.";
} else {
    echo "Feature is disabled.";
}

// Using empty()
if (!empty($options['enabled'])) {
    echo "Feature is enabled.";
} else {
    echo "Feature is disabled.";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In PHP, using &lt;code&gt;empty()&lt;/code&gt; is often more efficient than &lt;code&gt;isset()&lt;/code&gt; because it comprehensively checks whether a variable is both set and has a non-false value. This makes it an excellent tool for reducing code verbosity and complexity while ensuring the robustness of checks. While there are specific cases where &lt;code&gt;isset()&lt;/code&gt; is necessary such as when distinguishing between false, null, and unset values. in general, &lt;code&gt;empty()&lt;/code&gt; provides a simple and powerful alternative for most use cases.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Using Firebase Cloud Messaging(FCM) for Push Notifications in PHP: A Complete Guide</title>
      <dc:creator>Ramin Omrani</dc:creator>
      <pubDate>Sun, 20 Oct 2024 00:43:25 +0000</pubDate>
      <link>https://forem.com/omr4ni/using-firebase-fcm-for-push-notifications-in-php-a-complete-guide-497e</link>
      <guid>https://forem.com/omr4ni/using-firebase-fcm-for-push-notifications-in-php-a-complete-guide-497e</guid>
      <description>&lt;p&gt;Push notifications are an essential tool for engaging users and keeping them informed about updates, messages, and other important events. Firebase Cloud Messaging (&lt;strong&gt;FCM&lt;/strong&gt;) is a cross-platform solution that allows you to send notifications to web, Android, and iOS devices for free. In this guide, we'll use the &lt;code&gt;lkaybob/php-fcm-v1&lt;/code&gt; package to set up &lt;strong&gt;FCM&lt;/strong&gt; and send push notifications with PHP.&lt;/p&gt;

&lt;p&gt;To get up and running with &lt;strong&gt;FCM&lt;/strong&gt; basics (client and server setup) you can watch this great tutorial on youtube : &lt;a href="https://www.youtube.com/watch?v=iz5arafmatc" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=iz5arafmatc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now how do we send a push notification with &lt;code&gt;lkaybob/php-fcm-v1&lt;/code&gt; after setting up our &lt;strong&gt;FCM&lt;/strong&gt; client and server?&lt;/p&gt;

&lt;p&gt;Here is an example with comments explaining how the code snippet works :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
require_once __DIR__ . '/vendor/autoload.php';

use phpFCMv1\Client;
use phpFCMv1\Notification;
use phpFCMv1\Recipient;

// Client instance should be created with path to service account key file
$client = new Client("/path/to/service_account.json");
$recipient = new Recipient();

// Either Notification or Data (or both) instance should be created
$notification = new Notification();

// Recipient could accept individual device token,
// the name of topic, and conditional statement
$recipient-&amp;gt;setSingleREcipient('DEVICE_TOKEN');

// Setup Notificaition title and bod
$notification-&amp;gt;setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY');

//if you have extra data to send to the client
if(!empty($extraData)) {
    //create a data object
    $data = new Data();

    //add extra data to the payload
    $data-&amp;gt;setPayload(["data" =&amp;gt; $dataArray]);

    // Build FCM request payload
    $client-&amp;gt;build($recipient, $notification, $data);
} else {
    $client-&amp;gt;build($recipient, $notification);
}

// You can check the result
// If successful, true will be returned
// If not, error message will be returned
$result = $client-&amp;gt;fire();

if(!$result) {
    //do something in case of error
} else {
    //do something in case of success
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By following the steps in the example above, you can successfully integrate Firebase Cloud Messaging into your PHP application and start sending push notifications using the &lt;code&gt;lkaybob/php-fcm-v1&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>php</category>
      <category>firebase</category>
      <category>mobile</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
