{"id":2698,"date":"2015-11-26T08:33:02","date_gmt":"2015-11-26T08:33:02","guid":{"rendered":"http:\/\/www.webhozz.com\/blog\/?p=2698"},"modified":"2019-06-27T07:46:49","modified_gmt":"2019-06-27T07:46:49","slug":"cara-mengubah-tipe-data-php-type-juggling-dan-type-casting","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/","title":{"rendered":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting)"},"content":{"rendered":"<p>Pengertian Type Juggling dalam PHP<\/p>\n<p>PHP merupakan bahasa pemograman yang tidak terlalu ketat dalam aturan tipe data (dimana sebuah variabel dapat diisi dengan berbagai tipe data). Hal ini memberikan kemudahan penulisan, namun juga mendatangkan permasalahan tersendiri. Terkadang PHP mengubah tipe data suatu variabel menjadi tipe data lainnya secara tidak langsung tanpa kita instruksikan.<\/p>\n<p>Jika anda telah mengikuti tutorial PHP di duniailkom tentang pembahasan tipe data dan operator, maka beberapa kali kita telah membuat kode program yang \u201cmemaksa\u201d suatu tipe data berfungsi sebagai tipe data lainnya.<\/p>\n<p>Perhatikan kode program php berikut:<\/p>\n<p>Dalam kode diatas, saya mendefenisikan variabel $a sebagai integer (angka) dan variabel $b sebagai string. Operasi penambahan seharusnya membutuhkan 2 inputan berupa angka, namun seperti yang terlihat ketika program dijalankan, PHP dengan senang hati akan menjalankan perintah tersebut tanpa mengeluarkan error. 12+\u201d9 kucing\u201d akan menghasilkan nilai 21.<\/p>\n<p>Proses perubahan tipe data ini dikenal dengan istilah type juggling. Type Juggling dalam PHP adalah sebuah proses untuk menentukan jenis tipe data yang \u201ccocok\u201d dengan operasi saat itu, dan PHP akan menkonversinya secara langsung.<\/p>\n<p>PHP akan mencoba \u201cmenebak\u201d dan mengubah tipe data agar disesuaikan dengan peruntukannya. Misalkan operator aritmatika seharusnya membutuhkan 2 buah inputan (atau operand) yang bertipe angka (baik berupa integer maupun float). Namun apabila salah satu atau kedua operand itu bukan bertipe angka, maka PHP akan mengkonversinya menjadi angka.<\/p>\n<p>Seperti contoh program diatas, string \u201c9 kucing\u201d akan dikonversi menjadi angka. Dan menghasilkan angka 24 (aturan pengkonversian ini akan kita bahas sesaat lagi).<\/p>\n<p>Sebagai contoh lainnya, perhatikan kode PHP berikut ini:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n   $a=12;\r\n   $b=&quot;9 kucing&quot;;\r\n   echo $a AND $b;\r\n?&gt;\r\n<\/pre>\n<p>Nilai dari variabel $a dan $b masih sama dengan contoh saya yang pertama, namun kali ini saya membuat operator logika AND sebagai operasi yang akan diproses. Dan jika anda menjalankan program diatas, di browser akan tampil angka 1. Dari manakah angka 1 ini berasal?<\/p>\n<p>Operator AND membutuhkan 2 inputan bertipe boolean, yakni nilai TRUE atau FALSE. Namun karena saya menggunakan tipe integer \u201c12\u201d dan type string \u201c9 kucing\u201d, maka kedua operand ini akan dikonversi menjadi TRUE. Karena TRUE AND TRUE adalah TRUE, maka hasil $a AND $b pada contoh program diatas adalah TRUE.<\/p>\n<p>Namun proses type juggling PHP belum selesai, karena perintah echo membutuhkan inputan berupa string, bukan nilai boolean TRUE. Dan PHP akan mengkonversi boolean TRUE menjadi string \u201c1\u201d.<\/p>\n<p>Pemahaman tentang type juggling dalam PHP ini akan membantu kita untuk menghindari error kode program karena perubahan tipe data yang tidak terdeteksi.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pengertian Type Juggling dalam PHP PHP merupakan bahasa pemograman yang tidak terlalu ketat dalam aturan tipe data (dimana sebuah variabel dapat diisi dengan berbagai tipe<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1008,1007,1006,1005],"class_list":["post-2698","post","type-post","status-publish","format-standard","hentry","category-belajar-web-programming","tag-car-casting-kode","tag-casting","tag-juggling-php","tag-mengubah-tipe-data-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog<\/title>\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.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog\" \/>\n<meta property=\"og:description\" content=\"Pengertian Type Juggling dalam PHP PHP merupakan bahasa pemograman yang tidak terlalu ketat dalam aturan tipe data (dimana sebuah variabel dapat diisi dengan berbagai tipe\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-26T08:33:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-27T07:46:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting)\",\"datePublished\":\"2015-11-26T08:33:02+00:00\",\"dateModified\":\"2019-06-27T07:46:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/\"},\"wordCount\":403,\"publisher\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\"},\"keywords\":[\"car casting kode\",\"casting\",\"juggling php\",\"mengubah tipe data php\"],\"articleSection\":[\"Belajar Web Programming\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/\",\"name\":\"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-11-26T08:33:02+00:00\",\"dateModified\":\"2019-06-27T07:46:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\",\"name\":\"WebHozz Blog\",\"description\":\"Kursus Web &amp; Android di Jakarta Bandung\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\",\"name\":\"WebHozz\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/logo-persegi.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/logo-persegi.jpg\",\"width\":442,\"height\":442,\"caption\":\"WebHozz\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/author\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog","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.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/","og_locale":"en_US","og_type":"article","og_title":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog","og_description":"Pengertian Type Juggling dalam PHP PHP merupakan bahasa pemograman yang tidak terlalu ketat dalam aturan tipe data (dimana sebuah variabel dapat diisi dengan berbagai tipe","og_url":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/","og_site_name":"WebHozz Blog","article_published_time":"2015-11-26T08:33:02+00:00","article_modified_time":"2019-06-27T07:46:49+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/"},"author":{"name":"","@id":""},"headline":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting)","datePublished":"2015-11-26T08:33:02+00:00","dateModified":"2019-06-27T07:46:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/"},"wordCount":403,"publisher":{"@id":"https:\/\/www.webhozz.com\/blog\/#organization"},"keywords":["car casting kode","casting","juggling php","mengubah tipe data php"],"articleSection":["Belajar Web Programming"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/","url":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/","name":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting) - WebHozz Blog","isPartOf":{"@id":"https:\/\/www.webhozz.com\/blog\/#website"},"datePublished":"2015-11-26T08:33:02+00:00","dateModified":"2019-06-27T07:46:49+00:00","breadcrumb":{"@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/blog\/cara-mengubah-tipe-data-php-type-juggling-dan-type-casting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Cara Mengubah Tipe Data PHP (Type Juggling dan Type Casting)"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/blog\/#website","url":"https:\/\/www.webhozz.com\/blog\/","name":"WebHozz Blog","description":"Kursus Web &amp; Android di Jakarta Bandung","publisher":{"@id":"https:\/\/www.webhozz.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webhozz.com\/blog\/#organization","name":"WebHozz","url":"https:\/\/www.webhozz.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2018\/04\/logo-persegi.jpg","contentUrl":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2018\/04\/logo-persegi.jpg","width":442,"height":442,"caption":"WebHozz"},"image":{"@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"","url":"https:\/\/www.webhozz.com\/blog\/author\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/2698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/comments?post=2698"}],"version-history":[{"count":1,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/2698\/revisions"}],"predecessor-version":[{"id":2699,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/2698\/revisions\/2699"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/media?parent=2698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/categories?post=2698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/tags?post=2698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}