Typecasting in PHP for short datatype
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; }?>
2 likes | 1181 views | 1 comments | bookmark |
Related Blogs
Introduction to Kibana
Aug 1, 2020, 6:19:45 PM | Anurag Srivastava
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava
Introduction to Elasticsearch Aggregations
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Basics of Data Search in Elasticsearch
Aug 4, 2018, 7:02:21 AM | Anurag Srivastava
Top Blogs
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Elasticsearch REST APIs
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
How to count number of words in a HTML string and find Read time in Python 3
Jun 30, 2018, 12:07:47 PM | jitender yadav
Create a Chess board in PHP
Mar 9, 2020, 8:45:41 AM | Rocky Paul
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava