{"id":390,"date":"2018-07-13T00:43:03","date_gmt":"2018-07-13T00:43:03","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=390"},"modified":"2018-08-02T00:49:01","modified_gmt":"2018-08-02T00:49:01","slug":"php-datatypes","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/","title":{"rendered":"Datatypes in PHP"},"content":{"rendered":"<p>Variables can contain data of different types. Datatypes are an essential feature of any programming language because they allow us to process different types of information. PHP largely supports the following primitive data types:<\/p>\n<ul>\n<li>String<\/li>\n<li>Integer<\/li>\n<li>Float ( Floating point numbers or double)<\/li>\n<li>Boolean<\/li>\n<li>Array<\/li>\n<li>Object<\/li>\n<li>NULL<\/li>\n<li>Resource<\/li>\n<\/ul>\n<h2>Strings in PHP<\/h2>\n<p>A set of characters like \u201cExample\u201d constitute a string. A string consists of text which is enclosed by quotes. Double or single quotes are used because we need to differentiate a string from the rest of the text in the program. Depending on the type of quote used the string is processed differently.<\/p>\n<p><b>Example :<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$msg_1 = \"Learn PHP because PHP is the best\";\n$msg_2 = \u2018Learn PHP because it is easy!';\necho $msg_1;\n\/\/ Output: Learn PHP because PHP is the best\necho $msg_2;\n\/\/ Output: Learn PHP because it is easy!\n?&gt;<\/textarea><\/div>\n<h2>Integers in PHP<\/h2>\n<p>Any whole number between -2,147,483,648 and 2,147,483,647 is called as an integer. The datatype which is used to represent an integer is called the Int datatype. PHP requires us to follow some rules while using integer data types :<\/p>\n<ul>\n<li>An integer must be a valid whole number between 2,147,483,648 and 2,147,483,647<\/li>\n<li>An integer does not have a decimal point and can either be positive or negative.<\/li>\n<li>Integers can be specified in 3 formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)<\/li>\n<\/ul>\n<p>In the following example, $no1 is an integer. The PHP var_dump() function returns the data type and value:<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$no1 = 40;\nvar_dump($no1);\n\/\/The output will be int(40) because\u00a0int represents the data type and 40 is the value of the number\n?&gt;<\/textarea><\/div>\n<h2>Float in PHP<\/h2>\n<p>A number which has a decimal point or a number in the exponential form is called a floating point number because of the decimal point which is usually a part of the number. The datatype Float is used for floating point numbers. In the following example var_dump will return the datatype and the value.<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$no1 = 17801.365;\nvar_dump($no1);\n\/\/The output is float(17801.365) because float represents the data type and 101.365 is the value of the number\n?&gt;<\/textarea><\/div>\n<h2>Boolean Variable<\/h2>\n<p>A Boolean variable has only 2 possible values : TRUE or FALSE.<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">$bool1=TRUE;\n$bool2=FALSE;<\/textarea><\/div>\n<p>Boolean variables are handy while doing conditional testing.<\/p>\n<h2>Arrays in PHP<\/h2>\n<p>An array is used to store multiple data elements(of the same type) using a single variable and then we can access it using indexes. Arrays are popular because we usually need to store single or multi-dimensional data. The var_dump() function will output the array elements and the data type.<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$class_2018 =\u00a0array(\"Mary\",\"Prathik\",\"Sahil\");\nvar_dump($array1);\n\/* Output will be array(3) { [0]=&gt; string(3) \"Mary\" [1]=&gt; string(6) \"Prathik\" [2]=&gt; string(4) \"Sahil\" } because both the data and the datatype needs to be displayed*\/\n?&gt;<\/textarea><\/div>\n<h2>Objects in PHP<\/h2>\n<p>An object is a data type that consists of data as well as information on how to process the data. An object is the only data type which requires explicit declaration.<\/p>\n<p>The declaration of an object class requires us to use the keyword Class. A class is a structure that consists of methods and properties.<\/p>\n<pre><b>Example:<\/b>\r\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\nclass example_class {\r\n\u00a0\u00a0\u00a0 function example_class() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;subject = \"Science\";\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n\r\n\/\/ create an object of type example_class\r\n$batch_2018 = new example_class();\r\n\r\n\/\/ object property is science because it is initialized in the constructor\r\necho $batch_2018-&gt;subject;\r\n\/*output will be \u201cScience\u201d because it was assigned the value when the object was created. *\/\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>PHP NULL value<\/h2>\n<p>The NULL datatype is so called because a variable of NULL datatype can only have a value of NULL. A NULL variable is a variable that has no value assigned to it. A variable can be assigned a value of NULL to empty it of its current value.<\/p>\n<p>If a variable is not assigned any value initially it is auto-assigned a value of NULL.<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$x = \"Learn PHP because it is the best\";\n$msg = null;\nvar_dump($msg);\n\/\/ The output is NULL because $msg is reassigned to NULL\n?&gt;<\/textarea><\/div>\n<h2>Resource Datatype in PHP<\/h2>\n<p>The resource datatype enables us to store a reference to functions, external resources, and databases which are required by the program. These references need to be stored because they are frequently required by the program. Eg. A database call can be of the resource datatype.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-390","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Datatypes in PHP - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Datatypes in PHP - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-13T00:43:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-02T00:49:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"shabbir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"shabbir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/\",\"name\":\"Datatypes in PHP - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-13T00:43:03+00:00\",\"dateModified\":\"2018-08-02T00:49:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6\"},\"description\":\"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Datatypes in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6\",\"name\":\"shabbir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"caption\":\"shabbir\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Datatypes in PHP - Go4Expert Tutorials","description":"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.","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:\/\/www.go4expert.com\/tutorials\/php-datatypes\/","og_locale":"en_US","og_type":"article","og_title":"Datatypes in PHP - Go4Expert Tutorials","og_description":"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-13T00:43:03+00:00","article_modified_time":"2018-08-02T00:49:01+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"shabbir","twitter_card":"summary_large_image","twitter_misc":{"Written by":"shabbir","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/","name":"Datatypes in PHP - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-13T00:43:03+00:00","dateModified":"2018-08-02T00:49:01+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6"},"description":"Learn about PHP datatypes and their uses. With in-depth coverage and working code snippets get a thorough understanding of the different datatypes.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-datatypes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Datatypes in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6","name":"shabbir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","caption":"shabbir"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-6i","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=390"}],"version-history":[{"count":8,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/390\/revisions\/421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}