Blog Detail

Covid-19 Global Tracker

preview image Programming
by Lovish Sharma, Jun 8, 2018, 8:03:52 AM | 1 minutes |

Typecasting in PHP for short datatype

Like java we don't have short datatype in PHP. Before we proceed lets understand about short datatype in java.


short : The short datatype is a 16 bit signed two's complement integer and its value range is -32768 to 32767.

You can use the below program for PHP if you need to convert integer type value to short type value like Java.
<?php
//method to convert integer type value to short type value
function shortconversion($a){
    //checking datatype
    if(gettype($a) == 'integer'){
        //check the range of number
        if($a <= 32767 && $a >= -32768){
            return $a;            
        }
        //number greater than 32767
        if($a > 32767){
            $posit = $a - 32767;
            $retpost = -32768 + $posit - 1;
            return $retpost;
            
        }
        //number lesser than -32768
        if($a < -32768){
            $num = $a + 32768;
            $a = -$a;
            $gen_num = $a + ($num*2);
            return $gen_num;
        }
    }else{
        return false;
    }
    
}
//pass any integer value to function
$con_val = shortconversion(-32771.);
if($con_val == false){
    echo 'Only Integer Number Required';
}else{
    echo $con_val;
}

?>


Comments (1)

  • user image
    Sarita Yadav
    Jun 12, 2018, 6:39:23 AM

    nice

Leave a comment

Related Blogs

Elastic Stack Interview Questions

Sep 12, 2020, 3:58:55 PM | Anurag Srivastava

Introduction to Kibana

Aug 1, 2020, 6:19:45 PM | Anurag Srivastava

Create a Chess board in PHP

Mar 9, 2020, 8:45:41 AM | Rocky Paul

Handle Excel file using Python (Part 2)

Dec 31, 2019, 1:33:53 PM | Anurag Srivastava

Handle Excel file using Python (Part 1)

Dec 30, 2019, 11:20:08 AM | Anurag Srivastava

Create a Pie Chart in Kibana

Dec 24, 2018, 5:25:28 PM | Anurag Srivastava

Top Blogs

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava