{"id":617,"date":"2021-11-11T22:10:10","date_gmt":"2021-11-12T03:10:10","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=617"},"modified":"2024-02-08T08:45:51","modified_gmt":"2024-02-08T13:45:51","slug":"type-casting-in-php","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/type-casting-in-php\/","title":{"rendered":"Type casting in PHP"},"content":{"rendered":"\n<p>PHP does not support explicit type definition in variable declaration. For example, you CANNOT declare a variable as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int $count = 10;\n<\/code><\/pre>\n\n\n\n<p>In PHP, a variable&#8217;s type is determined by the context in which the variable is used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$student = \"23\";    \/\/ $students is a string\n$girls = 13;         \/\/ $girls is an int\n$boys = $students - $girls;    \/\/ all three variables are converted to int.\n<\/code><\/pre>\n\n\n\n<p>There are many scenarios when automatic typecasting could lead to erroneous functionality. In such cases, you can manually cast the variables.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$count = 1;     \/\/ $count is an int but I need it to be an boolean\n$count = (bool) $count;    \/\/ $count is a boolean now\n<\/code><\/pre>\n\n\n\n<p>Following casts are allowed in PHP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>binary will cast to binary\nbool, boolean will cast to boolean\nfloat, double will cast to float\nint, integer will cast to integer\nstring will cast to string\narray and object can also be used for casting<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP does not support explicit type definition in variable declaration. For example, you CANNOT declare a variable as follows: In PHP, a variable&#8217;s type is determined by the context in which the variable is used. There are many scenarios when automatic typecasting could lead to erroneous functionality. In such cases, you can manually cast the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/617\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}