Answers for "php foreach loop check if last"

PHP
1

php foreach if last item

$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
  if(++$i === $numItems) {
    echo "last index!";
  }
}
Posted by: Guest on May-19-2020
1

if first php

$x = 1;
$length = count($data); 

foreach($data as $value){
        echo"
            <button class='btn";
                if($x === 1){
                    echo " first";
                }
                else if($x === $length){
                    echo " last"; 
                }
                $x++;
        echo"
           '>$value->services_name</button>
        ";
    }
Posted by: Guest on September-28-2020

Code answers related to "php foreach loop check if last"

Browse Popular Code Answers by Language