{"id":581,"date":"2010-03-25T17:37:12","date_gmt":"2010-03-25T16:37:12","guid":{"rendered":"http:\/\/www.icantinternet.org\/?p=581"},"modified":"2016-12-21T07:01:02","modified_gmt":"2016-12-21T07:01:02","slug":"php-lesson-2-data-types","status":"publish","type":"post","link":"https:\/\/beheydt.be\/en\/php-lesson-2-data-types\/","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><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-4198 alignright\" src=\"https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896-300x225.jpg\" alt=\"photo-2015-05-19-12-56-17_1896\" width=\"300\" height=\"225\" srcset=\"https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896-300x225.jpg 300w, https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896-768x576.jpg 768w, https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896-1024x768.jpg 1024w, https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896.jpg 2048w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>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><a class=\"thirstylink\" title=\"Learning PHP, MySQL &amp; JavaScript: With jQuery, CSS &amp; HTML5 (Learning Php, Mysql, Javascript, Css &amp; Html5)\" href=\"https:\/\/beheydt.be\/link\/others-amazon\/learning-php-mysql-javascript-with-jquery-css-html5-learning-php-mysql-javascript-css-html5\/\" target=\"_blank\" rel=\"nofollow\"><img loading=\"lazy\" decoding=\"async\" class=\"thirstylinkimg alignleft\" title=\"Learning PHP, MySQL &amp; JavaScript: With jQuery, CSS &amp; HTML5 (Learning Php, Mysql, Javascript, Css &amp; Html5)\" src=\"https:\/\/beheydt.be\/wp-content\/uploads\/2016\/11\/51mk9J-dbL.jpg\" alt=\"Learning PHP, MySQL &amp; JavaScript: With jQuery, CSS &amp; HTML5 (Learning Php, Mysql, Javascript, Css &amp; Html5)\" width=\"249\" height=\"327\" \/><\/a>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","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":4198,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[942],"tags":[624],"class_list":["post-581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-webdevelopment-icantinternet-en","tag-php"],"jetpack_featured_media_url":"https:\/\/beheydt.be\/wp-content\/uploads\/2009\/03\/Photo-2015-05-19-12-56-17_1896.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbkKxJ-9n","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":0,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/media\/4198"}],"wp:attachment":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}