-3
<?php

function getLeeftijdsCategorie($leeftijd){
if($leeftijd<18){
    $categorie="kind";
}
elseif($leeftijd>=18&&$leeftijd<65){
    $categorie="volwassen";
}else{
    $categorie="bejaard";
}
return $categorie;
}

//globale array met leeftijden
$aLeeftijden = array(16,17,18,14,22,34,67,58,8,4,55,22,34,45,35);

$aantalKind = 0;
$aantalBejaard = 0;                    
$aantalVolwassen = 0;
for ($x=0; $x <= count($aLeeftijden); $x++) { 

    if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'kind') {
        $aantalKind;
    }

    if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'volwassen') {
        $aantalVolwassen++;
    }

    if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'bejaard') {
        $aantalBejaard++;
    }

}
echo "Aantal kinderen : ".$aantalKind;
echo "<br>Aantal volwassen personen  : ".$aantalVolwassen;
echo "<br>Aantal bejaarden  : ".$aantalBejaard;
    ?>

Hi, im getting 5 error messages can someone please help me i need to get how many people are children etcetra.

I already tried over an hour but i really cant find it.

The error message is:

PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 33 PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 37 PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 41

Thanks

2 Answers2

1

You “function return value in write context” is related to this line:

if (getLeeftijdsCategorie($aLeeftijden[$x]) = 'bejaard') {

You have to change = in ==.

Then, there is also a Parse Error:

echo "<br>Aantal bejaarden  : "$aantalBejaard;

must be:

echo "<br>Aantal bejaarden  : " . $aantalBejaard;
#                               ↑

The undefined offset error is due to your for loop construction:

for ($x=0; $x <= count($aLeeftijden); $x++) { 

must be:

for ($x=0; $x < count($aLeeftijden); $x++) { 

$aLeeftijden count is 15, but last index is 14.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

Give the following a try:

// Improved readability    
function getLeeftijdsCategorie( $leeftijd ) {
  if( $leeftijd < 18 ) {
    $categorie = "kind";
  } else if( $leeftijd >= 18 && $leeftijd < 65 ){
    $categorie = "volwassen";
  } else {
    $categorie = "bejaard";
  }
 return $categorie;
}

//globale array met leeftijden
$aLeeftijden = array(16, 17, 18, 14, 22, 34, 67, 58, 8, 4, 55, 22, 34, 45, 35);

$aantalKind = 0;
$aantalBejaard = 0;                    
$aantalVolwassen = 0;

for( $x = 0; $x < count( $aLeeftijden ); $x++ ) { 

    if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'kind') {
        $aantalKind++; // Forgot ++
    }

    if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'volwassen') {
        $aantalVolwassen++;
    }

    // Forgot =
    if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'bejaard') {
        $aantalBejaard++;
    }

}

// Writing strings like this is much less prone to errors 
echo "Aantal kinderen : {$aantalKind}";
echo "<br>Aantal volwassen personen  : {$aantalVolwassen}";
echo "<br>Aantal bejaarden  : {$aantalBejaard}";

don't close php if you include it in some other file this may lead to other errors if you have spaces after the closing php tag.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74