Home > Net >  How to write multiple conditions with functions in laravel?
How to write multiple conditions with functions in laravel?

Time:11-20

I have one function called conversionData() which takes the input from the request and converts based on the type of input .

Public static function conversationData (Request $request){
   $value=$request->type;
   if(is_float($value)){
     //return some code
   }
   if(is_string($value)){
     // Return code
   }
   else{
// If it's integer
     return $value;
  }
}

if i give in url as {url}&type=33.34 it should exceute is_float condition but it executes is_string condition.how to execute correctly please help me

CodePudding user response:

you can check for float then integer then string, cheking $value for float using this way:

$floatVal = floatval($value);
// If the parsing succeeded and the value is not equivalent to an int
if($floatVal && intval($floatVal) != $floatVal)
{
    // $num is a float
    //return some code
}

it's not a float, then continue cheking:

     if(is_integer($value)){
         //return some code
       }
if(is_string($value)){
     // Return code
   }
   else{

     return $value;
  }

CodePudding user response:

You can use gettype($value) function and then, usign a switch case you can accordingly apply the logic

switch(gettype($value)) {
   case 'integer':
      // write integer code here
      break;
   case 'string':
      // write string code here
      break;
   default:
      break;
}

For more information on gettype(); You can refer: https://www.php.net/manual/en/function.gettype.php

  • Related