Zum Inhalt springen

Empfohlene Beiträge

Geschrieben

hi und hallo,

kann mir jemand erklären wieso hier "false" ausgegeben wird??


$arrTest = Array(0,1,2,3,4);

$key = array_search(0,$arrTest);

if($key == NULL){

     echo("false");

}

denn "0" findet sich doch im array. nämlich an stelle [0]. wenn ich nach 1 oder 2 oder .... suche wird mir nicht false ausgegeben, die zahlen werden also gefunden.

Geschrieben

also


if($key === null){

    echo("false");

}

oder

if($key === 0){

    echo("false");

}

?

wenn ich dann aber nach -1 suche wird mir nicht false ausgegeben obwohl sich -1 ja nicht im array findet...

Geschrieben

siehe manual:

Description

mixed array_search ( mixed needle, array haystack [, bool strict])

Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise.

Note: If needle is a string, the comparison is done in a case-sensitive manner.

Note: Prior to PHP 4.2.0, array_search() returns NULL on failure instead of FALSE.

If the optional third parameter strict is set to TRUE then the array_search() will also check the types of the needle in the haystack.

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.


$arrTest = Array(0,1,2,3,4);
$key = array_search(0,$arrTest);
if(!$key){
echo "Da ist nichts drinn?!?";
} else {
echo "Juhu da ist doch was :)";
}
[/PHP]

oder:

[PHP]
$arrTest = Array(0,1,2,3,4);
$key = array_search(0,$arrTest);
if($key === false){
echo "Da ist nichts drinn?!?";
} else {
echo "Juhu da ist doch was :)";
}

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...