简体   繁体   中英

Java equals() equivalent in PHP

I want know whats Java equals() equivalent in PHP?

I want use it in if statement.

public function categories($lang) {
    echo 'LANG IN INDEX CONTROLLER CATEGORIES BEFORE: ' . $lang.'<br>';
    if ($lang !== "az" || $lang !== "en" || $lang !== "ru") {
        $lang = 'az';
    }
    echo 'LANG IN INDEX CONTROLLER CATEGORIES AFTER: ' . $lang;
    $db = new DBClass();
    $categories = $db->select("CALL SELECT_CATEGORY('$lang')");
    return $categories;
}

I sent $lang parameter 'en' and result LANG IN INDEX CONTROLLER CATEGORIES BEFORE: en LANG IN INDEX CONTROLLER CATEGORIES AFTER: az

I tryed strcmp() .

This code does't work, whats worng? How can I use == or strcmp() in if statement?

The line

if ($lang !== "az" || $lang !== "en" || $lang !== "ru") {

will always be true because because a scalar variable can't have three values at once. You need to use && :

if ($lang !== "az" && $lang !== "en" && $lang !== "ru") {

Another way to do this in PHP is using in_array to check if a variable is in a set of values. Since you want to check that $lang is not "az" , "en" or "ru" , then just negate the result:

$forbidden_langs = array("az", "en", "ru");
if (!in_array($lang, $forbidden_langs)) {
    //your code goes here...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM