{"id":261,"date":"2016-07-28T11:47:04","date_gmt":"2016-07-28T11:47:04","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/28\/type-cast\/"},"modified":"2024-03-03T15:01:37","modified_gmt":"2024-03-03T15:01:37","slug":"type-cast","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/type-cast\/","title":{"rendered":"Typecast"},"content":{"rendered":"<h2>How to Type cast variable \u00a0in PHP (Type casting)?<\/h2>\n<p>Typecast :\u00a0<a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP <\/a>does not \u00a0support type definition of the variable. In php we never define <a href=\"https:\/\/barkhane.com\/php\/understanding-php-data-type\/\" target=\"_blank\" rel=\"noopener\">data type<\/a> while declaring the <a href=\"https:\/\/barkhane.com\/php\/php-variable-deceleration\/\" target=\"_blank\" rel=\"noopener\">variable<\/a>. In PHP variables automatically decide the data type on the basis of the value assignment.<\/p>\n<p>We can cast following <a href=\"https:\/\/barkhane.com\/php\/understanding-php-data-type\/\" target=\"_blank\" rel=\"noopener\">data type<\/a> variable in php<\/p>\n<ol>\n<li>Integer \u00a0using (int) or (integer)<\/li>\n<li>Boolean using\u00a0(bool) or (boolean)<\/li>\n<li>Floating Number using (float) \u00a0or (real) or (double)<\/li>\n<li>String using (str)<\/li>\n<li>Array using (array)<\/li>\n<li>Object using (object)<\/li>\n<li>Null using (unset)<\/li>\n<\/ol>\n<p>For an example we are given $a=5;<\/p>\n<p>we can check datatype by gettype() function \u00a0gettype($a)<\/p>\n<p>now if you want to typecast \u00a0then you can do the following<\/p>\n<pre><code>     \n$b= (double)$a;\n\/\/\/ for type cast to double\n\n$b= (string)$a;\n\/\/\/ for type cast to string\n\n$b= (integer)$a;\n\/\/\/for type cast to integer\n\n$b= (bool)$a;\n\/\/\/for type cast to\u00a0Boolean\n\n$b= (array)$a;\n\/\/\/for type cast to array\n\n\/\/\/ Another type to typecast is <strong>settype()<\/strong>\n\n$a=242;\n\nsettype($a,\\'string\\');\n<\/code><\/pre>\n<p>Note: Settype changes datatype of variable ,where as typecasting \u00a0of changes datatype of new variable to which value is assign .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Type cast variable \u00a0in PHP (Type casting)? Typecast :\u00a0PHP does not \u00a0support type definition of the variable. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}