SugarClub
SugarClub
  • User
  • Site
  • Search
  • User
  • Groups & Discussions
    Groups & Discussions
    • Product Forums
      Product-focused Q&A, discussions, best practices, fixes, and help
      Product Forums
      • Sugar Market
      • Sugar Sell & Enterprise
      • Sugar Serve
      • sales-i
    • User Groups
      Professional, Industry, Language
    • Get Involved
      Learn how to become a Raving Fan
    • Social Club
      Live, interactive, virtual meetups with other Sugar customers and Sugar’s Subject Matter experts!
    • Leadership Lounge
      Network with fellow organizational leaders, ask questions, and share insights
    • Developers
      Visit DevClub, the SugarClub group for Sugar Developers
      Developers
      • DevClub
      • Mobile Developers
      • Developer Builds
        Supplemental access level required. Inquiries: developers@sugarcrm.com
    • Additional Groups (Access Required)
      Groups that require special access will be displayed here. Contact sugarclub@sugarcrm.com for assistance. Click here to see all groups
      Additional Groups (Access Required)
      • SugarCloud Platform
  • Product Information
    Product Information
    • Release Central
      Find release-specific content to prepare for your next Sugar update
    • Documentation & Resources
      Looking to expand your Sugar knowledge? Explore our in-depth documentation and other helpful resources!
    • Product Update Blogs
      Updates about each Sugar product
    • Customer Stories »
      Case Studies by SugarCRM
  • Training & Certification
    Training & Certification
    • Training & Certification Home
      Live & On-Demand classes, Quick Videos, Sugar Certifications, and more!
    • Quick Videos
      Short videos about using Sugar
    • My SugarU Dashboard »
    • SugarU News & Updates
  • Adoption
    Adoption
    • Grow Adoption Framework
      Get started on your adoption journey and review the adoption resources from SugarCRM
  • Calendar
  • News
    News
    • Sugar News
    • SugarCRM.com News »
    • Dev Blog
    • SugarCRM Marketplace Blog
  • Help
    Help
    • Welcome to Sugar!
      New to Sugar? Get started here!
    • SugarClub Help & Instructions
      Learn more about SugarClub and find answers to questions about this site
    • New to SugarClub?
      Start your community journey here
    • Technical Support
      Sugar's support resources
      Technical Support
      • Case Portal »
        Access the SugarCRM Case Portal
      • Working with Sugar Support »
        Find out more about engaging with the SugarCRM Support team
      • SugarCloud Information
        Find information about SugarCloud service updates and site status. Contact sugarclub@sugarcrm.com to request access
  • More from Sugar
    More from Sugar
    • DevClub
    • PartnerClub
    • Support
    • SugarOutfitters Marketplace
    • sugarcrm.com
  • DevClub
  • PartnerClub
  • Support
  • Marketplace
  • sugarcrm.com
DevClub
DevClub
Dev Tutorials PHP 7.4 Warnings to PHP8.2 Errors
Click here to join this group and curate your SugarClub experience.
  • +On-Boarding Framework
  • +Customization Guides
  • +Modern UI Technical Guide
  • -Automated PHP Compatibility Tool
    • Rector Basic Setup
    • Prepare Rector for MLP/Addon
    • Prepare Rector for Customizations
    • Executing Rector
    • Keeping Compatibility
    • Troubleshooting Rector
    • PHP 7.4 Warnings to PHP8.2 Errors
  • +Customization Best Practices in Sugar
  • How to write code for SugarCloud webinar Q&A
  • HOW TO: enforce ACL on Tags
  • Remove custom fields created via package installation
  • Sugar Developer Tools
  • Tutorial:  How to register custom platforms in Sugar instances via Platform extension
  • Adding a google reCAPTCHA in a Web-to-Lead form
  • Sugar Developer Blog Style Guide

You are currently reviewing an older revision of this page.

  • History View current version

PHP 7.4 Warnings to PHP8.2 Errors

PHP Warnings to Runtime Errors

In the transition from PHP 7.4 to PHP 8.2, notable changes have occurred with certain runtime errors that were previously treated as warnings by PHP. As of PHP 8.x, these errors have been elevated to runtime errors.

Automated tools like Rector can do a lot when it comes to lexical scanning but runtime (non-lexical and more closely tied to the logic and data aspects of code execution) its help is limited.

Defensive coding techniques serve as a robust shield against runtime errors, empowering you to anticipate and prevent such issues.

In our research, we've identified several of these techniques designed to assist you in addressing and rectifying potential errors.

Enable PHP logs

The starting point to identify such errors is to enable PHP logs. This foundational step will provide the necessary data (warnings/errors) so you can implement effective defensive coding to prevent them to happen.

Enable E_WARNING and E_DEPRECATION Log in to the PHP error log and test your code.
  1. Locate your php.ini file. The location can vary based on your operating system and PHP installation. Common paths include:

    • Windows: C:\Program Files\PHP\php.ini
    • Linux: /etc/php/{version}/apache2/php.ini
  2. Open the php.ini file in a text editor with administrative privileges.

  3. Search for the error_reporting directive.

  4. Update the directive to include E_WARNING and E_DEPRECATED:

Fullscreen
1
error_reporting = E_ALL & ~E_NOTICE | E_WARNING | E_DEPRECATED
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
error_reporting = E_ALL & ~E_NOTICE | E_WARNING | E_DEPRECATED

PHP Warning: count(): Parameter must be an array or an object that implements Countable in *

  • safeCount will take care of it

PHP Warning: A non-numeric value encountered*

  • Cast the values to int or float before the arithmetic operation.
  • Check the values for is_numeric($value) and skip the calculation if the values are not numeric.

PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable in*

  • if you have code like $a = sizeof($b) + 1;
  • replace sizeof with safeCount=

PHP Warning: strlen() expects parameter 1 to be string, array given in*

Add defensive for example:

Fullscreen
1
2
3
4
5
if (is_string($some_value)) {
$len = strlen($some_value);
} else {
// throw error or deal with your data
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (is_string($some_value)) {
  $len = strlen($some_value);
} else { 
  // throw error or deal with your data
}


PHP Warning: array_key_exists(): The first argument should be either a string or an integer in*

  • Add defensive for example:

Fullscreen
1
2
3
4
5
if ((is_string($haystack) || is_numeric($haystack)) && array_key_exists($haystack, $myArray)) {
// continue with your logic
} else {
// throw error or deal with your data;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if ((is_string($haystack) || is_numeric($haystack)) && array_key_exists($haystack, $myArray)) {
    // continue with your logic  
} else { 
    // throw error or deal with your data;
}

PHP Warning: strpos(): Empty needle in*

  • Add defensive code, for example:

Fullscreen
1
2
3
4
5
if (isset($haystack) && !empty($haystack) && isset($needle) && !empty($needle) && strpos($haystack, $needle, $offset)) {
// continue with your logic
} else {
// throw error or deal with your data;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (isset($haystack) && !empty($haystack) && isset($needle) && !empty($needle) && strpos($haystack, $needle, $offset)) {
   // continue with your logic
} else { 
   // throw error or deal with your data;
}

PHP Warning: array_intersect(): Expected parameter 1 to be an array, null given in*

  • Add defensive for example:

Fullscreen
1
2
3
4
5
if (is_array($array) && is_array($arrays)) {
$intersect = array_intersect($array, $arrays);
} else {
// throw error or deal with your data;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (is_array($array) && is_array($arrays)) {
   $intersect = array_intersect($array, $arrays);
} else { 
   // throw error or deal with your data;
}