{"id":6514,"date":"2021-07-15T15:49:26","date_gmt":"2021-07-15T06:49:26","guid":{"rendered":"https:\/\/learningbox.co.jp\/?p=6514"},"modified":"2021-07-16T14:23:17","modified_gmt":"2021-07-16T05:23:17","slug":"blog_php","status":"publish","type":"post","link":"https:\/\/learningbox.co.jp\/en\/2021\/07\/15\/blog_php\/","title":{"rendered":"How to make 0-10 without using numbers in PHP"},"content":{"rendered":"<p>Nice to meet you. I'm Koizumi from the Development Department.<br \/>\nIn this article, I will explain how to make 0-10 without using numbers in PHP.<br \/>\n&nbsp;<\/p>\n<h2>first of all<\/h2>\n<p>PHP. <code>echo true<\/code> will output \"1\".<br \/>\nIt occurred to me that I could use this to create other numbers without using any numbers at all, so I tried it out.<br \/>\nIt's not interesting to just make one, so I added the following restrictions.<\/p>\n<p class=\"well\">\n1. type must be int (confirmed by var_dump)<br \/>\n2. be as short as possible<br \/>\n3. don't use constants (because it's too easy)\n<\/p>\n<p>If you know PHP, please try to predict what the code will be.<br \/>\nIf you want to test whether the code in the text is correct, you can easily try it by copying and pasting it into the following site.<br \/>\n\u21d2<a href=\"https:\/\/3v4l.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/3v4l.org\/<\/a><\/p>\n<h2>1 and 0<\/h2>\n<p>I indicated at the beginning <code>echo true<\/code> looks like 1, but internally it is of type bool.<br \/>\nYou'll need to cast it to an int, but you can use the commonly-used<code>intval()<\/code>is long, so the operator + is used.<br \/>\nAlso, \"true\" is 4 characters long, so you can shorten it to 3 characters by adding \"(empty string)\" and \"! to shorten it to 3 characters.<br \/>\nYou can create false as well and cast it to int to create 0 as well.<br \/>\nThe result is the following code.<\/p>\n<pre>var_dump(+!'') ; \/\/0\r\nvar_dump(+!'') ; \/\/1\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Bit negation(2)<\/h2>\n<p>2 is not 1+1, but the bitwise negation operator<code>~<\/code>to make it.<br \/>\nIt's complicated, so I won't explain it, but bitwise negating an integer n results in -(n+1). (If you are curious, please look it up in 2's complement)<br \/>\nThe sign can be reversed by adding a -, so<code>-~<\/code>can \"cast a positive number to an int and add one\".<br \/>\nThis is a frequent technique, so please keep it in mind.<\/p>\n<pre>var_dump(-~+!'') ; \/\/2\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Pi (3 to 5)<\/h2>\n<p>Speaking of 3, Pi.<code>pi()<\/code>I know. As it is, it is a float type and becomes 3.14... so cast it to int.<br \/>\nThe bitwise operators will cast to int, so<code>~~<\/code>You can truncate by putting two bit negations in a row, such as<br \/>\nBy combining pi and bit negation, we can make 3 to 5.<\/p>\n<pre>var_dump(~~pi()); \/\/3\r\nvar_dump(-~pi()); \/\/4\r\nvar_dump(-~-~pi()); \/\/5\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Bit shift (6 to 9)<\/h2>\n<p>6 is twice as many as 3. Normally, we use *, but 2(<code>-~+!''<\/code>) is six characters long, so instead we use the bit shift<code>&lt;&lt;<\/code>Use the<br \/>\nSince we are going to multiply by a power of 2, 6 is<code>3&lt;&lt;1<\/code>, 8 is<code>1&lt;&lt;3<\/code>can be represented respectively by<br \/>\nFor 7 and 9, we just need to make 6 and 8 each one bigger.<a href=\"https:\/\/www.php.net\/manual\/ja\/language.operators.precedence.php\">Operator precedence<\/a>so you need to enclose it in parentheses.<\/p>\n<pre>var_dump(pi()&lt;&lt;!&#039;&#039;) ; \/\/6\r\nvar_dump(-~(pi()&lt;&lt;!&#039;&#039;)) ; \/\/7\r\nvar_dump(!&#039;&#039; &lt;&lt;pi()); \/\/8\r\nvar_dump(-~(!&#039;&#039; &lt;&lt;pi())); \/\/9\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Hexadecimal(10)<\/h2>\n<p>10 is a in hexadecimal and can be expressed without using numbers.<br \/>\nUnlike the past, I think the code is easy to read.<\/p>\n<pre>var_dump(hexdec('a')); \/\/10\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>summary<\/h2>\n<p>Finally, I'll restate the code so far.<\/p>\n<pre>var_dump(+!'') ; \/\/0\r\nvar_dump(+!'') ; \/\/1\r\nvar_dump(-~+!'') ; \/\/2\r\nvar_dump(~~pi()); \/\/3\r\nvar_dump(-~pi()); \/\/4\r\nvar_dump(-~-~pi()); \/\/5\r\nvar_dump(pi()&lt;&lt;!&#039;&#039;) ; \/\/6\r\nvar_dump(-~(pi()&lt;&lt;!&#039;&#039;)) ; \/\/7\r\nvar_dump(!&#039;&#039; &lt;&lt;pi()); \/\/8\r\nvar_dump(-~(!&#039;&#039; &lt;&lt;pi())); \/\/9\r\nvar_dump(hexdec(&#039;a&#039;)); \/\/10<\/pre>\n<p>The readability of the code would have been very poor.<br \/>\nIt also gave me a chance to learn more about things I'm not usually aware of, such as bit negation and operator precedence.<br \/>\nIt's interesting to exercise your mind when you think about something completely different from the usual programming like this.<\/p>","protected":false},"excerpt":{"rendered":"\u306f\u3058\u3081\u307e\u3057\u3066\u3001\u958b\u767a\u90e8\u306e\u5c0f\u6cc9\u3067\u3059\u3002 \u4eca\u56de\u306e\u8a18\u4e8b\u3067\u306fPHP\u3067\u6570\u5b57\u3092\u4f7f\u308f\u305a\u306b0\uff5e10\u3092\u4f5c\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3059\u3002  [&hellip;]","protected":false},"author":56,"featured_media":6547,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[6,7,40],"tags":[62,74,73],"class_list":["post-6514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-7","category-40","tag-php","tag-74","tag-73"],"acf":[],"jetpack_featured_media_url":"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2021\/07\/PHP_1626246619.png","jetpack_shortlink":"https:\/\/wp.me\/sgMrZ4-blog_php","_links":{"self":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/6514","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\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/comments?post=6514"}],"version-history":[{"count":16,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/6514\/revisions"}],"predecessor-version":[{"id":6546,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/6514\/revisions\/6546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media\/6547"}],"wp:attachment":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media?parent=6514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/categories?post=6514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/tags?post=6514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}