{"id":5755,"date":"2021-03-02T14:00:26","date_gmt":"2021-03-02T05:00:26","guid":{"rendered":"https:\/\/learningbox.co.jp\/?p=5755"},"modified":"2021-03-02T14:17:30","modified_gmt":"2021-03-02T05:17:30","slug":"blog_php-program","status":"publish","type":"post","link":"https:\/\/learningbox.co.jp\/en\/2021\/03\/02\/blog_php-program\/","title":{"rendered":"Let's write a safe program using PHP type declarations!"},"content":{"rendered":"<p>Hello! I'm Teshima, who joined the Development Department just recently.<br \/>\nIn this article, I'd like to introduce the type declarations updated since PHP 7.0.<\/p>\n<p>I'll get to that in a minute, but have you ever had an error when coding because the type of data you assigned to a variable was different?<\/p>\n<p>Recently, PHP has been adopted for large-scale system development, and when the amount of code becomes enormous, unexpected errors may occur due to type problems.<br \/>\nFor those times, you can avoid errors by coding with types in mind!<\/p>\n<p>So in this post, we're going to talk about type declarations!<\/p>\n<h2 class=\"title\">\u00a0\u00a0\u00a0\u00a0table of contents<\/h2>\n<p class=\"well2\">About variables in PHP<br \/>\nAbout the function about the data type of PHP<br \/>\n- Declaring types for function arguments<br \/>\n- Declaring a type for a function return value<br \/>\n- Extra (Comparison operator (= = =))<\/p>\n<h2 class=\"title\">\u00a0\u00a0\u00a0\u00a0About variables in PHP<\/h2>\n<p>In PHP, if you assign an integer to a variable, it will be of type int, if you assign a string, it will be of type string, and the type of the variable is automatically determined by implicit consent.<br \/>\nThe feature is that you don't need to define a type when you create a variable.<br \/>\nOn the other hand, other languages, such as Java and C, require variables to have type declarations as well.<\/p>\n<pre><code>\/\/ Example\r\n  $string = \"abc\"; \/\/Substitute a string\r\n  var_dump($string); \/\/string(3) \"abc\" become string type\r\n  $int = 123; \/\/assign an integer\r\n  var_dump($int); \/\/int(123) become int type\r\n\r\n  \/\/In Java\r\n  String string; \/\/declare the variable string of type string\r\n  int integer; \/\/declare the variable integer of type int\r\n\r\n  \/\/ Cannot be assigned unless it is the declared data type\r\n<\/code><\/pre>\n<p>The above example shows that PHP allows you to code without being aware of types.<br \/>\nHowever, in this case, it is not known which type of data is contained in the variable, and unexpected bugs may occur.<\/p>\n<h2 class=\"title\">\u00a0\u00a0\u00a0\u00a0About PHP's Data Type Features<\/h2>\n<div class=\"mb-30\">\n<h3 class=\"subtitle\">Declaring types for function arguments<\/h3>\n<\/div>\n<pre><code>\/\/ A program that doubles the argument and returns it\r\nfunction doubleInt(int $int){\r\nreturn $int * 2;\r\n}\r\necho doubleInt(10); \/\/printed as 20\r\necho doubleInt(\"abc\"); \/\/outputs an error\r\n<\/code><\/pre>\n<p>An error occurs when a data type other than numeric (int) is passed by specifying an argument to a function.<br \/>\nIt also allows you to organize your functions in a simple way without having to write conversion code in the function.<br \/>\nEven in the unlikely event that a different person is given the program, you can rest assured that an error will occur and the program will terminate.<\/p>\n<h3 class=\"subtitle\">Declaring a type for a function return value<\/h3>\n<p>This section describes the case of a program that outputs a string as the return value even though the return value of the function introduced in 1 above is specified as int type.<\/p>\n<pre><code>function doubleInt(int $int): int{\r\nreturn \"Tatsuno Information System\";\r\n}\r\necho doubleInt(10); \/\/Error.\r\n<\/code><\/pre>\n<p>In a large system development, the code in the function also becomes more complex.<br \/>\nYou can avoid unexpected bugs by specifying the type of the return value to ensure that the processing is done correctly and does not result in an abnormal return value.<\/p>\n<p>Since PHP7.4, type declaration is available for properties. If you are interested, please check the official manual.<\/p>\n<h3 class=\"subtitle\">Extra (Comparison operator (= = =))<\/h3>\n<p>This is something I have experienced myself in the past, so I hope you find it helpful.<\/p>\n<h5>The difference between the comparison operators (= =) and (= = =)<\/h5>\n<p>In the case of (==), even if the comparison target is a different data type, type conversion is automatically performed and comparison is executed.<br \/>\nOn the other hand, in the case of (= = =), comparison execution is performed without changing the type.<\/p>\n<p class=\"point\">The official PHP manual describes the<br \/>\n(= =) is described as a strict comparison and (= =) is described as a loose comparison.<\/p>\n<pre><code>\/\/Example.\r\n\/\/comparison by number\r\nvar_dump(12345 == '12345'); \/\/bool(true) unexpected but sometimes useful\r\nvar_dump(12345 === '12345'); \/\/bool(false)\r\n\r\n\/\/comparison with numbers + strings\r\nvar_dump(12345 == '12345tatsuno'); \/\/bool(true) not expected\r\nvar_dump(12345 === '12345tatsuno'); \/\/bool(false)\r\n\r\nvar_dump(12345 == '12345abcde'); \/\/bool(true) unexpected\r\nvar_dump(12345 === '12345abcde'); \/\/bool(false)\r\n\r\nvar_dump('tatsuno' == 0); \/\/bool(true) unexpected\r\nvar_dump('tatsuno' === 0); \/\/bool(false)\r\n<\/code><\/pre>\n<p>When comparing numeric values and strings with *(==), if either value contains a numeric value, the string is converted to a numeric value before the comparison is performed.<br \/>\nThe result (12345 == '12345tatsuno') becomes (12345 == 12345), which is true.<br \/>\nAlso, if ('tatsuno' == 0), then (null == 0), which is also true.<\/p>\n<p>This kind of comparison with the (==) operator can lead to unexpected bugs.<br \/>\nBasically, you can make your program safe by performing comparisons with the (==) operator.<\/p>\n<h2 class=\"title\">\u00a0\u00a0\u00a0\u00a0summary<\/h2>\n<p>How did it go?<br \/>\nI hope this will be helpful for those who usually code with type conversions without thinking (although I think there are not many...) and those who are going to learn programming.<br \/>\nLet's code with types in mind so we can write safer programs!<\/p>\n<p>That's all I have to say, thank you!<\/p>","protected":false},"excerpt":{"rendered":"\u3053\u3093\u306b\u3061\u306f\uff01\u3064\u3044\u6700\u8fd1\u958b\u767a\u90e8\u306b\u5165\u3063\u305f\u8c4a\u5cf6\u3067\u3059\u3002 \u4eca\u56de\u306fPHP 7.0\u4ee5\u964d\u306b\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3055\u308c\u305f\u578b\u5ba3\u8a00\u306b\u3064\u3044\u3066\u3054\u7d39\u4ecb [&hellip;]","protected":false},"author":51,"featured_media":5780,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"0","_wpcom_ai_launchpad_first_post":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[6,7,40],"tags":[63,62,64],"class_list":["post-5755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-7","category-40","tag-function","tag-php","tag-64"],"acf":[],"jetpack_shortlink":"https:\/\/wp.me\/pgMrZ4-1uP","jetpack_featured_media_url":"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2021\/02\/1984.png","_links":{"self":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/comments?post=5755"}],"version-history":[{"count":26,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5755\/revisions"}],"predecessor-version":[{"id":5800,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/5755\/revisions\/5800"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media\/5780"}],"wp:attachment":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media?parent=5755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/categories?post=5755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/tags?post=5755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}