{"id":48,"date":"2020-12-09T22:13:36","date_gmt":"2020-12-09T22:13:36","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=48"},"modified":"2026-10-29T01:27:26","modified_gmt":"2026-10-29T01:27:26","slug":"php-types-of-variables","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/","title":{"rendered":"PHP &#8211; Types of Variables"},"content":{"rendered":"<p>PHP uses variables a lot in storing pieces of data used by the program.<\/p>\n<ol>\n<li><a href=\"#t1\">Characteristics of PHP Variables<a><\/li>\n<li><a href=\"#t2\">PHP Data Types<\/a><\/li>\n<li><a href=\"#t3\">Working With Booleans<\/a><\/li>\n<li><a href=\"#t4\">Working With Strings<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Characteristics of PHP Variables<\/strong><\/h4>\n<p>I have made a list of important points for you to bear in mind while working with PHP variables.<\/p>\n<ul>\n<li>Variables names in PHP must begin with a leading $ sign<\/li>\n<li>The variable takes the value of its most recent assignment. So if you have $x = 5 and $x = 10, then the value of $x would be 10<\/li>\n<li>Values of variables are assigned using the assignment operator(=).<\/li>\n<li>It is not mandatory to declare a variable before assignment, but it is sometimes a good practice<\/li>\n<li>In PHP, variables do not have intrinsic types &#8211; this means that a variable can be used to store any data type. So we say that PHP is not strongly typed or PHP is dynamically typed(or weakly typed)<\/li>\n<li>Variables that are used before being assigned assume default values<\/li>\n<li>PHP dynamically converts from one type to another when necessary<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. PHP Data Types<\/strong><\/h4>\n<p>We would take a look of the eight data types supported by PHP &#8211;<\/p>\n<ul>\n<li>Integers &#8211; these are whole numbers without a decimal point<\/li>\n<li>Doubles &#8211; floating point numbers like 4.534, 50.0<\/li>\n<li>Booleans\u00a0 &#8211; take one of two possible values: true or false<\/li>\n<li>NULL\u00a0 &#8211; as special type that has only one value: NULL<\/li>\n<li>Strings\u00a0 &#8211; sequences of characters. For example &#8216;Hello World&#8217;<\/li>\n<li>Arrays &#8211; named collection of values with each value having an integer index<\/li>\n<li>Objects\u00a0 &#8211; these are programmer-defined classes. Could be made up of other data types<\/li>\n<li>Resources &#8211; special variables whose value is a reference to resources external to PHP<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Working With Booleans<\/strong><\/h4>\n<p>As mentioned, Booleans can have only one of two values: True or False For example:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (<span style=\"color: #008800; font-weight: bold;\">TRUE<\/span>)\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"This will always show&lt;br&gt;\"<\/span>);\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">else<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"This will never show&lt;br&gt;\"<\/span>);\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>When this code executes, it would display the first string. Now here a few rules to bear in mind when it comes to working with booleans:<\/p>\n<ul>\n<li>if the value of a boolean is a number, then it is false if it exactly equals zero, else it is true.<\/li>\n<li>if the value is a string, then it is false if it is an empty string, else, it is true. Also, it the string is &#8220;0&#8221;, then it is false too<\/li>\n<li>NULL values are always false<\/li>\n<li>It the value is an array, then it is false if it contains not values, else it is true<\/li>\n<li>Valid resources are always true<\/li>\n<li>Double should not be used a boolean<\/li>\n<li>A variable assigned NULL returns false if tested with the isSet() function<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Exercise on Booleans<\/strong><\/p>\n<p>Look at the code below and answer what would be the value of each expression. Leave your answer in the comment box below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #996633;\">$num1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #6600ee; font-weight: bold;\">0.4159<\/span>;\r\n    <span style=\"color: #996633;\">$str1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"The First String\"<\/span>\r\n    <span style=\"color: #996633;\">$array1<\/span>[<span style=\"color: #0000dd; font-weight: bold;\">49<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"A numbers array\"<\/span>;\r\n    <span style=\"color: #996633;\">$array2<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">array<\/span>();\r\n    <span style=\"color: #996633;\">$null1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">NULL<\/span>;\r\n    <span style=\"color: #996633;\">$num2<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">223<\/span> <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">223<\/span>;\r\n    <span style=\"color: #996633;\">$str2<\/span><span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\"<\/span>;\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Working With Strings<\/strong><\/h4>\n<p>As mentioned before, strings are sequences of characters.<\/p>\n<p>Some examples are given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$string1 = \"This is string in double quotes\";\r\n$string2 = 'This is a kind of longer, singly quoted string';\r\n$string3 = \"This string is thirty-eight characters\";\r\n$string4 = \"\"; \/\/ a string of zero characters (empty string)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Single and Double Quotes<\/strong><\/p>\n<p>Strings in single quotes are treated literally whereas string in double quotes replaces the variables with the actual values. Take for example<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n   <span style=\"color: #996633;\">$variable1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"myname\"<\/span>;\r\n   <span style=\"color: #996633;\">$literal1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'My $variable1 will not print!'<\/span>;\r\n   \r\n   <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"color: #996633;\">$literal1<\/span>);\r\n   <span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br&gt;\"<\/span>;\r\n   \r\n   <span style=\"color: #996633;\">$literal1<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"My <\/span><span style=\"background-color: #eeeeee;\">$variable1<\/span><span style=\"background-color: #fff0f0;\"> will print!\"<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"color: #996633;\">$literal1<\/span>);\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>The output of the code would be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">My $variable1 will not print!\r\nMy myname will print!\r\n<\/pre>\n<p>So you see that for the first line, the variable was not replaced because the string is in single quotes. However the second string replaced the variable<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP uses variables a lot in storing pieces of data used by the program. Characteristics of PHP Variables PHP Data Types Working With Booleans Working &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Types of Variables | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Types of Variables | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-09T22:13:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:27:26+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Types of Variables\",\"datePublished\":\"2020-12-09T22:13:36+00:00\",\"dateModified\":\"2026-10-29T01:27:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/\"},\"wordCount\":531,\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/\",\"name\":\"PHP - Types of Variables | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-09T22:13:36+00:00\",\"dateModified\":\"2026-10-29T01:27:26+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-types-of-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Types of Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Types of Variables | Kindson The Genius","description":"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Types of Variables | Kindson The Genius","og_description":"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-09T22:13:36+00:00","article_modified_time":"2026-10-29T01:27:26+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Types of Variables","datePublished":"2020-12-09T22:13:36+00:00","dateModified":"2026-10-29T01:27:26+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/"},"wordCount":531,"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/","url":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/","name":"PHP - Types of Variables | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-09T22:13:36+00:00","dateModified":"2026-10-29T01:27:26+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Types of Variables \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-types-of-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Types of Variables"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/48\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}