Blog Detail

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;
}

?>

About Author

Lovish Sharma

I am software engineer and always ready to learn new technologies.

View Profile

Comments (1)

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

    nice

Leave a comment

Related Blogs

Metrics Aggregation in Elasticsearch

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

Basics of Data Search in Elasticsearch

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

Elasticsearch Rest API

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

Bucket Aggregation in Elasticsearch

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

Wildcard and Boolean Search in Elasticsearch

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

Introduction to Elasticsearch Aggregations

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

Create a Pie Chart in Kibana

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

Create word cloud in Python

Jun 30, 2018, 6:06:45 AM | Anurag Srivastava

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Elasticsearch Rest API

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

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

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