How To Use Is Array Function With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

PHP is_array() Function is used to check whether a variable is an array or not. is_array() function is an inbuilt function in PHP.

PHP is_array() function with example

Syntax for is_array() Function
 

is_array(variable);


Parameter   Description

variable    Specifies the variable to check
 

Example(1)
 

$variable1=1;
$variable2=array('BIKE_1', 'BIKE_2', 'BIKE_3');

if (is_array($variable1))
    echo "variable1 is a array value";
else
    echo "variable1 is not a array value";

if (is_array($variable2))
    echo "<h1>variable2 is an array </h1>";
else
    echo "<h1>variable2 is not a array value</h1>";


the above example we create two variable called as variable1 and variable2 and by using if condition we check whether variables are array or not.



Complete code of is_array() function.
 
<!DOCTYPE html>
<html>
<head>
    <title>How to use PHP is_array() function with example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>PHP is_array() function</h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        // PHP code to demonstrate working of is_array()
        $variable1=1;
        $variable2=array('BIKE_1', 'BIKE_2', 'BIKE_3');

        // $variable_name1 is not array, gives FALSE
        if (is_array($variable1))
        {
            echo "<h1>variable1 is a array value</h1>";
        }
        else
        {
            echo "<h1>variable1 is not a array value</h1>";
        }

        // $variable2 is an array, returns TRUE
        if (is_array($variable2))
        {
            echo "<h1>variable2 is an array </h1>";
        }
        else
        {
            echo "<h1>variable2 is not a array value</h1>";
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post