Typecasting in PHP for short datatype


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

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

preview thumbnail
Introduction to Kibana

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

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava

preview thumbnail
Introduction to Elasticsearch Aggregations

Aug 14, 2018, 4:47:56 PM | Anurag Srivastava

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

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

preview thumbnail
Basics of Data Search in Elasticsearch

Aug 4, 2018, 7:02:21 AM | Anurag Srivastava

Top Blogs

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

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

preview thumbnail
Elasticsearch REST APIs

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

preview thumbnail
preview thumbnail
Create a Chess board in PHP

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

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava