{"id":471343,"date":"2010-03-25T12:37:12","date_gmt":"2010-03-25T16:37:12","guid":{"rendered":"http:\/\/www.icantinternet.org\/?p=581"},"modified":"2010-03-25T12:37:12","modified_gmt":"2010-03-25T16:37:12","slug":"php-lesson-2-data-types","status":"publish","type":"post","link":"https:\/\/mereja.media\/index\/471343","title":{"rendered":"PHP Lesson 2: Data types"},"content":{"rendered":"<p>Today&#8217;s PHP lesson is about data types. Although PHP can work with many different data types, we divide them in two different categories: scalar data and composite data.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Scalar data<\/strong><\/span><\/p>\n<p>Scalar data can hold only one value at a time. PHP knows four different types of scalar data:<br \/>\nBoolean: possible values are TRUE or FALSE<br \/>\nint: can be any signed (so both positive and negative values are possible) numeric integer value<br \/>\nfloat: can be any signed (so both positive and negative values are possible) floating-point value<br \/>\nstring: can be any collection of binary data<\/p>\n<p><strong>Numeric values<\/strong><br \/>\nNumeric values can be both of the type integer as floating point, and can be annotated in decimal, octal (identified by leading zero) or hexadecimal (identified by leading 0x), and for floating-popint numbers, they can be annotated in decimal and exponential (2E7, 2.3E5).<br \/>\nA very important thing to note when working with numbers i s that the precision and the range of your data types depends on the machine you are working on. For instance, &amp; 64-bit system will have a wider range than a 32-bit system. Also, PHP doesn&#8217;t track overflows. So stuffing your variable with too much data will NOT throw an error, but will result in a truncated value.<br \/>\nAnother thing to keep in mind is that floats are not always storing their value the way you would think. For example, if you execute the statement<\/p>\n<p style=\"padding-left: 30px;\"><em>integervar (int) (0.2 + 0.4) * 10);<br \/>\nprint integervar;<\/em><\/p>\n<p>The printed result will not be 6 as you would expect, but it will be 5. This is because the result of 0.2 and 0.4 is not stored as 0.6 in the float, but as 0.59999999, and times ten, this gives 5.999999<br \/>\nWhen this float is conversed to an int, it simply truncates everyting behind the fraction, resulting in 5.<br \/>\nThese things are not necessarily a disaster, but are limitations you should be aware of when doing calculations in your PHP code.<\/p>\n<p><strong>Strings<\/strong><br \/>\nWhen talking about strings, most programmers immediately think of text. This is indeed the case. In some languages. Not in PHP, oh no. In PHP, a string is an ordered collection of binary data. This data can be text, but can also be an image, an office document, or a mp3-file.<br \/>\nDue to the extended character of strings, and all that is possible with them in PHP, we will get back on them later.<\/p>\n<p><strong>Booleans<\/strong><br \/>\nBooleans are the easiest of all variables, as they can only hold one of two values: true or false, and are therefor mostly used in logical operations.<br \/>\nThere are some important things to know about booleans and conversions though:<br \/>\nWhen you convert any number (no matter if it is an integer or a float) to a boolean, the boolean will always be true, except when the value of the number is zero.<br \/>\nWhen you convert a string to a boolean, the boolean will always be true, except when the string is empty (null), or contains a single digit 0 (zero). When there are multiple zeros in the string (00000), the boolean will convert to true.<br \/>\nWhen you convert a boolean to a string, integer or float, the value will be 1 if the boolean was true, and 0 if the boolean is false.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Composite Data<\/strong><\/span><\/p>\n<p>PHP doesn&#8217;t only support the scalar data types we discussed above, but also supports two compound or composite datatypes. The name compound or somposite comes from the fact that they are in essence containers of other data.<\/p>\n<p><strong>Arrays<\/strong><br \/>\nArrays contain data of other datatypes in an ordered way. Arrays can contain any of the abovementioned scalar data. We will digg deeper into arrays in a future lesson.<\/p>\n<p><strong>Objects<\/strong><br \/>\nObjects contain both data and code. Objects are the cornerstones of Object Oriented Programming (OOP) (who would&#8217;ve thought, right?), and are also dugg into in a future lesson.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Special Data Types<\/strong><\/span><\/p>\n<p>Besides the two data types we mentioned above, there are also two very special data types:<\/p>\n<p><strong>NULL<\/strong><br \/>\nNull indicates that a variable has no value. A variable can get the NULL value by getting it specifically assigned, or by not yet having been assigned any value at all.<\/p>\n<p><strong>Resource<\/strong><br \/>\nThe resource data type is used when dealing with external resources that are not part of PHP, but are used inside your code, for example a file.<\/p>\n<p>Converting between data types<\/p>\n<p>PHP is very easy when it comes to converting data types, as it does all the work for you, for free! However, if you really want to convert a variable to another data type yourself, you can do this by putting the name of the data type you want to convert to between parentheses, and place it in front of the expression or variable you want to convert. Check it out, it was already there in the example we used before:<\/p>\n<p style=\"padding-left: 30px;\"><em>integervar (int) (0.2 + 0.4) * 10);<\/em><\/p>\n<p><!-- Begin SexyBookmarks Menu Code --><\/p>\n<div class=\"sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy\">\n<ul class=\"socials\">\n<li class=\"sexy-delicious\">\n\t\t\t<a href=\"http:\/\/del.icio.us\/post?url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Share this on del.icio.us\">Share this on del.icio.us<\/a>\n\t\t<\/li>\n<li class=\"sexy-digg\">\n\t\t\t<a href=\"http:\/\/digg.com\/submit?phase=2&amp;url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Digg this!\">Digg this!<\/a>\n\t\t<\/li>\n<li class=\"sexy-reddit\">\n\t\t\t<a href=\"http:\/\/reddit.com\/submit?url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Share this on Reddit\">Share this on Reddit<\/a>\n\t\t<\/li>\n<li class=\"sexy-yahoobuzz\">\n\t\t\t<a href=\"http:\/\/buzz.yahoo.com\/submit\/?submitUrl=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;submitHeadline=PHP+Lesson+2%3A+Data+types&amp;submitSummary=Today%27s%20PHP%20lesson%20is%20about%20data%20types.%20Although%20PHP%20can%20work%20with%20many%20different%20data%20types%2C%20we%20divide%20them%20in%20two%20different%20categories%3A%20scalar%20data%20and%20composite%20data.%0D%0A%0D%0AScalar%20data%0D%0A%0D%0AScalar%20data%20can%20hold%20only%20one%20value%20at%20a%20time.%20PHP%20knows%20four%20different%20types%20of%20scalar%20data%3A%0D%0ABoolean%3A%20possible&amp;submitCategory=science&amp;submitAssetType=text\" rel=\"\" class=\"external\" title=\"Buzz up!\">Buzz up!<\/a>\n\t\t<\/li>\n<li class=\"sexy-stumbleupon\">\n\t\t\t<a href=\"http:\/\/www.stumbleupon.com\/submit?url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Stumble upon something good? Share it on StumbleUpon\">Stumble upon something good? Share it on StumbleUpon<\/a>\n\t\t<\/li>\n<li class=\"sexy-technorati\">\n\t\t\t<a href=\"http:\/\/technorati.com\/faves?add=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/\" rel=\"\" class=\"external\" title=\"Share this on Technorati\">Share this on Technorati<\/a>\n\t\t<\/li>\n<li class=\"sexy-mixx\">\n\t\t\t<a href=\"http:\/\/www.mixx.com\/submit?page_url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Share this on Mixx\">Share this on Mixx<\/a>\n\t\t<\/li>\n<li class=\"sexy-facebook\">\n\t\t\t<a href=\"http:\/\/www.facebook.com\/share.php?v=4&amp;src=bm&amp;u=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;t=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Share this on Facebook\">Share this on Facebook<\/a>\n\t\t<\/li>\n<li class=\"sexy-twitter\">\n\t\t\t<a href=\"http:\/\/twitter.com\/home?status=PHP+Lesson+2%3A+Data+types+-+http:\/\/b2l.me\/k6tpw+(via+@Icantinternet)\" rel=\"\" class=\"external\" title=\"Tweet This!\">Tweet This!<\/a>\n\t\t<\/li>\n<li class=\"sexy-comfeed\">\n\t\t\t<a href=\"http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/feed\" rel=\"\" class=\"external\" title=\"Subscribe to the comments for this post?\">Subscribe to the comments for this post?<\/a>\n\t\t<\/li>\n<li class=\"sexy-linkedin\">\n\t\t\t<a href=\"http:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types&amp;summary=Today%27s%20PHP%20lesson%20is%20about%20data%20types.%20Although%20PHP%20can%20work%20with%20many%20different%20data%20types%2C%20we%20divide%20them%20in%20two%20different%20categories%3A%20scalar%20data%20and%20composite%20data.%0D%0A%0D%0AScalar%20data%0D%0A%0D%0AScalar%20data%20can%20hold%20only%20one%20value%20at%20a%20time.%20PHP%20knows%20four%20different%20types%20of%20scalar%20data%3A%0D%0ABoolean%3A%20possible&amp;source=iCan&#039;t%20Internet\" rel=\"\" class=\"external\" title=\"Share this on Linkedin\">Share this on Linkedin<\/a>\n\t\t<\/li>\n<li class=\"sexy-blogmarks\">\n\t\t\t<a href=\"http:\/\/blogmarks.net\/my\/new.php?mini=1&amp;simple=1&amp;url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/&amp;title=PHP+Lesson+2%3A+Data+types\" rel=\"\" class=\"external\" title=\"Mark this on BlogMarks\">Mark this on BlogMarks<\/a>\n\t\t<\/li>\n<li class=\"sexy-blogengage\">\n\t\t\t<a href=\"http:\/\/www.blogengage.com\/submit.php?url=http:\/\/www.icantinternet.org\/2010\/03\/php-lesson-2-data-types\/\" rel=\"\" class=\"external\" title=\"Engage with this article!\">Engage with this article!<\/a>\n\t\t<\/li>\n<\/ul>\n<div style=\"clear:both;\"><\/div>\n<\/div>\n<p><!-- End SexyBookmarks Menu Code --><\/p>\n<p>Related posts:<\/p>\n<ol>\n<li><a href='http:\/\/www.icantinternet.org\/2010\/04\/php-lesson-3-variables-and-constants\/' rel='bookmark' title='Permanent Link: PHP Lesson 3: Variables and Constants'>PHP Lesson 3: Variables and Constants<\/a> <small>Variables Variables are containers, they contain data. PHP is a&#8230;<\/small><\/li>\n<li><a href='http:\/\/www.icantinternet.org\/2010\/02\/php-lesson-1-general-introduction\/' rel='bookmark' title='Permanent Link: PHP Lesson 1: General Introduction'>PHP Lesson 1: General Introduction<\/a> <small>What is PHP? PHP is a scripting language, primarily intended&#8230;<\/small><\/li>\n<li><a href='http:\/\/www.icantinternet.org\/2009\/01\/recognizing-the-iphone-for-webpages\/' rel='bookmark' title='Permanent Link: Recognizing the iPhone for webpages'>Recognizing the iPhone for webpages<\/a> <small>As said in my 2009 predictions, the iPhone, and other&#8230;<\/small><\/li>\n<\/ol>\n<p><a href=\"http:\/\/feedads.g.doubleclick.net\/~a\/zMiKD29Y8Sy1uTQwk2QHiO5jlqY\/0\/da\"><img decoding=\"async\" src=\"http:\/\/feedads.g.doubleclick.net\/~a\/zMiKD29Y8Sy1uTQwk2QHiO5jlqY\/0\/di\" border=\"0\" ismap=\"true\"><\/img><\/a><br \/>\n<a href=\"http:\/\/feedads.g.doubleclick.net\/~a\/zMiKD29Y8Sy1uTQwk2QHiO5jlqY\/1\/da\"><img decoding=\"async\" src=\"http:\/\/feedads.g.doubleclick.net\/~a\/zMiKD29Y8Sy1uTQwk2QHiO5jlqY\/1\/di\" border=\"0\" ismap=\"true\"><\/img><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~r\/IcantInternet\/~4\/ssslV25dK5I\" height=\"1\" width=\"1\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s PHP lesson is about data types. Although PHP can work with many different data types, we divide them in two different categories: scalar data and composite data. Scalar data Scalar data can hold only one value at a time. PHP knows four different types of scalar data: Boolean: possible values are TRUE or FALSE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-471343","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/471343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/comments?post=471343"}],"version-history":[{"count":0,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/471343\/revisions"}],"wp:attachment":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/media?parent=471343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/categories?post=471343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/tags?post=471343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}