Home > database >  How to check if a String is not empty in php, to prevent undefined function calls?
How to check if a String is not empty in php, to prevent undefined function calls?

Time:02-03

So basically, I have a String that is located in a two dimensional Array like this.

$array[$i][$k];

I applied some includes() functions on those strings to change it's color whenever it contains "SA" but not "SAP" or "Schulaufgabe" OR "Unterrichtsende"

if ($array[$i][$k].includes("SA") && !$array[$i][$k].includes("SAP") || 
$array[$i][$k].includes("Schulaufgabe") || $array[$i][$k].includes("Unterrichtsende"))

I tried many options to prevent this error: Fatal error: Uncaught Error: Call to undefined function includes() in C:\xampp\htdocs\test\test.php:101 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\test.php on line 101

For example with this method.

if (strlen($array[$i][$k]) != 0)

None of them did work. Are there any other options to get rid of this error?

CodePudding user response:

if (preg_match('/SA(?!P)|Schulaufgabe|Unterrichtsende/',$array[$i][$k])) {
    echo "<td style='color: red'>" . $array[$i][$k];          
}

The Answer was in the method .includes() which just partly worked, I do not recommend using it! Even though I found it somewhere in the phpManual

CodePudding user response:

     //not empty 
     if(!empty($array[$i][$k])) {
        echo "Variable is not empty.<br>";
      }

      //not empty

      if($array[$i][$k] !="") {
        echo "Variable is not empty.<br>";
      }

      //not empty
      
     if($array[$i][$k] !=NULL) {
        echo "Variable is not empty.<br>";
      }
      //contains SA

      if(preg_match('SA',$array[$i][$k])) 
      {
       echo "success";          
      }

      
  •  Tags:  
  • Related