Php check if value exists

I was wondering if it is possible to have a general system function that checks for a value and if it doesn't exist return another value.

below is what I have come up with however if I use it on a value say $thisvalue but $thisvalue doesn't exist I am getting the error Undefined variable: thisvalue. If thisvalue is empty or null etc below works.

Thanks

public function checkingValue($value = NULL, $empty_return = NULL)
{
    if(isset($value))
    {
        return $value;
    }
    else
    {
        return $empty_return;
    }
    if(!empty($value))
    {
        return $value;
    }
    if(!is_null($value))
    {
        return $value;
    }

    return $empty_return;
}

asked May 23, 2013 at 1:24

isset and empty are not functions, they're actually language constructs like switch and list and class.

If you do this:

function my_isset($var) {
    return isset($var);
}

isset($undef); // false
my_isset($undef); // Error: Undefined variable: $undef

answered May 23, 2013 at 1:29

Php check if value exists

HalcyonHalcyon

56.6k10 gold badges87 silver badges126 bronze badges

2

In this article, we will see how to find the value in the array using the in_array() function in PHP, & will also understand its implementation through the examples.

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise. 

Syntax:

bool in_array( $val, $array_name, $mode )

Parameters: The in_array() function accepts 3 parameters, out of which 2 are compulsory and another 1 is optional. All three parameters are described below:

  • $val: This is a required parameter that specifies the element or value to be searched in the given array. This parameter can be of mixed type i.e, it can be of string type or integer type, or any other type. If this parameter is of string type then the search will be performed in a case-sensitive manner.
  • $array_name: This is a required parameter and it specifies the array in which we want to search.
  • $mode: This is an optional parameter and is of boolean type. This parameter specifies the mode in which we want to perform the search. If it is set to TRUE, then the in_array() function searches for the value with the same type of value as specified by the $val parameter. The default value of this parameter is FALSE.

Return Value: The in_array() function returns a boolean value i.e, TRUE if the value $val is found in the array otherwise it returns FALSE.

Approach: In order to search an array for a specific value, we will be using the in_array() function where the parameter for the search is of string type & its value is set to true. Otherwise, this function returns a false value if the specified value is not found in an array.

We will understand the concept of the in_array() function in PHP, through the example.

Example 1: The below program performs the search using the in_array() function in non-strict mode ie, the last parameter $mode is set to false which is its default value. The value to be searched is of string type whereas this value in the array is of integer type still the in_array() function returns true as the search is in non-strict mode.

PHP

  $marks = array(100, 65, 70, 87);

  if (in_array("100", $marks))

  {

    echo "found";

  }

  else

  {

    echo "not found";

  }

?>

Example 2: The below program performs the search using the in_array() function in strict mode ie., the last parameter $mode is set to true and the function will now also check the type of values.

PHP

  $name = array("ravi", "ram", "rani", 87);

  if (in_array("ravi", $name, TRUE))

    {

    echo "found \n";

    }

  else

    {

    echo "not found \n";

    }

  if (in_array(87, $name, TRUE))

    {

    echo "found \n";

    }

  else

    {

    echo "not found \n";

    }

  if (in_array("87", $name, TRUE))

    {

    echo "found \n";

    }

  else

    {

    echo "not found \n";

    }

?>

Output

found 
found 
not found 

Reference: http://php.net/manual/en/function.in-array.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How do you check if a value exists in an array PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

How do you check if a value exists in an array?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.

How check variable is empty or not in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

What is use of count () function in PHP?

The count() function returns the number of elements in an array.